Andris
Andris

Reputation: 175

Separate users in Yii2's basic template

I am working with Yii2 and just can't separate users. My app has two modules.

This is my config from web.php:

'frontendUser' => [
      'class' => 'yii\web\User',
      'identityClass' => 'app\models\User',
      'enableAutoLogin' => false,
      'loginUrl' => ['frontend/default/index'],
],
'user' => [
      'identityClass' => 'app\models\Owner',
      'enableAutoLogin' => false,
      'loginUrl' => ['arena/default/login'],
],

The problem is when I log in with one of the above, both the Yii::$app->user->isGuest and Yii::$app->frontendUser->isGuest returns true, and became logged in.

I found solutions only for the advanced template...

Thanks,

Upvotes: 3

Views: 171

Answers (1)

Blizz
Blizz

Reputation: 8408

You'll need to configure somewhat more than what you did.

As it is now, they are both saving in the same variables in your session and they are both using the same cookie.

'frontendUser' => [
  'class' => 'yii\web\User',
  'identityClass' => 'app\models\User',
  'enableAutoLogin' => false,
  'loginUrl' => ['frontend/default/index'],
  'identityCookie' = ['name' => '_feIdentity', 'httpOnly' => true], // THIS
  'idParam' => '__feId', // THIS
  'authTimeoutParam' => '__feExpire', // THIS, only if you want to keep separate expiry times
], 
'user' => [
  'identityClass' => 'app\models\Owner',
  'enableAutoLogin' => false,
  'loginUrl' => ['arena/default/login'],
],

This directs the frontend user to use different variables to store/retrieve data from, effectively separating it into another user.

Take a look at the yii\web\User documentation for an explanation.

Upvotes: 3

Related Questions