[英]Users login not working, showing error “incorrect username or password”
I have UsersController i'm trying to login using email and password fields, i'm also using Password Hashing, also my password field is of text datatype. 我有UsersController,我正在尝试使用电子邮件和密码字段登录,我也在使用密码哈希,而且我的密码字段是text数据类型。 which is working fine, but i'm unable to login, showing me an error
工作正常,但我无法登录,显示错误
username or password is incorrect
用户名或密码不正确
UsersController.php
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('Your username or password is incorrect.');
}
}
AppController.php
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth',[
'authenticate'=>[
'Form'=>[
'fields'=>[
'username'=>'email',
'password'=>'password'
]
]
],
'loginRedirect'=>[
'controller'=>'Users',
'action'=>'index'
],
'loginAction'=>[
'controller'=>'Users',
'action'=>'login'
]
]);
$this->Auth->allow(['add']);
}
login.ctp
<h1>Login</h1>
<?= $this->Form->create() ?>
<?= $this->Form->control('email') ?>
<?= $this->Form->control('password') ?>
<?= $this->Form->button('Login') ?>
<?= $this->Form->end() ?>
UsersTable 用户表
id char(100)
name varchar(100)
email varchar(100)
password (text)
Please add password hasher at your Entity Class( scr/Model/Entity/User.php ) 请在您的实体类(scr / Model / Entity / User.php)上添加密码哈希器
use Cake\Auth\DefaultPasswordHasher;
protected function _setPassword($password)
{
if (strlen($password) > 0) {
return (new DefaultPasswordHasher)->hash($password);
}
}
After adding this, make a new user then try to login from newly created user. 添加后,创建一个新用户,然后尝试从新创建的用户登录。
I did this which is working here 我做了这在这里工作
UsersController.php UsersController.php
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('Your username or password is incorrect.');
}
}
public function logout()
{
return $this->redirect($this->Auth->logout());
}
AppController.php AppController.php
$this->loadComponent('Auth',[
'authenticate'=>[
'Form'=>[
'fields'=>[
'username'=>'email',
'password'=>'password'
]
]
],
'loginRedirect'=>[
'controller'=>'Users',
'action'=>'index'
],
'loginAction'=>[
'controller'=>'Users',
'action'=>'login'
]
]);
$this->Auth->allow(['add']);
User.php [Entity] User.php [实体]
protected function _setPassword($password)
{
if (strlen($password) > 0) {
return (new DefaultPasswordHasher)->hash($password);
}
}
login.ctp login.ctp
<h1>Login</h1>
<?= $this->Form->create() ?>
<?= $this->Form->control('email') ?>
<?= $this->Form->control('password') ?>
<?= $this->Form->button('Login') ?>
<?= $this->Form->end() ?>
If do not work the above code for you, you can try adding this at your AppController 如果上述代码对您不起作用,您可以尝试在AppController中添加此代码
'fields'=>[ 'username'=>'email', 'password'=>'password' ],
'userModel' => 'Users'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.