简体   繁体   中英

Facing issue with identity object after login while using Multiple User Identity Class in Yii2 Advanced

I have setup/created 2 user identity classes for 2 different login under config/main.php components:

'user' => [
            'class'=>'yii\web\User',
            'identityClass' => 'frontend\models\CustomerUser',
            'enableAutoLogin' => false,
            'authTimeout' => 60*30,
            'loginUrl' => ['customer/login'],
            'identityCookie' => [
                'name' => '_panelCustomer',
                'httpOnly' => true,
            ],
        ],
        'franchise'=>[
            'class'=>'yii\web\Franchise',
            'identityClass' => 'frontend\models\FranchiseUser',
            'enableAutoLogin' => false,
            'authTimeout' => 60*30,
            'loginUrl' => ['franchise/login'],
            'identityCookie' => [
                'name' => '_panelFranchise',
                'httpOnly' => true,
            ],
        ],

When i logged in using franchise, after login if i check Yii::$app->user->identity it gives me details for 1st record in database (vice versa for user login). I want to get null for Yii::$app->user->identity when i logged in as franchise.

You select 1'st components of the user, Check with this:

$user = Yii::$app->get('franchise');
$user->identity

But, The best solution for this purpose using an advanced template with a separated configuration for users.

https://github.com/yiisoft/yii2-app-advanced

Or you can use module and change configuration in runtime, Inside of Module.php:

public function init() {
    parent::init();
    Yii::$app->setComponents([
        'user' => [
            'class'=>'yii\web\Franchise',
            'identityClass' => 'frontend\models\FranchiseUser',
            'enableAutoLogin' => false,
            'authTimeout' => 60*30,
            'loginUrl' => ['franchise/login'],
            'identityCookie' => [
                'name' => '_panelFranchise',
                'httpOnly' => true,
            ],
        ],
    ]);
}

And repeat this for another user module.

When we add multiple identity into configuration, please change its idParam parameter.

'user' => [
            'class'=>'yii\web\User',
            'identityClass' => 'frontend\models\CustomerUser',
            'enableAutoLogin' => false,
            'authTimeout' => 60*30,
            'loginUrl' => ['customer/login'],
            'idParam' => '__cid',
            'identityCookie' => [
                'name' => '_panelCustomer',
                'httpOnly' => true,
            ],
        ],
        'franchise' => [
            'class'=>'yii\web\User',
            'identityClass' => 'frontend\models\FranchiseUser',
            'enableAutoLogin' => false,
            'authTimeout' => 60*30,
            'loginUrl' => ['franchise/login'],
            'idParam' => '__fid',
            'identityCookie' => [
                'name' => '_panelFranchise',
                'httpOnly' => true,
            ],
        ],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM