From bc21c86414b06dc945382eb70878f2b9e80b7ab9 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Mon, 9 Dec 2024 00:29:48 +0100 Subject: [PATCH] Add password reset confirm --- .../Auth/ResetPasswordController.php | 50 ----------- modules/Auth/AuthServiceProvider.php | 3 +- .../Auth/Components/PasswordResetConfirm.php | 87 +++++++++++++++++++ .../js/views/authentication/PasswordReset.vue | 44 ---------- .../authentication/PasswordResetConfirm.vue | 62 ------------- 5 files changed, 89 insertions(+), 157 deletions(-) delete mode 100644 app/Http/Controllers/Auth/ResetPasswordController.php create mode 100644 modules/Auth/Components/PasswordResetConfirm.php delete mode 100644 resources/js/views/authentication/PasswordReset.vue delete mode 100644 resources/js/views/authentication/PasswordResetConfirm.vue diff --git a/app/Http/Controllers/Auth/ResetPasswordController.php b/app/Http/Controllers/Auth/ResetPasswordController.php deleted file mode 100644 index a2d83dcc..00000000 --- a/app/Http/Controllers/Auth/ResetPasswordController.php +++ /dev/null @@ -1,50 +0,0 @@ -route()->parameter('token'); - - return Inertia::render('authentication/PasswordResetConfirm', [ - 'token' => $token, - 'email' => $request->email, - ]); - } -} diff --git a/modules/Auth/AuthServiceProvider.php b/modules/Auth/AuthServiceProvider.php index 0755bc9d..ed3ff5ed 100644 --- a/modules/Auth/AuthServiceProvider.php +++ b/modules/Auth/AuthServiceProvider.php @@ -6,6 +6,7 @@ use Illuminate\Routing\Router; use Illuminate\Support\ServiceProvider; use Modules\Auth\Components\LoginForm; use Modules\Auth\Components\PasswordReset; +use Modules\Auth\Components\PasswordResetConfirm; class AuthServiceProvider extends ServiceProvider { @@ -28,7 +29,7 @@ class AuthServiceProvider extends ServiceProvider app(Router::class)->middleware(['web', 'guest'])->group(function ($router) { $router->get('/login', LoginForm::class)->name('login'); $router->get('/password/reset', PasswordReset::class)->name('password.request'); - $router->get('/password/reseta', PasswordReset::class)->name('password.reset'); + $router->get('/password/reset/{token}', PasswordResetConfirm::class)->name('password.reset'); }); } } diff --git a/modules/Auth/Components/PasswordResetConfirm.php b/modules/Auth/Components/PasswordResetConfirm.php new file mode 100644 index 00000000..949322f9 --- /dev/null +++ b/modules/Auth/Components/PasswordResetConfirm.php @@ -0,0 +1,87 @@ +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' + +
+
+ + Hier kannst du dein Passwort zurücksetzen.
+ Gebe dafür ein neues Passwort ein.
+ Merke oder notiere dir dieses Passwort, bevor du das Formular absendest.
+ Danach wirst du zum Dashboard weitergeleitet. +
+ + + + Passwort zurücksetzen + +
+
+
+ HTML; + } +} diff --git a/resources/js/views/authentication/PasswordReset.vue b/resources/js/views/authentication/PasswordReset.vue deleted file mode 100644 index ac46418a..00000000 --- a/resources/js/views/authentication/PasswordReset.vue +++ /dev/null @@ -1,44 +0,0 @@ - - - diff --git a/resources/js/views/authentication/PasswordResetConfirm.vue b/resources/js/views/authentication/PasswordResetConfirm.vue deleted file mode 100644 index 6a44c06a..00000000 --- a/resources/js/views/authentication/PasswordResetConfirm.vue +++ /dev/null @@ -1,62 +0,0 @@ - - -