php - $this->Auth->identify();在 cakephp 3.2 中返回 false

标签 php authentication cakephp-3.0 cakephp-3.x

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

用户 Controller 中的登录函数

  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 获取电子邮件和密码。

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

最佳答案

请像下面这样更改:-

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

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'
    ]);
}

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

关于php - $this->Auth->identify();在 cakephp 3.2 中返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35332297/

相关文章:

javascript - 使用 PHP 和 Javascript 输入数据库

FreeBSD jail 上的 PHP、Apache 和 MySQL

java - 如何通过以编程方式设置用户名和密码来连接到 ActiveMQ 服务器?

php - 我可以启动一个脚本,使其独立于 Linux 上的父进程吗?

c# - WebAPI/Owin - 登录后身份未被授权

mobile - 验证 ADF 移动应用程序中的用户名和密码

php - cakephp3 读取组件中的 session

authentication - 将额外的数据传递给Finder身份验证

php - CakePHP 3 - 将实体字段 FLOAT 保存为 INTEGER

php - Laravel 5 – 清除共享主机服务器中的缓存