2

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 ?

user5300122
  • 143
  • 4
  • 13

3 Answers3

1

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");
peaceman
  • 1,681
  • 12
  • 19
0
  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

David Yew
  • 417
  • 5
  • 15
0

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.
 */
Ejaz Karim
  • 3,676
  • 6
  • 36
  • 49