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