$this->身份验证->识别();在 Cakephp 3.2 中返回 false


$this->Auth->identify(); returning false in cakephp 3.2

我已经在 cakephp 3.2 中完成了登录的所有设置,但是在登录时它返回 false。

用户控制器中的登录功能

  public function login() {
        $this->viewBuilder()->layout('');
        if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            pj($user);//returning false
            if ($user) {
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error(__('Invalid username or password, try again'));
        }
    }

此函数$user = $this->Auth->identify();始终返回 false。为什么我无法登录?在数据库中,密码存储为

$2y$10$4aD6Ye6YcmPGKgI/CmhJBO0E//9tV.KvhJIOFAhajyqt8vfxDVASC

我收到来自$this->request->data的电子邮件和密码。

任何建议将不胜感激。提前谢谢你。

请像下面这样更改:-

首先在您的控制器中添加此代码(最好是应用程序控制器(:-

public function initialize()
{
    parent::initialize();
    $this->loadComponent('Auth', [
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login',
            'plugin' => 'Users'
        ],
        'authError' => 'Did you really think you are allowed to see that?',
        'authenticate' => [
            'Form' => [
                'fields' => ['username' => 'email']
            ]
        ],
        'storage' => 'Session'
    ]);
}

然后使用您的代码。它将正常工作。

我遇到了同样的问题。 我正在使用Cake 3.7.这是给谁使用routes.

我在路由文件中创建了用于登录的路由。

$routes->connect('/login',
        ['controller' => 'Users', 'action' => 'login'],
        ['_name' => 'login']
    );

现在,当您使用以下代码时,Auth不会登录,identify()返回 false。

$this->loadComponent('Auth', [
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login'
        ],
        'loginRedirect' => ['what ... ever'],
        'logoutRedirect' => '/',
        'authError' => 'Please log in to view your account.',
        'authenticate' => [
            'Form' => [
                'fields' => ['username' => 'email']
            ]
        ],
    ]);

所以。。。你看。。。您必须调用路由名称才能尝试登录。

$this->loadComponent('Auth', [
        'loginAction' => ['_name' => 'login'], // <= Here is Mr glitch 
        'loginRedirect' => ['what ... ever'],
        'logoutRedirect' => '/',
        'authError' => 'Please log in to view your account.',
        'authenticate' => [
            'Form' => [
                'fields' => ['username' => 'email']
            ]
        ],
    ]);

真?!!