I am using below code to auto logout after some time interval
'session' => [
'timeout' => 10,
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => false,
'authTimeout' => 10,
],
It logged out successfully
but did not redirect to login page
how to do that?
You can use behaviors for actions.
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['index', 'logout','view','create','update','delete'],
'rules' => [
[
'actions' => ['index', 'logout','view','create','update','delete'],
'allow' => true,
'roles' => ['@'],
],
],
],
];
}
So user is not logged in it will redirect to login url. You can also set login url
'session' => [
'timeout' => 10,
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => false,
'authTimeout' => 10,
'loginUrl' => 'admin/default/login' // set your login path here
],
OR You can use beforeAction method to check that user is logged in or not and send user to login page.
public function beforeAction($action){
if (Yii::$app->user->isGuest){
return $this->redirect(['site/login'])->send(); // login path
}
}