简体   繁体   中英

I can't save user identity in yii2

I created a module in yii2 under api/modules/v1 folder, the problem is when I request:

Yii::$app->user->login($user, 3600); 

The identity got saved temporary until I refresh the page or I request another url, When I reviewed the log file, I found this : User '1' logged in from ::1. Session not enabled. Also I tried to add :

'enableAutoLogin' => true,
'enableSession' => true,

user component configuration :

'user' => [
        'identityClass' => 'common\models\User',
        'enableAutoLogin' => true,
        'enableSession' => true,
        'identityCookie' => [
            'name' => '_APIUser', // unique for backend
            'path' => '/api/web/v1'  // correct path for the backend app.
        ]
    ],
    'session' => [
        'name' => '_apiSessionId', // unique for frontend
        'savePath' => __DIR__ . '/../runtime', // a temporary folder on frontend
    ],

But the same problem occurred.

Have you model common\\models\\User implements "login" method? If so, login with this code

Yii::$app->user->identity->login($user, 3600); 

it seems like you are configuring 'common\\config\\main' as you have indicated frontend and backend session details in the same config.

Leave the common\\config\\main blank and you have to configure your backend\\config\\main and frontend\\config\\main separately. Your login code should work.

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