为什么$ user = $ this-> Auth-> identify()总是返回false?

时间:2018-06-07 16:35:31

标签: cakephp

我开始使用CakePHP 3.6,但我无法创建身份验证登录。新的auth函数$this->Auth->identify()总是返回false。

我的代码:

public function initialize()
{
    parent::initialize();

    $this->loadComponent('RequestHandler', [
        'enableBeforeRedirect' => false,
    ]);

    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'authorize' => ['Controller'],
        'loginRedirect' => [
            'controller' => 'Users',
            'action' => 'index'
        ],
        'logoutRedirect' => [
            'controller' => 'Users',
            'action' => 'login',
            'home'
        ],

这是我设置字段的地方:

        'authenticate' => [
            'Form' => [
                'fields' => ['username' => 'username', 'password' => 'password']
            ]
        ]

    ]);

    /*
     * Enable the following components for recommended CakePHP security settings.
     * see https://book.cakephp.org/3.0/en/controllers/components/security.html
     */
    //$this->loadComponent('Security');
    //$this->loadComponent('Csrf');
}
class UsersController extends AppController
{
    // Other methods..

    public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);    
        $this->Auth->allow(['logout']);
    }

    public function login()
    {        
        if ($this->request->is('post')) {

这总是返回false,但为什么?

            $user = $this->Auth->identify();

            debug($this->request);
            debug($user);
            die;

            if ($user) {
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error(__('Invalid username or password, try again'));
        }
    }

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

class User extends AppModel {

    public $validate = array(
        'username' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A username is required'
            )
        ),
        'password' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A password is required'
            )
        ),
        'email' => array(
            'email' => array(
                'rule'    => array('email', true),
                'message' => 'Please supply a valid email address.'
            ),
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A email is required'
            )
        ),
        'role' => array(
            'valid' => array(
                'rule' => array('inList', array('admin')),
                'message' => 'Please enter a valid role',
                'allowEmpty' => false
            )
        )
    );

    public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {
            $passwordHasher = new SimplePasswordHasher();
            $this->data[$this->alias]['password'] = $passwordHasher->hash(
                $this->data[$this->alias]['password']
            );
        }
        return true;
    }

}

我按照cakephp网站上的教程,运气不好。救命啊!

1 个答案:

答案 0 :(得分:0)

确保使用散列密码进行测试,因为Auth组件期望对其进行哈希处理,因此这是我的错误。