Take back to login page after successfully reset his/her password in laravel 5.4

不羁岁月 提交于 2019-12-08 04:17:30

By default Laravel authenticates the user after they reset their password. So it's impossible to redirect to the login page since only guest users can view the login page. Even if you set the $redirectTo to /login, the guest middleware would redirect the user back to /home since the user is authenticated.

If you need to prevent the user from being automatically logged in on resetting password and redirect them to the login page, you need to follow these steps.

Do the following changes in ResetPasswordController located at app/Http/Controllers/Auth.

Change the redirect path to your login page.

protected $redirectTo = '/login';

Override the resetPassword method to prevent user being logged in. Add this to the controller.

protected function resetPassword($user, $password)
{
    $user->forceFill([
        'password' => bcrypt($password),
        'remember_token' => Str::random(60),
    ])->save();
}

Add this on top

use Illuminate\Support\Str;

The original method in ResetsPasswords trait looks like this.

protected function resetPassword($user, $password)
{
    $user->forceFill([
        'password' => bcrypt($password),
        'remember_token' => Str::random(60),
    ])->save();

    $this->guard()->login($user);
}

Edit : To send a custom response on redirection you can override sendResetResponse in your controller and add any custom session/flash messages. By default laravel sets its own response on successful password reset.

protected function sendResetResponse($response)
{
    return redirect($this->redirectPath())
                        ->with('status', trans($response));
}

When password successfully reset. It should auto set the session/cookie as logged on user. Try to unset the session/cookie and middleware will detect that no login is happening and you should redirected to /login page after successfully reset password.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!