Based on Ngo's answer, I figured out a method that works well and is easier to configure.
1) Add the "last_session_id" field to the user table.
2) Add the following to the main controller:
public function afterAction($action, $result) { $result = parent::afterAction($action, $result); if(Yii::$app->user->id) { //update the user table with last_session_id $user = User::find()->where(['id' => Yii::$app->user->id])->one(); $user->last_session_id = Yii::$app->session->id; $user->save(false); } return $result; }
3) Change the site / login action as follows:
public function actionLogin() { if (!\Yii::$app->user->isGuest) { return $this->goHome(); } $model = new LoginForm(); if ($model->load(Yii::$app->request->post()) && $model->login()) { //delete previous session ID and change auth key Yii::$app->session->destroySession(Yii::$app->user->identity->last_session_id); $u = \common\models\User::find()->where(['id' => Yii::$app->user->id])->one(); $u->auth_key = Yii::$app->security->generateRandomString(); $u->save(false); return $this->goBack(); } else { return $this->render('/site/login', [ 'model' => $model, ]); } }
source share