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

前端 未结 2 741
迷失自我
迷失自我 2021-01-15 02:01

I want to redirect to login page whenever a user successfully reset his/her password in laravel 5.4.

Currently it redirects to dashboard page after successfully rese

2条回答
  •  梦毁少年i
    2021-01-15 02:34

    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));
    }
    

提交回复
热议问题