Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yii2 losing user identity after login redirect

i have looked at the other similar questions, and while this one (Yii2 user identity loss after page redirection) asks pretty much the same question, there is no solution that applies to my situation.

i have created a new identity class, i have implemented all the necessary methods and implemented the IdentityInterface. note that it does NOT extend ActiveRecord, but rather extends my own base Model class that does not derive from AR.

everything seems to work fine, i can log in and be correctly authenticated. i have traced the code and i can see that my identity class is correctly set in Yii::$app->user after IdentityInterface::validatePassword() and User::login() have been called.

however, once the login succeeds and the user is redirected, Yii::$app->user contains an empty IdentityInterface, rather than the one that was there immediately prior to the redirect.

in SiteController::actionLogin(), i have modified the code from:

return $this->goHome();

to:

$tmp = $this->goHome();
// echo '<pre>';var_dump(Yii::$app->user);echo '</pre>';exit;
return($tmp);

and i can see that Yii::$app->user still has the correct identity up until the return() statement.

can anyone tell me why i am losing my identity? i have traced through everything i can think of: yii\web\Controller, yii\base\Controller, yii\web\Response, SiteController, yii\web\User, etc...

any help at all would be greatly appreciated. thanks!

like image 292
Laz Avatar asked Dec 15 '22 17:12

Laz


1 Answers

Same problem, after 3 days find solution and I it working. My User model is:

namespace app\models;

use Yii;
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
    public $username;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'tbl_users';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'email', 'password'], 'required'],
            [['email'], 'unique'],
            [['role', 'status'], 'integer'],
            [['created', 'last_update'], 'safe'],
            [['name', 'email', 'password'], 'string', 'max' => 250]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'email' => 'Email',
            'password' => 'Password',
            'role' => 'Role',
            'status' => 'Status',
            'created' => 'Created',
            'last_update' => 'Last Update',
        ];
    }
    public function getAuthKey() {

    }

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

    public function validateAuthKey($authKey) {

    }

    public static function findIdentity($id) {

    }

    public static function findIdentityByAccessToken($token, $type = null) {

    }
    public static function findByEmail($email)
    {
        return static::findOne(array('email'=>$email));
    }

    public function validatePassword($password)
    {
        return $this->password === $password;
    }
}

I change:

public static function findIdentity($id) {

}

to:

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

It working for me.

like image 95
Nguyễn Thành Bồi Avatar answered Feb 03 '23 11:02

Nguyễn Thành Bồi