[英]after make or change password User dont login in Yii2
after make or edit password User i have login error 'Incorrect username or password' in Yii2 制作或编辑密码后用户我在Yii2中有登录错误'用户名或密码不正确'
login with user 'admin' is work ( i make admin user with actionCreate1) 使用用户'admin'登录是可行的(我使用actionCreate1创建管理员用户)
auth_key: kYm0pvYAXY4IzuV7eYgGgtjSqoxxMNUL
password: $2y$13$QqsbMW3ErXwWOPad3abDYOPzh5XLwuEvQKBhZGEEDoT0Av5l0bE2S
but i make user or edit password , in login page i have error: 'Incorrect username or password' 但我使用户或编辑密码,在登录页面我有错误:'用户名或密码不正确'
i think problem from beforeSave in Model USER 我认为来自模型USER中的beforeSave的问题
User Table: 用户表:
id int
auth_key text
username text
password text
actionCreate: it's not work actionCreate: 它不起作用
public function actionCreate()
{
$model = new User();
if ($model->load(Yii::$app->request->post())) {
if($model->save())
{
$model->img = UploadedFile::getInstance($model, 'img');
if($model->img !== null) $model->upload('img',$model->id);
$model->save();
}
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
actionCreate1: it's GOOD WORK actionCreate1: 这很好用
public function actionCreate()
{
$model = new User();
if ($model->load(Yii::$app->request->post())) {
$model->username = Yii::$app->request->post('User')['username'];
$model->password = Yii::$app->request->post('User')['password'];
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
Model User: 型号用户:
public function beforeSave($insert)
{
if($this->password) {
$this->setPassword($this->password);
}
if($insert)
{
$this->generateAuthKey();
}
return parent::beforeSave($insert);
}
public function setPassword($password) {
$this->password = Yii::$app->security->generatePasswordHash($password);
}
public function generateAuthKey() {
$this->auth_key = Yii::$app->security->generateRandomString();
}
/**
* @param $password
* @return bool
*/
public function validatePassword($password) {
return Yii::$app->security->validatePassword($password, $this->password);
}
/**
* @inheritdoc
*/
public static function findIdentity($id)
{
return static::findOne([
'id' => $id
]);
}
public static function findIdentityByAccessToken($token, $type = null) {}
/**
* @param $username
* @return null|static
*/
public static function findByUsername($username)
{
return static::findOne([
'username' => $username,
]);
}
/**
* @inheritdoc
*/
public function getId()
{
return $this->id;
}
/**
* @inheritdoc
*/
public function getAuthKey()
{
return $this->auth_key;
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return $this->auth_key === $authKey;
}
You are using the same name for attribute storing password hash and raw password. 您使用相同的名称存储密码哈希和原始密码的属性。
Either change password hash attribute to something like passwod_hash
or raw password attribute to something like raw_password
and modify the code accordingly to handle it. 将密码哈希属性更改为passwod_hash
或raw password属性,如raw_password
并相应地修改代码以处理它。
for fix this problem. 解决这个问题。
step1: change before save Model USER step1:保存模型USER之前更改
public function beforeSave($insert)
{
if($insert)
{
if($this->password) {
$this->setPassword($this->password);
}
$this->generateAuthKey();
}
return parent::beforeSave($insert);
}
step2: small change in update user step2:更新用户的小变化
In the first code
$password = $model->password;
if(Yii::$app->request->post('User')['password'] != null) {
$model->password = Yii::$app->security->generatePasswordHash(Yii::$app->request->post('User')['password']);
}
else
{
$model->password = $password;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.