始终不正确的用户名或密码.- Yii2

时间:2018-03-14 14:00:07

标签: php yii2 yii2-advanced-app

我已经安装了Yii2高级应用程序,我根据我的用户数据库自定义了注册。注册后我尝试登录并显示"Incorrect username or password",我的密码是qwerty,我已经多次检查过它仍然无效。

User Table

As you can see here, the user information after signing up it records the data to the user table.

注册模式

class SignupForm extends Model
{
public $username;
public $email;
public $password;
public $first_name;
public $middle_name;
public $last_name;
public $contact;
public $birth_date;
public $type;
public $external_type;
public $status;
public $region_id;
public $barangay_id;
public $province_id;
public $city_municipal_id;


/**
 * {@inheritdoc}
 */
public function rules()
{
    return [
        ['username', 'trim'],
        ['username', 'required'],
        ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
        ['username', 'string', 'min' => 2, 'max' => 255],

        ['email', 'trim'],
        ['email', 'required'],
        ['email', 'email'],
        ['email', 'string', 'max' => 255],
        ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],

        ['password', 'required'],
        ['password', 'string', 'min' => 6],

        ['first_name', 'required'],
        ['first_name', 'string', 'max' => 45],

        ['middle_name', 'string', 'max' => 45],

        ['last_name', 'required'],
        ['last_name', 'string', 'max' => 45],

        ['contact', 'required'],
        ['contact', 'string', 'max' => 11],

        ['birth_date', 'required'],

        ['type', 'required'],
        ['type', 'string', 'max' => 45],

        ['external_type', 'string', 'max' => 45],

        ['status', 'string', 'max' => 45],

        ['region_id', 'required'],
        ['barangay_id', 'required'],
        ['province_id', 'required'],
        ['city_municipal_id', 'required'],
    ];
}

/**
 * Signs user up.
 *
 * @return User|null the saved model or null if saving fails
 */
public function signup()
{
    if (!$this->validate()) {
        return null;
    }

    $user = new User();
    $user->username = $this->username;
    $user->email = $this->email;
    $user->setPassword($this->password);
    $user->generateAuthKey();

    $user->first_name = $this->first_name;
    $user->middle_name = $this->middle_name;
    $user->last_name = $this->last_name;
    $user->contact = $this->contact;
    $user->birth_date = $this->birth_date;
    $user->type = $this->type;
    $user->external_type = $this->external_type;
    $user->status = $this->status;
    $user->region_id = $this->region_id;
    $user->barangay_id = $this->barangay_id;
    $user->province_id = $this->province_id;
    $user->city_municipal_id = $this->city_municipal_id;

    return $user->save() ? $user : null;
}
}

我在哪里弄错了?令人困惑我认为我的代码没有错,因为我遵循了Yii2 Advanced App的正确安装设置。

登录模式

public function login()
{
    if ($this->validate()) {
        return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
    }

    return false;
}

3 个答案:

答案 0 :(得分:0)

虽然您没有为注册或注册添加操作,但是您从模型中调用$model->signup()函数的位置必须在if语句中进行检查,然后添加在里面打电话给\Yii::$app->user->login($userModel);,它会在注册后登录。

您的signup()函数在用户插入表格后返回用户模型对象。

见下面的代码示例

if(($userModel=$model->signUp())!==null){
   \Yii::$app->user->login($userModel);
}

希望它可以帮助你

答案 1 :(得分:0)

您是否尝试过设置用户模型类来实现身份界面?

首先声明你的课程

class MyUser extends ActiveRecord implements IdentityInterface

现在你班上的某个地方你需要实现这些方法

public static function findIdentity($id)
    {
        return self::findOne(['id'=>$id]);
    }

    public static function findIdentityByAccessToken($token, $type = null)
    {
        throw new NotSupportedException("Validation method not supported as of yet.");
    }

    public function getId()
    {
        return $this->id;
    }

    public function getAuthKey()
    {
        return $this->auth_key;
    }

    public function validateAuthKey($authKey)
    {
        return $this->auth_key === $authKey;
    }

接下来转到/config/web.php的components部分并搜索'user'组件

'user' => [
        'identityClass' => 'app\models\MyUser', //<---Your model class
        'enableAutoLogin' => true,
    ],

我认为你的好处就是去。如果这不起作用,请告诉我。

答案 2 :(得分:0)

在phpmyadmin中检查您的用户表,查看状态列,如果该值不等于10,则可以用10进行更改。