因为某些原因,我不能登录到我注册的帐户。下面是更多的信息。
函数来自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。
我想知道的是:
users谢谢!
发布于 2018-02-26 13:51:32
似乎没有任何明显的错误,除了在_setPassword()方法中缺少一个空检查,这将防止空$password被散列。您应该做一些类似于文档中显示的事情:
if (strlen($password) > 0) {
return (new DefaultPasswordHasher)->hash($password);
}请参阅Cookbook > Controllers > Components > Authentication > Hashing Passwords
另外,FormHelper::create()方法也不接受字符串,它只不会因为向后兼容的原因而出错。如果您没有要传递的有效上下文,则根本不传递任何值。
话虽如此,您将不得不自己做更多的调试。首先,使用DefaultPasswordHasher::validate()方法手动验证存储在数据库中的散列密码,以确保正确的值已被散列。
然后,在身份验证代码流中设置一些断点,以确定可能出错的地方,请检查:
FormAuthenticate::authenticate()FormAuthenticate::_checkFields()BaseAuthenticate::_findUser()BaseAuthenticate::_query()是否读取正确的请求数据,是否按预期构建查询条件,是否返回用于密码验证的值,等等.
发布于 2018-02-26 14:55:33
好吧,我浪费了整个上午和下午。
我以为我的密码列长是255,但实际上是32。显然,我检查了错误的列的长度,大约4次。
谢谢帮助@ndm。
https://stackoverflow.com/questions/48985886
复制相似问题