How can I achieve auto login after registration in yii2? In yii1, we achieved this through user Identity, but now I couldn't find it.
MY controller
public function actionCreate()
{
$model = new User();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash('success', 'Please Login with Email/Password!');
return $this->redirect('../site/login');
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
If registration is successful, I want to auto login instead of going site login.
if ($model->load(Yii::$app->request->post()) && $model->save()) {
\Yii::$app->user->login($model);
return $this->redirect(['/site/index']);
}
You can accomplish that with switchIdentity() method.
Example:
if ($userModel->load(Yii::$app->request->post()) && $userModel->save()) {
Yii::$app->user->switchIdentity($userModel); // log in
// do your stuff
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.