You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.3 KiB

  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Auth\Events\PasswordReset;
  5. use Illuminate\Http\RedirectResponse;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Hash;
  8. use Illuminate\Support\Facades\Password;
  9. use Illuminate\Support\Str;
  10. use Illuminate\Validation\Rules;
  11. use Illuminate\Validation\ValidationException;
  12. use Inertia\Inertia;
  13. use Inertia\Response;
  14. class NewPasswordController extends Controller
  15. {
  16. /**
  17. * Display the password reset view.
  18. */
  19. public function create(Request $request): Response
  20. {
  21. return Inertia::render('Auth/ResetPassword', [
  22. 'email' => $request->email,
  23. 'token' => $request->route('token'),
  24. ]);
  25. }
  26. /**
  27. * Handle an incoming new password request.
  28. *
  29. * @throws \Illuminate\Validation\ValidationException
  30. */
  31. public function store(Request $request): RedirectResponse
  32. {
  33. $request->validate([
  34. 'token' => 'required',
  35. 'email' => 'required|email',
  36. 'password' => ['required', 'confirmed', Rules\Password::defaults()],
  37. ]);
  38. // Here we will attempt to reset the user's password. If it is successful we
  39. // will update the password on an actual user model and persist it to the
  40. // database. Otherwise we will parse the error and return the response.
  41. $status = Password::reset(
  42. $request->only('email', 'password', 'password_confirmation', 'token'),
  43. function ($user) use ($request) {
  44. $user->forceFill([
  45. 'password' => Hash::make($request->password),
  46. 'remember_token' => Str::random(60),
  47. ])->save();
  48. event(new PasswordReset($user));
  49. }
  50. );
  51. // If the password was successfully reset, we will redirect the user back to
  52. // the application's home authenticated view. If there is an error we can
  53. // redirect them back to where they came from with their error message.
  54. if ($status == Password::PASSWORD_RESET) {
  55. return redirect()->route('login')->with('status', __($status));
  56. }
  57. throw ValidationException::withMessages([
  58. 'email' => [trans($status)],
  59. ]);
  60. }
  61. }