CakePHP 3.3 - 验证无效

时间:2017-02-07 12:30:24

标签: php authentication cakephp cakephp-3.3

我是CakePHP的新手,我看了一个关于如何实现Login视图的tutorial on YouTube

我一步一步地按照教程,但$user似乎永远不会成真。即使我尝试使用正确的凭据登录,也会出现“登录不正确”错误。

我的错误在哪里?

UsersController.php中的登录功能

public function login() {
    if ($this->request->is('post')) {
       $user = $this->Auth->identify();
        if ($user) {
            /* never gets here ****************/
            $this->Auth->setUser($user);
            return $this->redirect(['controller' => 'posts']);
        }
        //Bad Login
        $this->Flash->error('Incorrect Login');
    }
}

在AppController中初始化Auth

$this->loadComponent('Auth', [
            'authenticate' => [
                'Form' => [
                    'fields' => [
                        'username' => 'email',
                        'password' => 'password'
                    ]
                ]
            ],
            'loginAction' => [
                'controller' => 'Users',
                'action' => 'login'
            ]
        ]);

登录表单

<br>
<div class="index large-4 medium-4 large-offset-4 medium-offset-4 columns">
    <div class="panel">
        <h2 class="text-center">
            LOGIN
        </h2>
        <?= $this->Form->create(); ?>
            <?= $this->Form->input('email'); ?>
            <?= $this->Form->input('password', array('type' => 'password')); ?>
            <?= $this->Form->submit('Login', array('class' => 'button')); ?>
        <?= $this->Form->end(); ?>
    </div>
</div>
用户模型中的

_setPassword

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

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,我将$this->$password传递给哈希函数,它应该只是$password