设置或更改密码后用户不登录 Yii2 [英] after make or change password User dont login in Yii2

查看:41
本文介绍了设置或更改密码后用户不登录 Yii2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建或编辑密码用户后,我在 Yii2 中出现登录错误用户名或密码不正确"

after make or edit password User i have login error 'Incorrect username or password' in Yii2

使用用户admin"登录是可行的(我使用 actionCreate1 创建管理员用户)

login with user 'admin' is work ( i make admin user with 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'

我认为之前保存在模型用户中的问题

用户表:

id                  int          
auth_key            text          
username            text           
password            text          

actionCreate:它不起作用

actionCreate: it's not work

    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:​​效果很好

actionCreate1: it's GOOD WORK

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,
            ]);
        }
    }

模型用户:

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;
}

推荐答案

用于解决此问题.

第一步:保存模型用户前更改

step1: change before save Model USER

public function beforeSave($insert)
    {
        if($insert)
        {
            if($this->password) {
                $this->setPassword($this->password);
            }

            $this->generateAuthKey();
        }

        return parent::beforeSave($insert);
    }

步骤2:更新用户的小变化

step2: small change in update user

    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;
}

这篇关于设置或更改密码后用户不登录 Yii2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆