$this->Auth->identify();在 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。 为什么我不能登录? 在数据库中密码存储为

yaD6Ye6YcmPGKgI/CmhJBO0E//9tV.KvhJIOFAhajyqt8vfxDVASC

我正在从 $this->request->data 获取电子邮件和密码。

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

请按如下更改:-

首先在您的控制器中添加此代码(最好是 App 控制器):-

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() returns 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']
            ]
        ],
    ]);

真的吗?!!