I am new to Yii2. I'm using Yii 2 basic template.
I have the "remember me" functionality implemented on my web application, but it is not working as I think it was supposed to. I can successfully log in (with a checked "remember me" checkbox). But after closing the browser and opening the website again I am not logged in, but instead redirected to the login page.
I have set enableAutoLogin
to true
in the config file
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
'authTimeout' => 86400,
],
Using "Remember Me" in the login :
1- Create your identity class using this link : http://www.yiiframework.com/doc-2.0/guide-security-authentication.html
2- Add this line to the config file :
...
...
3- Use this code after you validate login form :
1. Do this in your user (if using any other model for login then use that model) model.
add this just before
rules
public $rememberMe = true;
and define in your model
2. now do this in your view page
Make sure that your user model have implemented
yii\web\IdentityInterface
and it has the following methodsvalidateAuthKey()
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
Refer docs for more about auto login
what i have done is in models:
and in view page retrive it using :
and my problem is solved :)