update-profile-information-form.blade.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. use App\Models\User;
  3. use Illuminate\Support\Facades\Auth;
  4. use Illuminate\Support\Facades\Session;
  5. use Illuminate\Validation\Rule;
  6. use Livewire\Volt\Component;
  7. new class extends Component
  8. {
  9. public string $name = '';
  10. public string $email = '';
  11. /**
  12. * Mount the component.
  13. */
  14. public function mount(): void
  15. {
  16. $this->name = Auth::user()->name;
  17. $this->email = Auth::user()->email;
  18. }
  19. /**
  20. * Update the profile information for the currently authenticated user.
  21. */
  22. public function updateProfileInformation(): void
  23. {
  24. $user = Auth::user();
  25. $validated = $this->validate([
  26. 'name' => ['required', 'string', 'max:255'],
  27. 'email' => ['required', 'string', 'lowercase', 'email', 'max:255', Rule::unique(User::class)->ignore($user->id)],
  28. ]);
  29. $user->fill($validated);
  30. if ($user->isDirty('email')) {
  31. $user->email_verified_at = null;
  32. }
  33. $user->save();
  34. $this->dispatch('profile-updated', name: $user->name);
  35. }
  36. /**
  37. * Send an email verification notification to the current user.
  38. */
  39. public function sendVerification(): void
  40. {
  41. $user = Auth::user();
  42. if ($user->hasVerifiedEmail()) {
  43. $this->redirectIntended(default: route('dashboard', absolute: false));
  44. return;
  45. }
  46. $user->sendEmailVerificationNotification();
  47. Session::flash('status', 'verification-link-sent');
  48. }
  49. }; ?>
  50. <section>
  51. <header>
  52. <h2 class="text-lg font-medium text-gray-900 dark:text-gray-100">
  53. {{ __('Profile Information') }}
  54. </h2>
  55. <p class="mt-1 text-sm text-gray-600 dark:text-gray-400">
  56. {{ __("Update your account's profile information and email address.") }}
  57. </p>
  58. </header>
  59. <form wire:submit="updateProfileInformation" class="mt-6 space-y-6">
  60. <div>
  61. <x-input-label for="name" :value="__('Name')" />
  62. <x-text-input wire:model="name" id="name" name="name" type="text" class="mt-1 block w-full" required autofocus autocomplete="name" />
  63. <x-input-error class="mt-2" :messages="$errors->get('name')" />
  64. </div>
  65. <div>
  66. <x-input-label for="email" :value="__('Email')" />
  67. <x-text-input wire:model="email" id="email" name="email" type="email" class="mt-1 block w-full" required autocomplete="username" />
  68. <x-input-error class="mt-2" :messages="$errors->get('email')" />
  69. @if (auth()->user() instanceof \Illuminate\Contracts\Auth\MustVerifyEmail && ! auth()->user()->hasVerifiedEmail())
  70. <div>
  71. <p class="text-sm mt-2 text-gray-800 dark:text-gray-200">
  72. {{ __('Your email address is unverified.') }}
  73. <button wire:click.prevent="sendVerification" class="underline text-sm text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 dark:focus:ring-offset-gray-800">
  74. {{ __('Click here to re-send the verification email.') }}
  75. </button>
  76. </p>
  77. @if (session('status') === 'verification-link-sent')
  78. <p class="mt-2 font-medium text-sm text-green-600 dark:text-green-400">
  79. {{ __('A new verification link has been sent to your email address.') }}
  80. </p>
  81. @endif
  82. </div>
  83. @endif
  84. </div>
  85. <div class="flex items-center gap-4">
  86. <x-primary-button>{{ __('Save') }}</x-primary-button>
  87. <x-action-message class="me-3" on="profile-updated">
  88. {{ __('Saved.') }}
  89. </x-action-message>
  90. </div>
  91. </form>
  92. </section>