【问题标题】:Cakephp 2.4.5 Authentication functionality not workingCakephp 2.4.5 身份验证功能不起作用
【发布时间】:2014-04-11 11:28:27
【问题描述】:

我是 Cakephp 的新手,正在尝试制作一个简单的用户注册和登录应用程序。编写代码后,我面临的问题是我的登录表单正在授权任何电子邮件和密码,并重定向到我在登录后设置的页面。 这是我的代码:

AppController.php

App::uses('Controller', 'Controller');
class AppController extends Controller 
{
var $components = array('Auth');                                                                      
}

UserController.php

<?php
//Filename  -UsersController.php

//Registering new users and login
class UsersController extends AppController
{

 public $helpers = array('Html','Form','Session');
 public $components = array('Session',
                        'Auth' => array(
                           'authenticate' => array(
                           'Form' => array (
                           'fields' => array('username'=>'email','password'=>'password')
 ))));

 /**
  *  Function for saving a new user
  */
  public function index()
  {
  if($this->request->is('post'))
  {
  //print_r($this->request->data);die;  
  $this->User->create();
  if($this->User->save($this->request->data))
  {
  $this->Session->setFlash(__('Congrats, you are registered successfully'));
  return $this->redirect(array('action'=>'index')); 
  } 
  $this->Session->setFlash(__('Unable to store the user'));
  } 
  }


  /**
   *  Function for login functionality
   */
   function beforeFilter()
{
 $this->Auth->loginRedirect=array('controller'=>'pages','action'=>'welcome');
 $this->Auth->logoutRedirect = array('controller'=>'pages','action'=>'welcome');
 }

 public function login()
 {
    if($this->request->is('post'))
    {
      if($this->Auth->login())
      {
        //print_r($this->request->data);die;
        $this->redirect($this->Auth->redirect());
      } 
      $this->Session->setFlash(__('Invalid username or password, try again'));
    }
 }

 public function logout() {
                        return $this->redirect($this->Auth->logout());
                          } }

在模型中User.php

    <?php 
        // Filename - User.php
        // Model for registering new users
       App::uses('AppModel', 'Model');
       App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
       class User extends AppModel
       {        
public $validate = array(
                        'username'=>array('required'=>array(
                        'rule'=>array('notEmpty'),                                                  
                        'message'=>'Username is required'
                                                           )),
                        'password'=>array('required'=>array(
                         'rule'=>array('notEmpty'),                                   
                         'message'=>'Password is required'
                                                           )),
                        'email'=>array('required'=>array(
                        'rule'=>array('notEmpty'),                                   
                        'message'=>'Email is required'
                                                        ))  
                        );
     public function beforeSave($options = array())
 {
     if(isset($this->data['User']['password']))
{
$passwordHasher = new SimplePasswordHasher();
$this->data['User']['password']=
    $passwordHasher->hash($this->data['User']['password']);
    }
    return true;
        }
       }

login.ctp

   <?php
         echo $this->Form->create();
         echo $this->Form->input('username',array('style'=>'width:200px;'));
         echo $this->Form->input('password',array('style'=>'width:200px;'));
         echo $this->Form->end('Sign in');
         ?>

请帮我解决这个问题。提前致谢。

【问题讨论】:

标签: cakephp-2.4


【解决方案1】:

如果您想要一个简单的用户注册/登录应用程序,我建议您查看CakeDC Users Plugin

【讨论】:

    【解决方案2】:

    首先在您的 AppController 上而不是在 UsersController 上使用 AuthComponents。所以,你的代码-

    在 AppController.php 中

    App::uses('Controller', 'Controller');
     class AppController extends Controller 
     {
     public $helpers = array('Html','Form','Session');
     public $components = array('Session',
                            'Auth' => array(
                               'authenticate' => array(
                                    'Form' => array (
                                        'fields' => array('username'=>'email','password'=>'password')
     ))));  
    
    
    }
    

    UsersController.php

    从 UsersController.php 中删除 public $helpers, public $components

    在 login.ctp 中

    因此,您将默认 username 更改为 email,因此在视图中使用 email

       <?php
             echo $this->Form->create();
             echo $this->Form->input('email',array('style'=>'width:200px;'));
             echo $this->Form->input('password',array('style'=>'width:200px;'));
             echo $this->Form->end('Sign in');
             ?>
    

    【讨论】:

    • 它解决了我的问题。谢谢你。关于 Cake 登录,我想知道的另一件事是,假设我使用电子邮件和密码登录,然后单击浏览器的后退按钮(我使用的是 google chrome),它再次将我带到登录屏幕。现在,如果我输入任何错误的密码或编辑电子邮件。它会登录。我不明白为什么会这样?是不是因为我的浏览器中 cake php 做的 cookie?
    • 好吧,如果它解决了您的问题,请勾选此作为答案。你问的关于后退按钮的问题,我有这个答案,但你需要创建另一个问题,因为我认为这将是一个很好的问题,其他人可能会从这个问题中得到帮助。最好不要在这里回答。谢谢...
    猜你喜欢
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多