DefaultPasswordHasher CakePHP的问题

时间:2016-10-13 14:05:07

标签: cakephp

早上好,

登录我的网络应用程序时出现问题。

当我在我的应用上注册时,没有错误(在我看来):

//Register
public function register() {
    $user = $this->Users->newEntity();
    if($this->request->is('post')) {
        $this->request->data('status', '0');
        $this->request->data('role', '0');
        $user = $this->Users->patchEntity($user, $this->request->data);
        if($this->Users->save($user)) {

            $this->Flash->success('You are registered and can login');
            return $this->redirect(['action' => 'login']);
        } else {
            $this->Flash->error('You are not registered');
        }
    }
    $this->set(compact('user'));
    $this->set('_serialize', ['user']);
}

当用户注册时我无法登录(我不知道原因),但是如果我将登录其他用户并且我更改了旧用户的密码 - >旧用户可以登录。

我不知道问题的原因在哪里。

登录:

//Login
public function login() {
    if($this->request->is('post')) {
        $user = $this->Auth->identify();

        //dump($user);
        //die();
        if($user) {
            if($user['status'] == '1') {
                $this->Auth->setUser($user);
                return $this->redirect(['controller' => 'tasks']);
            }elseif($user['status'] == '0') {
                $this->Flash->error('You are not authorized to Log In. Please contact with Admin of Application');
            }
        } else {
            //Bad Login
            $this->Flash->error('Incorrect Login or Password');
        }
    }
}

有趣的是,当我登录时,我写了

dump($user);
die();

我收到" false" ...

Login.ctp:

<div class="index large-4 medium-4 large-offset-4 medium-offset-4 columns">
<div class="panel">
    <center><?= $this->HTML->image('small_logo.png'); ?></center>
    <?= $this->Form->create(); ?>
        <?= $this->Form->input('email'); ?>
        <?= $this->Form->input('password'); ?>
        <center><?= $this->Form->submit('Log In', array('class' => 'button')); ?></center>
    <?= $this->Form->end(); ?>
</div>

我认为这个问题与&#39; DefaultHasherPassword&#39;有关。但我不知道问题在哪里以及如何解决。请帮忙。

就像新用户无法登录一样,但是当用户被修改(密码已更改)时 - 他可以登录。

1 个答案:

答案 0 :(得分:0)

您可能忘记将字段用户名添加为电子邮件(不确定,因为您没有共享您的代码)

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