Amin Shafiee
Amin Shafiee

Reputation: 425

Automatic Logout after 5 minutes of inactive in yii2

May I know how to have function of automatic logout if users have inactive more than 5 minutes in yii2 ?

Upvotes: 0

Views: 5333

Answers (4)

Angelus Roman
Angelus Roman

Reputation: 191

Besides of setting up the main.php I have three suggestion to handle this situation.

  1. You should set you application in production mode ..

  2. customize the site/error.php to check if user is guest and if not display the div with message like "Session expires" and a link to "site/login".

  3. Alternatively, To redirect to login page when clicks on any link, define the access-control in a controller behaviors function and then you are done.

Upvotes: 0

Kalpesh Desai
Kalpesh Desai

Reputation: 1421

In your component configuration you need to add config in user component like this

'components'=>[
        'user' => [
            'class'=>'yii\web\User',
            'identityClass' => 'common\models\User',
            'loginUrl'=>['sign-in/login'],
            'enableAutoLogin' => false,
            'authTimeout'=>300,  //Number of second to Automatic Logout if inactive
            //this config is optional
            'identityCookie' => [
                'name' => '_backendUser', // unique for backend
                'path'=>'@backend/web'  // correct path for the backend app.
            ],
            'as afterLogin' => 'common\behaviors\LoginTimestampBehavior'
        ],
    ],

Upvotes: 0

Insane Skull
Insane Skull

Reputation: 9368

Try this configuration :

'user' => [
        'enableAutoLogin' => false,
        'authTimeout' => 300,
    ],

authTimeout

Upvotes: 6

AwesomeGuy
AwesomeGuy

Reputation: 1089

Your answer lies in configuration of "user" component in your config files.

Everything you need to know is in this documentation Yii2 User Component, set authTimout property to 300 (that's in seconds) and your user should be logged out after 5 minutes of inactivity.

Upvotes: 0

Related Questions