由于某些原因,我无法登录到注册的帐户。下面的更多信息。
功能来自Userscontroller.php
public function login() {
if ($this->request->is('post')) {
$auth = $this->Auth->identify(); // Returns false
debug($this->request->getData()); // Return email & with unhashed password
debug($auth);
if ($auth) {
$this->Auth->setUser($auth);
$this->redirect($this->Auth->redirectUrl());
} else {
$this->Flash->error('E-mail or password is wrong.');
}
}
}
public function register() {
$user = $this->Users->newEntity();
$this->set('user', $user);
$this->loadModel('Groups');
$group = $this->Groups->newEntity();
$this->set('group', $user);
if ($this->request->is('post')) {
// Check if passwords matches
$pass = $this->request->getData('password');
$con_pass = $this->request->getData('password_confirm');
if ($pass !== $con_pass) {
return $this->Flash->error('Passwords don't match');
}
// Patch entities
$group = $this->Groups->patchEntity($group, $this->request->getData());
$user = $this->Users->patchEntity($user, $this->request->getData());
// Make group and user
if (empty($group->errors()) && empty($user->errors())) {
// Group
if (!$this->Groups->save($group)) {
return $this->Flash->error('Something went wrong');
}
// User
$user->group_id = $group->id;
if ($this->Users->save($user)) {
$this->Flash->success('Welkom ' . $user->name . '!');
// return $this->redirect(['action' => 'register']);
} else {
return $this->Flash->error('something went wrong2');
}
}
}
}
AppController中的auth组件:
$this->loadComponent('Auth', [
'userModel' => 'Users',
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
]
],
//'authError' => false,
'storage' => 'Session'
]);
登录表格:
<?= $this->Form->create('User'); ?>
<?= $this->Form->email('email', ['placeholder' => 'E-mail', 'maxlength' => '42', 'label' => false]) ?>
<?= $this->Form->password('password', ['type' => 'password', 'placeholder' => 'Wachtwoord', 'maxlength' => '32', 'label' => false]) ?>
<?= $this->Form->submit('Login', ['class' => 'button']) ?>
<?= $this->Form->end(); ?>
用户实体:
class User extends Entity {
protected $_accessible = [
'group_id' => true,
'name' => true,
'email' => true,
'password' => true,
'profile_img_url' => true,
'pass_reset_time' => true,
'creation_date' => true,
'modified_date' => true
];
protected function _setPassword($password) {
return (new DefaultPasswordHasher)->hash($password);
}
protected $_hidden = [
'password'
];
}
用用户使用哈希密码正确保存在数据库中。
当我尝试登录$this->Auth->identify();
时,总是返回false。
我尝试/要知道的事情:
- 我正在尝试使用电子邮件和密码登录。
- db中的表名是
users
- 更新盐(并创建一个新帐户并使用该帐户登录(
- 密码列长度为255。
- 检查验证文档
- 检查博客教程
- 在堆栈和其他网站上检查了许多与此有关的问题,但还没有解决我的问题。
- 用户可以正确存储。但是,一旦我尝试登录,它就不会让我。
- 我试图在没有密码HASHER功能的情况下登录,并且使用未打过的密码也无效。
- 在不同的浏览器中检查&amp;删除缓存。
谢谢!
似乎没有任何明显的错误,除了_setPassword()
方法中缺少空虚检查,该检查可以阻止空的$password
被哈希。您应该做类似文档中显示的事情:
if (strlen($password) > 0) {
return (new DefaultPasswordHasher)->hash($password);
}
请参阅 Cookbook> Controllers>组件>身份验证> HASHINing密码
也没有FormHelper::create()
方法也不采用字符串,它仅出于向后兼容的原因IIRC而出现错误。如果您没有有效的上下文可以通过,请不要通过任何值。
DefaultPasswordHasher::validate()
方法手动验证存储在数据库中的哈希广告,以确保已使用正确的值。
然后在身份验证代码流中设置一些断点以确定出现问题的位置,请检查:
-
FormAuthenticate::authenticate()
-
FormAuthenticate::_checkFields()
-
BaseAuthenticate::_findUser()
-
BaseAuthenticate::_query()
是否正在读取正确的请求数据,是否按预期构建查询条件,是否以及返回什么值进行密码验证等...
好吧,我浪费了整个早晨和下午。
我以为我的密码列长度为255,但实际上是32。我显然检查了错误的列的长度。
感谢您的帮助@NDM。