NewPasswordController.php 2.2 KB

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