mysql - CakePHP 3.x 从 AppController 以外的其他 Controller 启动 Auth 组件给出错误结果

标签 mysql authentication cakephp-3.0

我正在登录网站的一部分。 因此,我不想在 AppController 中启动身份验证,但在实际的 Controller 中我需要它。

在 PremiumController 中:

[...]

use Cake\Controller\Component\AuthComponent;

[...]

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

    $this->loadComponent('Auth', [
        'Basic' => ['userModel' => 'Clients'],
        'Form' => ['userModel' => 'Clients'],
        'loginAction' => [
            'controller' => 'Premium',
            'action' => 'login',
            'plugin' => false
        ],
        'authError' => 'Unauthorized Access',
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'naam',
                    'password' => 'pass'
                ],
                'userModel'=>'Clients', //Other table than Users
                'passwordHasher' => [
                    'className' => 'Premium', //use MD5
                ]
            ]
        ],
        'loginRedirect' => [
            'controller' => 'Premium',
            'action' => 'dashboard'
        ],
        'logoutRedirect' => [
            'controller' => 'Search',
            'action' => 'index'
        ]
    ]);


}

[...] 并降低登录部分

public function login()
{

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

我没有得到任何结果。 $user 仍为“false”

我怎样才能做到这一点?有没有办法正确调试 Auth 组件?

最佳答案

尝试在 session 中分配 key ,以便 Auth 检查 session 中是否存在该 key 。

像这样更改你的代码,

$this->loadComponent('Auth', [
    'Basic' => ['userModel' => 'Clients'],
    'Form' => ['userModel' => 'Clients'],
    'loginAction' => [
        'controller' => 'Premium',
        'action' => 'login',
        'plugin' => false
    ],
    'authError' => 'Unauthorized Access',
    'authenticate' => [
        'Form' => [
            'fields' => [
                'username' => 'naam',
                'password' => 'pass'
            ],
            'userModel'=>'Clients', //Other table than Users
            'passwordHasher' => [
                'className' => 'Premium', //use MD5
            ]
        ]
    ],
    'loginRedirect' => [
        'controller' => 'Premium',
        'action' => 'dashboard'
    ],
    'logoutRedirect' => [
        'controller' => 'Search',
        'action' => 'index'
    ]
]);
$this->Auth->__set('sessionKey', 'Auth.client');

关于mysql - CakePHP 3.x 从 AppController 以外的其他 Controller 启动 Auth 组件给出错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45337795/

相关文章:

php - 如果未登录 WordPress,则重定向到特定页面

asp.net - 使用Asp.net在Asp页面中设置Session

cakephp 3 多级关联保存

调试 save() 返回 false CakePHP 3.0

mysql - 在 mySQL 中哪里可以找到数据库当前的时区?

python - 如何在peewee中选择虚拟列?

php - MySQL 查询问题

php mysql查询删除项目,即使该项目不存在于表中

flash - WCF 中基于 token 的身份验证

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