gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:35:33 25 4
gpt4 key购买 nike

我正在登录网站的一部分。因此,我不想在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com