【问题标题】:$this->Auth->identify(); returning false in cakephp 3.2$this->Auth->identify();在 cakephp 3.2 中返回 false
【发布时间】:2016-02-11 06:28:53
【问题描述】:

我在 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(); 总是返回假。 为什么我无法登录? 在数据库中密码存储为

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

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

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

【问题讨论】:

  • 嗨,请检查链接,因为您使用的是电子邮件而不是用户名,所以请执行本文档中的一些设置:-book.cakephp.org/3.0/en/controllers/components/…`
  • “配置身份验证处理程序”下的第四个示例
  • 是的,如果我使用用户名,那么它工作正常。@A-2-A

标签: php login cakephp-3.0 cakephp-3.x


【解决方案1】:

请更改如下:-

首先在您的控制器中添加此代码(最好是 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'
    ]);
}

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

【讨论】:

  • 我已经尝试过了,但在我的情况下,它每次都返回 false
  • @SoubhagyaKumar 请用您的实际代码提出一个新问题,您肯定会得到答案
【解决方案2】:

我遇到了同样的问题。我正在使用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']
            ]
        ],
    ]);

真的吗?!!

【讨论】:

    猜你喜欢
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 2018-01-03
    • 2014-06-25
    相关资源
    最近更新 更多