由于某种原因,我无法登录我注册的帐户。更多信息如下。
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
谢谢!
答案 0 :(得分:2)
似乎没有任何明显的错误,除了_setPassword()
方法中缺少空洞检查,这会阻止空$password
进行哈希处理。您应该执行类似于文档中显示的内容:
if (strlen($password) > 0) {
return (new DefaultPasswordHasher)->hash($password);
}
参见 Cookbook > Controllers > Components > Authentication > Hashing Passwords
同样,FormHelper::create()
方法也不会使用字符串,因为向后兼容性原因IIRC,它不会出错。如果您没有有效的上下文传递,那么根本不会传递任何值。
话虽这么说,你必须自己做更多的调试。首先使用DefaultPasswordHasher::validate()
方法手动验证存储在数据库中的散列密码,以确保已经散列了正确的值。
然后在身份验证代码流中设置一些断点以查明可能出错的地方,请检查:
FormAuthenticate::authenticate()
FormAuthenticate::_checkFields()
BaseAuthenticate::_findUser()
BaseAuthenticate::_query()
是否正在读取正确的请求数据,查询条件是否按预期构建,是否以及为密码验证返回的值等等...
答案 1 :(得分:0)
好吧,我整个上午和下午都浪费了。
我认为我的密码列长度是255但实际上是32.我明确检查了错误列的长度,显示了4次。
感谢@ndm的帮助。