更改密码后,您不需要重新登录用户。
您只需要根据当前类似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);
}
}