user5300122
user5300122

Reputation: 143

how to log out user automatically in yii2 after user is idle for a fixed seconds

I am using Yii2, I want to user logout automatically and redirect to login page, after fixed idle seconds.

I already tried

 'components' => [
    'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
             'authTimeout' => 60,
        ]
    ],

What should I do ?

Upvotes: 2

Views: 2899

Answers (3)

Ejaz Karim
Ejaz Karim

Reputation: 3696

Don't set enableAutoLogin to true. Use the following configuration:

...
'user' => [
        'identityClass' => 'backend\models\Users',
        'enableSession' => true,
        'authTimeout' => 300,
   ],
....
// below is documentation from source code for authTimeout

/**
 * @var integer the number of seconds in which the user will be logged out automatically if he
 * remains inactive. If this property is not set, the user will be logged out after
 * the current session expires (c.f. [[Session::timeout]]).
 * Note that this will not work if [[enableAutoLogin]] is true.
 */

Upvotes: 0

peaceman
peaceman

Reputation: 1721

You can set up JavaScript function to run every minute or so that makes ajax call and check if session for current user is expired. Then you can redirect with JavaScript with:

window.location("example.com/login");

Upvotes: 1

David Yew
David Yew

Reputation: 417

  1. Did you have a 'loginUrl' property for your 'user'? See http://www.yiiframework.com/doc-2.0/yii-web-user.html

    'user' =>[ 'loginUrl'=>['site/login']]

  2. Does your Controller have rules for the actions that required authenticated users? See http://www.yiiframework.com/doc-2.0/guide-security-authorization.html

Upvotes: 0

Related Questions