Files
many-notes/app/Livewire/Forms/ResetPasswordForm.php
2024-07-15 14:41:46 +01:00

78 lines
2.2 KiB
PHP

<?php
namespace App\Livewire\Forms;
use Livewire\Form;
use Illuminate\Support\Str;
use Livewire\Attributes\Locked;
use Illuminate\Validation\Rules;
use Livewire\Attributes\Validate;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Password;
use Illuminate\Auth\Events\PasswordReset;
class ResetPasswordForm extends Form
{
#[Locked]
public string $token = '';
public string $email = '';
public string $password = '';
public string $password_confirmation = '';
public function rules()
{
return [
'token' => ['required'],
'email' => ['required', 'string', 'email'],
'password' => ['required', 'string', 'confirmed', Rules\Password::defaults()],
];
}
public function setToken(string $token): void
{
$this->token = $token;
}
public function setEmail(string $email): void
{
$this->email = $email;
}
/**
* Reset the password for the given user.
*/
public function resetPassword(): bool
{
$this->validate();
// Here we will attempt to reset the user's password. If it is successful we
// will update the password on an actual user model and persist it to the
// database. Otherwise we will parse the error and return the response.
$status = Password::reset(
$this->only('email', 'password', 'password_confirmation', 'token'),
function ($user) {
$user->forceFill([
'password' => Hash::make($this->password),
'remember_token' => Str::random(60),
])->save();
event(new PasswordReset($user));
}
);
// If the password was successfully reset, we will redirect the user back to
// the application's home authenticated view. If there is an error we can
// redirect them back to where they came from with their error message.
if ($status != Password::PASSWORD_RESET) {
$this->addError('email', __($status));
return false;
}
Session::flash('status', __($status));
return true;
}
}