且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在laravel 5.4中成功重置密码后,返回登录页面

更新时间:2023-09-28 17:33:40

默认情况下,Laravel在重置密码后对用户进行身份验证.因此,不可能重定向到登录页面,因为只有来宾用户才能查看登录页面.即使将$redirectTo设置为/login,来宾中间件也会将用户重定向回/home,因为该用户已通过身份验证.

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.

在位于app/Http/Controllers/AuthResetPasswordController中进行以下更改.

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

将重定向路径更改为您的登录页面.

Change the redirect path to your login page.

protected $redirectTo = '/login';

重写resetPassword方法以防止用户登录.将其添加到控制器.

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

将此添加到顶部

use Illuminate\Support\Str;


具有ResetsPasswords特性的原始方法如下所示.


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

编辑:要发送重定向时的自定义响应,您可以在控制器中覆盖sendResetResponse并添加任何自定义会话/Flash消息.默认情况下,laravel会在成功重置密码后设置自己的响应.

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