yii2登录重定向后丢失用户身份 [英] yii2 losing user identity after login redirect

查看:155
本文介绍了yii2登录重定向后丢失用户身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了其他类似的问题,而这个问题( Yii2页面重定向后用户身份丢失)提出了几乎相同的问题,没有适用于我的情况的解决方案.

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.

我创建了一个新的标识类,我已经实现了所有必需的方法并实现了IdentityInterface.请注意,它不会扩展ActiveRecord,而是扩展了我自己的,不是从AR派生的基本Model类.

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.

一切似乎都正常,我可以登录并进行正确的身份验证.我已经跟踪了代码,可以看到在调用IdentityInterface :: validatePassword()和User :: login()之后,在Yii :: $ app-> user中正确设置了我的身份类.

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.

但是,一旦登录成功并且用户被重定向,Yii :: $ app-> user包含一个空的IdentityInterface,而不是重定向之前的那个空接口.

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.

在SiteController :: actionLogin()中,我修改了以下代码:

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

return $this->goHome();

收件人:

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

并且我可以看到Yii :: $ app->用户在return()语句之前仍然具有正确的身份.

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

有人可以告诉我为什么我失去身份吗?我已经找到了所有可以想到的内容:yii \ web \ Controller,yii \ base \ Controller,yii \ web \ Response,SiteController,yii \ web \ User等...

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!

推荐答案

相同的问题,三天后找到解决方案,并且它可以正常工作. 我的用户模型是:

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

我更改:

public static function findIdentity($id) {

}

收件人:

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

对我有用.

这篇关于yii2登录重定向后丢失用户身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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