adrema/modules/Auth/Components/PasswordResetConfirm.php

88 lines
3.1 KiB
PHP
Raw Normal View History

2024-12-09 00:29:48 +01:00
<?php
namespace Modules\Auth\Components;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
use Illuminate\Validation\Rules\Password as PasswordRule;
use Illuminate\Validation\ValidationException;
use Livewire\Attributes\Layout;
use Livewire\Component;
class PasswordResetConfirm extends Component
{
public string $password = '';
public string $password_confirmation = '';
public string $token = '';
public string $email = '';
public function mount(string $token): void
{
$this->email = request()->query('email');
$this->token = $token;
}
public function submit()
{
$this->validate([
'token' => 'required',
'email' => 'required|email',
'password' => ['required', 'confirmed', PasswordRule::defaults()],
]);
$response = Password::broker()->reset([
'email' => $this->email,
'password' => $this->password,
'password_confirmation' => $this->password_confirmation,
'token' => $this->token
], fn ($user, $password) => $this->resetPassword($user, $password));
if ($response == Password::PASSWORD_RESET) {
$this->dispatch('success', 'Passwort erfolgreich geändert.');
return redirect()->route('home');
}
ValidationException::withMessages([
'password' => 'Passwort konnte nicht geändert werden.',
]);
}
protected function resetPassword($user, $password)
{
$user->password = Hash::make($password);
$user->setRememberToken(Str::random(60));
$user->save();
event(new PasswordReset($user));
auth()->login($user);
}
#[Layout('components.layouts.full')]
public function render(): string
{
return <<<'HTML'
<x-page::full heading="Passwort vergessen" title="Passwort vergessen">
<form wire:submit.prevent="submit">
<div class="grid gap-5">
<span class="text-gray-500 text-sm">
Hier kannst du dein Passwort zurücksetzen.<br />
Gebe dafür ein neues Passwort ein.<br />
Merke oder notiere dir dieses Passwort, bevor du das Formular absendest.<br />
Danach wirst du zum Dashboard weitergeleitet.
</span>
<x-form::text type="password" name="password" wire:model="password" label="Neues Passwort"></x-form::text>
<x-form::text type="password" name="password_confirmation" wire:model="password_confirmation" label="Neues Passwort widerholen"></x-form::text>
<x-ui::button type="submit">Passwort zurücksetzen</x-ui::button>
<div class="flex justify-center">
<a href="/login" class="text-gray-500 text-sm hover:text-gray-300">Zurück zum Login</a>
</div>
</div>
</form>
</x-page::full>
HTML;
}
}