-
Notifications
You must be signed in to change notification settings - Fork 94
Auth Reset Page
This file is responsible for displaying the reset password form in a Laravel project using the Livewire and Volt libraries. This form allows users to request a password reset link.
The file makes use of the Livewire's state and rules functions, which are imported at the beginning of the file. The state function is used to define an initial state, while the rules function sets validation rules for the email.
<?php
use Illuminate\Support\Facades\Password;
use function Livewire\Volt\{state, rules};
state(['email' => null, 'emailSentMessage' => false]);
rules(['email' => 'required|email']);
$sendResetPasswordLink = function(){
$this->validate();
$response = Password::broker()->sendResetLink(['email' => $this->email]);
if ($response == Password::RESET_LINK_SENT) {
$this->emailSentMessage = trans($response);
return;
}
$this->addError('email', trans($response));
}
?>
The above PHP code does the following:
-
It uses two Laravel classes:
Password
fromIlluminate\Support\Facades
andstate
,rules
fromLivewire\Volt
. -
It defines two states:
email
andemailSentMessage
with initial valuesnull
andfalse
respectively. -
It sets a validation rule for the
email
state. This requires thatemail
should be a required field and a valid email address. -
It defines a function
sendResetPasswordLink
that:- Validates the email using the set rules.
- Sends a password reset link to the provided email.
- If the link is sent successfully, it sets the
emailSentMessage
state to the response message. Otherwise, it adds an error to theemail
field with the response message.
<x-layouts.app>
<div class="flex flex-col items-stretch justify-center w-screen h-screen sm:items-center">
<!-- omitted code -->
@volt('auth.password.reset')
<div class="mt-8 sm:mx-auto sm:w-full sm:max-w-md">
<div class="px-10 py-0 sm:py-8 sm:shadow-sm sm:bg-white sm:border sm:rounded-lg border-gray-200/60">
@if ($emailSentMessage)
<!-- omitted code -->
@else
<form wire:submit="sendResetPasswordLink" class="space-y-6">
<x-ui.input label="Email address" type="email" id="email" name="email" wire:model="email" />
<x-ui.button type="primary" submit="true">Send password reset link</x-ui.button>
</form>
@endif
</div>
</div>
@endvolt
</div>
</x-layouts.app>
The above HTML code section is responsible for:
-
Loading the
app
layout and indicating a section for the password reset. -
If the
$emailSentMessage
state is true (meaning the password reset link was sent successfully), it displays a success message (omitted in the example for brevity). -
If the
$emailSentMessage
state is false (i.e., the password reset link hasn't been sent), it displays a form for the user to enter their email and request a password reset link.
Note: The HTML code uses Volt and Blade syntax along with ✨ Tailwind CSS ✨ for styling.