cakephp - CakePHP Auth->identify() 返回 false
问题描述
我正在玩 CakePHP,但似乎无法登录。似乎 $this->Auth->identify() 不断返回 false 并且不允许我登录。
我已经阅读了有关我的问题的所有以前的帖子,但是,没有人为我提供解决方案。我尝试登录的用户都是使用 cake 密码哈希创建的,我检查过该密码已存储在数据库中。我检查了数据库中设置为 varchar (255) 的密码字段长度,我检查了 Auth => authenticate => Form => 字段设置为正确的值(login.ctp 字段也是正确的)。我还尝试按照某人的建议将 $this->Form->control() 更改为 $this->Form->input() ,但没有运气。
应用控制器:
$this->loadComponent('Auth', [
'loginRedirect' => [
'controller' => 'Users',
'action' => 'classes'
],
'logoutRedirect' => [
'controller' => 'Users',
'action' => 'index'
]
]);
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
]
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
]
]);
UsersController 中的 login() 函数:
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
pj($user);
if ($user) {
$this->Auth->setUser($user);
return $this->redirect(['controller' => 'users']);
}
$this->Flash->error(__('Invalid username or password, try again'));
}
}
登录.ctp:
<div class="users form">
<?= $this->Form->create() ?>
<fieldset>
<legend><?= __('Please enter your username and password') ?></legend>
<?= $this->Form->input('email') ?>
<?= $this->Form->input('password') ?>
</fieldset>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->end() ?>
</div>
编辑:我忘了补充说我可以成功添加用户,我只是无法登录。
解决方案
您在 AppController 中加载 AuthComponent 两次。未加载带有表单字段配置的第二次加载。
使用所需的配置一次加载组件。
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
]
],
'loginRedirect' => [
'controller' => 'Users',
'action' => 'classes'
],
'logoutRedirect' => [
'controller' => 'Users',
'action' => 'index'
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
]
]);
推荐阅读
- python - 需要帮助使用 Python 生成 XML
- android - 为什么具有产品风格的项目没有 lintDebug 任务?
- python - 在直方图顶部绘制数据点
- php - 如何仅从 MySQL 获取与 id 对应的文本并将其存储为 PHP 中的变量
- amp-html - 作为供应商开始使用 AMP 和 RTC
- c++ - ROS C++ 近似时间同步器回调在节点成员类中不起作用(同步超出范围)
- c++ - 当 Visual Studio 未以管理员身份运行(GLFW/glad 应用程序)时,KernelBase.dll 异常代码 0x0EEDFADE(但没有崩溃)
- c++ - Qt中具有多个窗口的应用程序
- java - 设置 Maven 项目时构建失败
- javascript - 为每 15 分钟时间戳拆分数据并循环遍历它