update-password-form.blade.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. use Illuminate\Support\Facades\Auth;
  3. use Illuminate\Support\Facades\Hash;
  4. use Illuminate\Validation\Rules\Password;
  5. use Illuminate\Validation\ValidationException;
  6. use Livewire\Volt\Component;
  7. new class extends Component
  8. {
  9. public string $current_password = '';
  10. public string $password = '';
  11. public string $password_confirmation = '';
  12. /**
  13. * Update the password for the currently authenticated user.
  14. */
  15. public function updatePassword(): void
  16. {
  17. try {
  18. $validated = $this->validate([
  19. 'current_password' => ['required', 'string', 'current_password'],
  20. 'password' => ['required', 'string', Password::defaults(), 'confirmed'],
  21. ]);
  22. } catch (ValidationException $e) {
  23. $this->reset('current_password', 'password', 'password_confirmation');
  24. throw $e;
  25. }
  26. Auth::user()->update([
  27. 'password' => Hash::make($validated['password']),
  28. ]);
  29. $this->reset('current_password', 'password', 'password_confirmation');
  30. $this->dispatch('password-updated');
  31. }
  32. }; ?>
  33. <section>
  34. <header>
  35. <h2 class="text-lg font-medium text-gray-900 dark:text-gray-100">
  36. {{ __('Update Password') }}
  37. </h2>
  38. <p class="mt-1 text-sm text-gray-600 dark:text-gray-400">
  39. {{ __('Ensure your account is using a long, random password to stay secure.') }}
  40. </p>
  41. </header>
  42. <form wire:submit="updatePassword" class="mt-6 space-y-6">
  43. <div>
  44. <x-input-label for="update_password_current_password" :value="__('Current Password')" />
  45. <x-text-input wire:model="current_password" id="update_password_current_password" name="current_password" type="password" class="mt-1 block w-full" autocomplete="current-password" />
  46. <x-input-error :messages="$errors->get('current_password')" class="mt-2" />
  47. </div>
  48. <div>
  49. <x-input-label for="update_password_password" :value="__('New Password')" />
  50. <x-text-input wire:model="password" id="update_password_password" name="password" type="password" class="mt-1 block w-full" autocomplete="new-password" />
  51. <x-input-error :messages="$errors->get('password')" class="mt-2" />
  52. </div>
  53. <div>
  54. <x-input-label for="update_password_password_confirmation" :value="__('Confirm Password')" />
  55. <x-text-input wire:model="password_confirmation" id="update_password_password_confirmation" name="password_confirmation" type="password" class="mt-1 block w-full" autocomplete="new-password" />
  56. <x-input-error :messages="$errors->get('password_confirmation')" class="mt-2" />
  57. </div>
  58. <div class="flex items-center gap-4">
  59. <x-primary-button>{{ __('Save') }}</x-primary-button>
  60. <x-action-message class="me-3" on="password-updated">
  61. {{ __('Saved.') }}
  62. </x-action-message>
  63. </div>
  64. </form>
  65. </section>