【问题标题】:Stop user being logged out when updating the password更新密码时停止用户退出
【发布时间】:2021-02-28 18:15:59
【问题描述】:

我正在添加一些实现,允许用户在管理面板中更新密码,前提是他们提供了正确的当前密码。

我面临的问题是更新密码时的使用被注销,因为password_hash 在会话中不再匹配,我知道这一点,因为我已经注释掉了中间件AuthenticateSession

我可以看到这里有一个password_hash的会话值检查,带有默认驱动程序的后缀。

if ($request->session()->get('password_hash_'.$this->auth->getDefaultDriver()) !== $request->user()->getAuthPassword()) {
    $this->logout($request);
}

似乎我总是打这个,尽管在更新密码时我将会话变量设置为用户的新密码。

$user->password = Hash::make($request->input('password'));
$user->save();

auth()->guard('web')->login($user, true);
$request->session()->put([
    'password_hash_' . auth()->getDefaultDriver() =>  $user->getAuthPassword(),
]);

即便如此,我还是使用了注销方法,显然我不想删除中间件,因为它在帐户被盗时很有用。

【问题讨论】:

    标签: laravel authentication jetstream


    【解决方案1】:

    您不需要在更改密码后重新登录用户。

    你只需要检查当前的请求密码,比如Hash::check($request->current_password, $this->User->password)或类似的方法

    你可以随时查看 Illuminate\Foundation\Auth\ResetsPasswords 类来了解 laravel 是如何实现它的

    这是一个检查当前密码的更新密码方法示例

        /**
         * Update the specified resource in storage.
         *
         * @param \Illuminate\Http\Request $request
         * @param \App\Models\User $user
         * @return \Illuminate\Http\JsonResponse
         */
        public function updatePassword(Request $request)
        {
            if (!(Hash::check($request->current_password, $this->User->password))) {
                return response()->json([
                    'status'  => 'error',
                    'message' => 'Your current password is incorrect. Please try again.'
                ], 403);
            }
    
            if (strcmp($request->current_password, $request->new_password) == 0) {
                return response()->json([
                    'status'  => 'error',
                    'message' => 'New Password cannot be the same as your current password. Please choose a different password.'
                ], 403);
            }
    
            $validatedData = $request->validate([
                'current_password' => 'required',
                'new_password'     => 'required|strong_password|string|min:6|confirmed',
            ]);
    
            try {
                $this->User->password = bcrypt($request->new_password);
                $this->User->save();
    
                return response()->json([
                    'status'  => 'success',
                    'message' => 'Your password has been updated',
                ], 200);
            } catch (\Exception $e) {
                return response()->json([
                    'status'  => 'error',
                    'message' => $e->getMessage()
                ], 500);
            }
        }
    

    【讨论】:

    • 我认为是这种情况,但是我使用的是 Laravel Jetstream 和惯性,我认为这是默认行为。如果你看一下Illuminate\Session\Middleware\AuthenticateSession,你会看到我上面提到的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2017-07-18
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    相关资源
    最近更新 更多