Bakayaro
Bakayaro

Reputation: 130

Yii2 Advanced app, different sessions for frontend and backend



I'm trying to set up a Yii2 advanced project. For this I used kartik-v's advanced app template. It works fine but (as He just mentioned here) if you log into the frontend and then go to backend you'll be logged in as well. So I would like to separate the frontend and backend logins (different sessions). I tried to configure the identity cookies but It didn't work.
In the comments I found this: "Still, when either frontend or backend is signed in and we open the other, it shows automatically signed in because the session cookie is same, PHPSESSID." So I changed the name and the savePath of the sessions in the config of frontend and the backend. With this it should work, but It doesnt.
I got an 500 internal server error every time I go to my page. And if I try to log in, it just doesnt work, it redirects me but does not log me in. I found out that If I dont set the savePath I dont get the error but still nothing happens. And If check in the 'remember me' option I get the error message but the login works.. So I dont know what to do now. My main config files:


backend:

'components' => [
    'session' => [
        'name' => 'backend_sessid',
        'savePath' => __DIR__ . '/../tmp',
    ],
    'user' => [
        'identityClass' => 'common\models\User',
        'enableAutoLogin' => true,
        'identityCookie' => [
            'name' => '_backendUser',
            'path' => '/projectname/backend/web'
        ]
    ],

frontend:

'components' => [
    'session' => [
        'name' => 'frontend_sessid',
        'savePath' => __DIR__ . '/../tmp',
    ],
    'user' => [
        'identityClass' => 'common\models\User',
        'enableAutoLogin' => true,
        'identityCookie' => [
            'name' => '_frontendUser',
            'path' => '/projectname'
        ]
    ],

Upvotes: 0

Views: 1203

Answers (1)

brunoapimentel
brunoapimentel

Reputation: 76

One approach would be to use the Role Based Access Control described here: http://www.yiiframework.com/doc-2.0/guide-security-authorization.html

This way, you would set different roles for frontend users and backend users. If a user with different privileges tried to access the wrong site area, you could log him out and redirect him to the login page.

Upvotes: 1

Related Questions