route()->parameter('token'); return view('auth.passwords.reset')->with( ['token' => $token, 'email' => $request->email] ); } /** * Reset the given user's password. * * @param Request $request * @return RedirectResponse|JsonResponse */ public function reset(Request $request): JsonResponse|RedirectResponse { $request->validate($this->rules(), $this->validationErrorMessages()); // 如果当前用户已登录,那么将 email 设置为当前用户的 email // if (Auth::guard('web')->check()) { // $request->merge(['email' => Auth::user()->email]); // } // Here we will attempt to reset the user's password. If it is successful we // will update the password on an actual user model and persist it to the // database. Otherwise, we will parse the error and return the response. $response = $this->broker()->reset( $this->credentials($request), function ($user, $password) { $this->resetPassword($user, $password); } ); // If the password was successfully reset, we will redirect the user back to // the application's home authenticated view. If there is an error we can // redirect them back to where they came from with their error message. return $response == Password::PASSWORD_RESET ? $this->sendResetResponse($request, $response) : $this->sendResetFailedResponse($request, $response); } /** * Get the password reset validation rules. * * @return array */ protected function rules(): array { return [ 'token' => 'required', 'email' => 'required|email', 'password' => ['required', 'confirmed', Rules\Password::defaults()], ]; } /** * Get the password reset validation error messages. * * @return array */ protected function validationErrorMessages(): array { return []; } /** * Get the broker to be used during password reset. * * @return PasswordBroker */ public function broker(): PasswordBroker { return Password::broker(); } /** * Get the password reset credentials from the request. * * @param Request $request * @return array */ protected function credentials(Request $request): array { return $request->only( 'email', 'password', 'password_confirmation', 'token' ); } /** * Reset the given user's password. * * @param CanResetPassword|User $user * @param string $password * @return void */ protected function resetPassword(CanResetPassword|User $user, string $password): void { $this->setUserPassword($user, $password); $user->setRememberToken(Str::random(60)); $user->save(); event(new PasswordReset($user)); $this->guard()->login($user); } /** * Set the user's password. * * @param CanResetPassword $user * @param string $password * @return void */ protected function setUserPassword(CanResetPassword $user, string $password): void { // if it has password field if (isset($user->password)) { $user->password = Hash::make($password); } } /** * Get the guard to be used during password reset. * * @return StatefulGuard */ protected function guard(): StatefulGuard { return Auth::guard(); } /** * Get the response for a successful password reset. * * @param Request $request * @param string $response * @return RedirectResponse|JsonResponse */ protected function sendResetResponse(Request $request, string $response): JsonResponse|RedirectResponse { if ($request->wantsJson()) { return new JsonResponse(['message' => trans($response)], 200); } return redirect($this->redirectPath()) ->with('status', trans($response)); } /** * Get the response for a failed password reset. * * @param Request $request * @param string $response * @return RedirectResponse */ protected function sendResetFailedResponse(Request $request, string $response): RedirectResponse { if ($request->wantsJson()) { throw ValidationException::withMessages([ 'email' => [trans($response)], ]); } return redirect()->back() ->withInput($request->only('email')) ->withErrors(['email' => trans($response)]); } }