middleware('guest'); } /** * Display the password reset form for the given token. */ public function showResetForm(Request $request, ?string $token = null): InertiaResponse { return Inertia::render('auth/ResetPassword', [ 'token' => $token, 'email' => $request->email, ]); } /** * Reset the given user's password. */ public function reset(Request $request): RedirectResponse|JsonResponse { $request->validate([ 'token' => 'required', 'email' => 'required|email', 'password' => ['required', 'confirmed', Rules\Password::defaults()], ]); $response = $this->broker()->reset( $request->only('email', 'password', 'password_confirmation', 'token'), fn (CanResetPassword $user, string $password) => $this->resetPassword($user, $password) ); return $response === Password::PASSWORD_RESET ? $this->sendResetResponse($request, $response) : $this->sendResetFailedResponse($request, $response); } /** * Reset the given user's password. * * The password is assigned unhashed because the User model hashes it. */ protected function resetPassword(CanResetPassword $user, string $password): void { $user->password = $password; $user->setRememberToken(Str::random(60)); $user->save(); event(new PasswordReset($user)); $this->guard()->login($user); } /** * Get the response for a successful password reset. */ protected function sendResetResponse(Request $request, string $response): RedirectResponse|JsonResponse { if ($request->wantsJson()) { return new JsonResponse(['message' => trans($response)], 200); } return redirect($this->redirectTo)->with('status', trans($response)); } /** * Get the response for a failed password reset. * * @throws ValidationException */ protected function sendResetFailedResponse(Request $request, string $response): RedirectResponse { if ($request->wantsJson()) { throw ValidationException::withMessages(['email' => [trans($response)]]); } return back() ->withInput($request->only('email')) ->withErrors(['email' => trans($response)]); } /** * Get the broker to be used during password reset. */ public function broker(): PasswordBroker { return Password::broker(); } /** * Get the guard to be used during password reset. */ protected function guard(): StatefulGuard { return Auth::guard(); } }