用户登录不工作,显示错误“用户名或密码不正确”

时间:2018-02-17 08:21:11

标签: php cakephp cakephp-3.0

我有 UsersController 我正在尝试使用电子邮件和密码字段登录,我也使用密码哈希,我的密码字段也是文本数据类型。哪个工作正常,但我无法登录,显示错误

  

用户名或密码不正确

UsersController.php

public function login()
    {
        if ($this->request->is('post'))
        {
            $user = $this->Auth->identify();
            if ($user)
            {
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error('Your username or password is incorrect.');
        }

    }

AppController.php

   public function initialize()
        {
            parent::initialize();
            $this->loadComponent('RequestHandler');
            $this->loadComponent('Flash');
            $this->loadComponent('Auth',[
                'authenticate'=>[
                    'Form'=>[
                        'fields'=>[
                            'username'=>'email',
                            'password'=>'password'
                        ]
                    ]
                ],

                'loginRedirect'=>[
                    'controller'=>'Users',
                    'action'=>'index'
                ],

                'loginAction'=>[
                    'controller'=>'Users',
                    'action'=>'login'
                ]
            ]);
             $this->Auth->allow(['add']);
}

login.ctp

<h1>Login</h1>

<?= $this->Form->create() ?>
<?= $this->Form->control('email') ?>
<?= $this->Form->control('password') ?>
<?= $this->Form->button('Login') ?>
<?= $this->Form->end() ?>

UsersTable

id char(100)
name varchar(100)
email varchar(100)
password (text)

1 个答案:

答案 0 :(得分:0)

请在您的实体类中添加密码哈希(scr / Model / Entity / User.php)

    use Cake\Auth\DefaultPasswordHasher;    
    protected function _setPassword($password)
        {
            if (strlen($password) > 0) {
              return (new DefaultPasswordHasher)->hash($password);
            }
        }

添加此内容后,请创建一个新用户,然后尝试从新创建的用户登录。

我这样做是在这里工作

UsersController.php

public function login()
{
    if ($this->request->is('post'))
    {
        $user = $this->Auth->identify();
        if ($user)
        {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Flash->error('Your username or password is incorrect.');
    }

}

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

AppController.php

$this->loadComponent('Auth',[
            'authenticate'=>[
                'Form'=>[
                    'fields'=>[
                        'username'=>'email',
                        'password'=>'password'
                    ]                        
                ]
            ],

            'loginRedirect'=>[
                'controller'=>'Users',
                'action'=>'index'
            ],

            'loginAction'=>[
                'controller'=>'Users',
                'action'=>'login'
            ]
        ]);
         $this->Auth->allow(['add']);

User.php [实体]

protected function _setPassword($password)
{
    if (strlen($password) > 0) {
      return (new DefaultPasswordHasher)->hash($password);
    }
}

login.ctp

<h1>Login</h1>
<?= $this->Form->create() ?>
<?= $this->Form->control('email') ?>
<?= $this->Form->control('password') ?>
<?= $this->Form->button('Login') ?>
<?= $this->Form->end() ?>

如果您不能使用上述代码,可以尝试在AppController上添加此代码

'fields'=>[ 'username'=>'email', 'password'=>'password' ], 
'userModel' => 'Users'