reset-password.blade.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. use Illuminate\Auth\Events\PasswordReset;
  3. use Illuminate\Support\Facades\Hash;
  4. use Illuminate\Support\Facades\Password;
  5. use Illuminate\Support\Facades\Session;
  6. use Illuminate\Support\Str;
  7. use Illuminate\Validation\Rules;
  8. use Livewire\Attributes\Layout;
  9. use Livewire\Attributes\Locked;
  10. use Livewire\Volt\Component;
  11. new #[Layout('layouts.guest')] class extends Component
  12. {
  13. #[Locked]
  14. public string $token = '';
  15. public string $email = '';
  16. public string $password = '';
  17. public string $password_confirmation = '';
  18. /**
  19. * Mount the component.
  20. */
  21. public function mount(string $token): void
  22. {
  23. $this->token = $token;
  24. $this->email = request()->string('email');
  25. }
  26. /**
  27. * Reset the password for the given user.
  28. */
  29. public function resetPassword(): void
  30. {
  31. $this->validate([
  32. 'token' => ['required'],
  33. 'email' => ['required', 'string', 'email'],
  34. 'password' => ['required', 'string', 'confirmed', Rules\Password::defaults()],
  35. ]);
  36. // Here we will attempt to reset the user's password. If it is successful we
  37. // will update the password on an actual user model and persist it to the
  38. // database. Otherwise we will parse the error and return the response.
  39. $status = Password::reset(
  40. $this->only('email', 'password', 'password_confirmation', 'token'),
  41. function ($user) {
  42. $user->forceFill([
  43. 'password' => Hash::make($this->password),
  44. 'remember_token' => Str::random(60),
  45. ])->save();
  46. event(new PasswordReset($user));
  47. }
  48. );
  49. // If the password was successfully reset, we will redirect the user back to
  50. // the application's home authenticated view. If there is an error we can
  51. // redirect them back to where they came from with their error message.
  52. if ($status != Password::PASSWORD_RESET) {
  53. $this->addError('email', __($status));
  54. return;
  55. }
  56. Session::flash('status', __($status));
  57. $this->redirectRoute('login', navigate: true);
  58. }
  59. }; ?>
  60. <div>
  61. <form wire:submit="resetPassword">
  62. <!-- Email Address -->
  63. <div>
  64. <x-input-label for="email" :value="__('Email')" />
  65. <x-text-input wire:model="email" id="email" class="block mt-1 w-full" type="email" name="email" required autofocus autocomplete="username" />
  66. <x-input-error :messages="$errors->get('email')" class="mt-2" />
  67. </div>
  68. <!-- Password -->
  69. <div class="mt-4">
  70. <x-input-label for="password" :value="__('Password')" />
  71. <x-text-input wire:model="password" id="password" class="block mt-1 w-full" type="password" name="password" required autocomplete="new-password" />
  72. <x-input-error :messages="$errors->get('password')" class="mt-2" />
  73. </div>
  74. <!-- Confirm Password -->
  75. <div class="mt-4">
  76. <x-input-label for="password_confirmation" :value="__('Confirm Password')" />
  77. <x-text-input wire:model="password_confirmation" id="password_confirmation" class="block mt-1 w-full"
  78. type="password"
  79. name="password_confirmation" required autocomplete="new-password" />
  80. <x-input-error :messages="$errors->get('password_confirmation')" class="mt-2" />
  81. </div>
  82. <div class="flex items-center justify-end mt-4">
  83. <x-primary-button>
  84. {{ __('Reset Password') }}
  85. </x-primary-button>
  86. </div>
  87. </form>
  88. </div>