From a3e42448ab564b99a1493d1a4506051bf20ed68c Mon Sep 17 00:00:00 2001 From: DariusIII Date: Tue, 10 Mar 2026 15:28:07 +0100 Subject: [PATCH] Unify toast messages usage --- .../Auth/ForgotPasswordController.php | 22 +++++-- app/Http/Controllers/Auth/LoginController.php | 14 ++--- .../Controllers/Auth/RegisterController.php | 3 +- .../Auth/ResetPasswordController.php | 9 ++- .../PasswordSecurityController.php | 57 +++++++------------ resources/views/auth/2fa.blade.php | 17 ------ resources/views/auth/2fa_verify.blade.php | 12 ---- resources/views/auth/google2fa.blade.php | 17 ------ resources/views/auth/login.blade.php | 12 ---- .../views/auth/passwords/email.blade.php | 10 ---- resources/views/auth/verify.blade.php | 11 ---- resources/views/layouts/guest.blade.php | 26 +++++++++ tests/Feature/RegisterControllerTest.php | 3 +- 13 files changed, 75 insertions(+), 138 deletions(-) diff --git a/app/Http/Controllers/Auth/ForgotPasswordController.php b/app/Http/Controllers/Auth/ForgotPasswordController.php index e06761cea..5e4691d39 100644 --- a/app/Http/Controllers/Auth/ForgotPasswordController.php +++ b/app/Http/Controllers/Auth/ForgotPasswordController.php @@ -50,26 +50,38 @@ class ForgotPasswordController extends Controller $rssToken = $request->input('apikey') ?? ''; if (empty($email) && empty($rssToken)) { - return view('auth.passwords.email')->withErrors(['error' => 'Missing parameter (email and/or apikey) to send password reset']); + return redirect() + ->route('forgottenpassword') + ->withErrors(['error' => 'Missing parameter (email and/or apikey) to send password reset']) + ->withInput($request->except(\App\Support\CaptchaHelper::getResponseFieldName())); } if (\App\Support\CaptchaHelper::isEnabled()) { $validate = Validator::make($request->all(), \App\Support\CaptchaHelper::getValidationRules()); if ($validate->fails()) { - return view('auth.passwords.email')->withErrors(['error' => 'Captcha validation failed.']); + return redirect() + ->route('forgottenpassword') + ->withErrors(['error' => 'Captcha validation failed.']) + ->withInput($request->except(\App\Support\CaptchaHelper::getResponseFieldName())); } } // Check users exists and send an email $ret = ! empty($rssToken) ? User::getByRssToken($rssToken) : User::getByEmail($email); if ($ret === null) { - return view('auth.passwords.email')->withErrors(['error' => 'The email or apikey are not recognised.']); + return redirect() + ->route('forgottenpassword') + ->withErrors(['error' => 'The email or apikey are not recognised.']) + ->withInput($request->except(\App\Support\CaptchaHelper::getResponseFieldName())); } // Check if user is soft deleted $user = User::withTrashed()->find($ret['id']); if ($user && $user->trashed()) { - return view('auth.passwords.email')->withErrors(['error' => 'This account has been deactivated.']); + return redirect() + ->route('forgottenpassword') + ->withErrors(['error' => 'This account has been deactivated.']) + ->withInput($request->except(\App\Support\CaptchaHelper::getResponseFieldName())); } // Generate a forgottenpassword guid, store it in the user table @@ -80,6 +92,6 @@ class ForgotPasswordController extends Controller $resetLink = url('/').'/resetpassword?guid='.$guid; SendPasswordForgottenEmail::dispatch($ret, $resetLink); - return view('auth.passwords.email')->with('status', 'Password reset email has been sent!'); + return redirect()->route('forgottenpassword')->with('success', 'Password reset email has been sent!'); } } diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index 807f1e2f9..36992367d 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -79,7 +79,7 @@ class LoginController extends Controller if ($this->hasTooManyLoginAttempts($request)) { $this->fireLockoutEvent($request); - $request->session()->flash('message', 'You have failed to login too many times.Try again in '.$this->decayMinutes().' minutes.'); + $request->session()->flash('warning', 'You have failed to log in too many times. Try again in '.$this->decayMinutes().' minutes.'); return redirect()->to('login'); } @@ -95,7 +95,7 @@ class LoginController extends Controller if ($user !== null) { // Check if user is soft deleted if ($user->trashed()) { - $request->session()->flash('message', 'This account has been deactivated. Please contact us through contact form to have your account reactivated.'); + $request->session()->flash('error', 'This account has been deactivated. Please contact us through contact form to have your account reactivated.'); return redirect()->to('login'); } @@ -103,7 +103,7 @@ class LoginController extends Controller $rememberMe = $request->has('rememberme') && $request->input('rememberme') === 'on'; if (! $user->isVerified() || $user->isPendingVerification()) { - $request->session()->flash('message', 'You have not verified your email address!'); + $request->session()->flash('warning', 'You have not verified your email address!'); return redirect()->to('login'); } @@ -179,18 +179,18 @@ class LoginController extends Controller $this->incrementLoginAttempts($request); Log::channel('failed_login')->error('Failed login attempt by user: '.$request->input('username').' from IP address: '.$request->ip()); - $request->session()->flash('message', 'Username or email and password combination used does not match our records!'); + $request->session()->flash('error', 'Username or email and password combination used does not match our records!'); } else { $this->incrementLoginAttempts($request); Log::channel('failed_login')->error('Failed login attempt by user: '.$request->input('username').' from IP address: '.$request->ip()); - $request->session()->flash('message', 'Username or email used do not match our records!'); + $request->session()->flash('error', 'Username or email used do not match our records!'); } return redirect()->to('login'); } $this->incrementLoginAttempts($request); - $request->session()->flash('message', implode('', Arr::collapse($validator->errors()->toArray()))); + $request->session()->flash('error', implode('', Arr::collapse($validator->errors()->toArray()))); Log::channel('failed_login')->error('Failed login attempt by user: '.$request->input('username').' from IP address: '.$request->ip()); return redirect()->to('login'); @@ -260,7 +260,7 @@ class LoginController extends Controller } } - return redirect()->to('login')->with('message', 'You have been logged out successfully'); + return redirect()->to('login')->with('success', 'You have been logged out successfully'); } /** diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index f7a1f7a21..283c4c9d9 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -227,8 +227,7 @@ class RegisterController extends Controller return redirect() ->route('login') - ->with('message', 'Your account has been created. You will receive an account confirmation email shortly. Please verify your email address before logging in.') - ->with('message_type', 'info'); + ->with('info', 'Your account has been created. You will receive an account confirmation email shortly. Please verify your email address before logging in.'); } $error = 'Invalid or expired invitation token!'; diff --git a/app/Http/Controllers/Auth/ResetPasswordController.php b/app/Http/Controllers/Auth/ResetPasswordController.php index 72b911cbf..625dd9d65 100644 --- a/app/Http/Controllers/Auth/ResetPasswordController.php +++ b/app/Http/Controllers/Auth/ResetPasswordController.php @@ -35,17 +35,17 @@ class ResetPasswordController extends Controller public function reset(Request $request): mixed { if ($request->missing('guid')) { - return redirect()->route('password.request')->withErrors(['error' => 'No reset code provided.']); + return redirect()->route('password.request')->with('error', 'No reset code provided.'); } $user = User::getByPassResetGuid($request->input('guid')); if ($user === null) { - return redirect()->route('password.request')->withErrors(['error' => 'Bad reset code provided.']); + return redirect()->route('password.request')->with('error', 'Bad reset code provided.'); } // Check if user is soft deleted if ($user->trashed()) { - return redirect()->route('password.request')->withErrors(['error' => 'This account has been deactivated.']); + return redirect()->route('password.request')->with('error', 'This account has been deactivated.'); } // Reset the password, inform the user, send out the email @@ -56,8 +56,7 @@ class ResetPasswordController extends Controller SendPasswordResetEmail::dispatch($user, $newpass); return redirect()->route('login') - ->with('message', 'Your password has been reset to '.$newpass.' and sent to your e-mail address.') - ->with('message_type', 'success'); + ->with('success', 'Your password has been reset to '.$newpass.' and sent to your e-mail address.'); } public function showResetForm(Request $request, mixed $token = null): mixed diff --git a/app/Http/Controllers/PasswordSecurityController.php b/app/Http/Controllers/PasswordSecurityController.php index f579ce14a..cbd728c08 100644 --- a/app/Http/Controllers/PasswordSecurityController.php +++ b/app/Http/Controllers/PasswordSecurityController.php @@ -24,9 +24,7 @@ class PasswordSecurityController extends Controller $user = $request->user(); if (! $user) { - return redirect()->route('login') - ->with('message', 'Please log in to access 2FA settings.') - ->with('message_type', 'danger'); + return $this->redirectToLoginWithError('Please log in to access 2FA settings.'); } $google2fa_url = ''; @@ -55,9 +53,7 @@ class PasswordSecurityController extends Controller $user = $request->user(); if (! $user) { - return redirect()->route('login') - ->with('message', 'Please log in to access 2FA settings.') - ->with('message_type', 'danger'); + return $this->redirectToLoginWithError('Please log in to access 2FA settings.'); } // Add the secret key to the registration data @@ -87,9 +83,7 @@ class PasswordSecurityController extends Controller $user = $request->user(); if (! $user) { - return redirect()->route('login') - ->with('message', 'Please log in to access 2FA settings.') - ->with('message_type', 'danger'); + return $this->redirectToLoginWithError('Please log in to access 2FA settings.'); } $secret = $request->input('verify-code'); @@ -111,9 +105,7 @@ class PasswordSecurityController extends Controller $user = $request->user(); if (! $user) { - return redirect()->route('login') - ->with('message', 'Please log in to access 2FA settings.') - ->with('message_type', 'danger'); + return $this->redirectToLoginWithError('Please log in to access 2FA settings.'); } // Only allow canceling if 2FA is not yet enabled @@ -131,9 +123,7 @@ class PasswordSecurityController extends Controller $user = $request->user(); if (! $user) { - return redirect()->route('login') - ->with('message', 'Please log in to access 2FA settings.') - ->with('message_type', 'danger'); + return $this->redirectToLoginWithError('Please log in to access 2FA settings.'); } if (! (Hash::check($request->get('current-password'), $user->password))) { @@ -164,9 +154,7 @@ class PasswordSecurityController extends Controller // Get the user ID from session if (! $request->session()->has('2fa:user:id')) { - return redirect()->route('login') - ->with('message', 'The two-factor authentication session has expired. Please login again.') - ->with('message_type', 'danger'); + return $this->redirectToLoginWithError('The two-factor authentication session has expired. Please login again.'); } $userId = $request->session()->get('2fa:user:id'); @@ -175,9 +163,7 @@ class PasswordSecurityController extends Controller if (! $user || ! $user->passwordSecurity) { $request->session()->forget('2fa:user:id'); - return redirect()->route('login') - ->with('message', 'User not found or 2FA not configured. Please login again.') - ->with('message_type', 'danger'); + return $this->redirectToLoginWithError('User not found or 2FA not configured. Please login again.'); } // Verify the OTP code @@ -188,8 +174,7 @@ class PasswordSecurityController extends Controller if (! $valid) { return redirect()->route('2fa.verify') - ->with('message', 'Invalid authentication code. Please try again.') - ->with('message_type', 'danger'); + ->with('error', 'Invalid authentication code. Please try again.'); } // Get the remember me preference from session (defaults to false if not set) @@ -213,8 +198,7 @@ class PasswordSecurityController extends Controller // Create the redirect response $redirect = redirect()->to($redirectUrl) - ->with('message', 'Two-factor authentication verified successfully.') - ->with('message_type', 'success'); + ->with('success', 'Two-factor authentication verified successfully.'); // Check for password breach if we have the password stored if ($passwordToCheck) { @@ -279,8 +263,7 @@ class PasswordSecurityController extends Controller { // Check if user ID is stored in the session if (! $request->session()->has('2fa:user:id')) { - return redirect()->route('login') - ->withErrors(['msg' => 'The two-factor authentication session has expired. Please login again.']); + return $this->redirectToLoginWithError('The two-factor authentication session has expired. Please login again.'); } // Get the user ID from session @@ -291,8 +274,7 @@ class PasswordSecurityController extends Controller if (! $user) { $request->session()->forget('2fa:user:id'); - return redirect()->route('login') - ->withErrors(['msg' => 'User not found. Please login again.']); + return $this->redirectToLoginWithError('User not found. Please login again.'); } return view('auth.2fa_verify', compact('user')); @@ -307,9 +289,7 @@ class PasswordSecurityController extends Controller $user = $request->user(); if (! $user) { - return redirect()->route('login') - ->with('message', 'Please log in to access 2FA settings.') - ->with('message_type', 'danger'); + return $this->redirectToLoginWithError('Please log in to access 2FA settings.'); } $request->validate([ @@ -336,9 +316,7 @@ class PasswordSecurityController extends Controller $user = $request->user(); if (! $user) { - return redirect()->route('login') - ->with('message', 'Please log in to access 2FA settings.') - ->with('message_type', 'danger'); + return $this->redirectToLoginWithError('Please log in to access 2FA settings.'); } $google2fa_url = ''; @@ -366,9 +344,7 @@ class PasswordSecurityController extends Controller $user = $request->user(); if (! $user) { - return redirect()->route('login') - ->with('message', 'Please log in to access 2FA settings.') - ->with('message_type', 'danger'); + return $this->redirectToLoginWithError('Please log in to access 2FA settings.'); } $google2fa_url = ''; @@ -387,4 +363,9 @@ class PasswordSecurityController extends Controller return view('auth.2fa')->with('data', $data); } + + private function redirectToLoginWithError(string $message): RedirectResponse + { + return redirect()->route('login')->with('error', $message); + } } diff --git a/resources/views/auth/2fa.blade.php b/resources/views/auth/2fa.blade.php index 38a83c7ff..6ba6d7009 100644 --- a/resources/views/auth/2fa.blade.php +++ b/resources/views/auth/2fa.blade.php @@ -25,23 +25,6 @@

- @if (session('error')) -
-
- - {{ session('error') }} -
-
- @endif - @if (session('success')) -
-
- - {{ session('success') }} -
-
- @endif - @if(!($data['user']->passwordSecurity))

To enable 2FA on your account:

diff --git a/resources/views/auth/2fa_verify.blade.php b/resources/views/auth/2fa_verify.blade.php index 668ad687a..e0f335c6e 100644 --- a/resources/views/auth/2fa_verify.blade.php +++ b/resources/views/auth/2fa_verify.blade.php @@ -21,18 +21,6 @@
- - @if(session('message')) -
-
- - - {{ session('message') }} - -
-
- @endif - @if($errors->any())
diff --git a/resources/views/auth/google2fa.blade.php b/resources/views/auth/google2fa.blade.php index 05806f651..44c891a5f 100644 --- a/resources/views/auth/google2fa.blade.php +++ b/resources/views/auth/google2fa.blade.php @@ -19,23 +19,6 @@
- @if (session('error')) -
-
- - {{ session('error') }} -
-
- @endif - @if (session('success')) -
-
- - {{ session('success') }} -
-
- @endif -
@csrf
diff --git a/resources/views/auth/login.blade.php b/resources/views/auth/login.blade.php index a7a8dd608..9d6584d76 100644 --- a/resources/views/auth/login.blade.php +++ b/resources/views/auth/login.blade.php @@ -21,18 +21,6 @@
- - @if(session('message')) -
-
- - - {{ session('message') }} - -
-
- @endif - @if($errors->any())
diff --git a/resources/views/auth/passwords/email.blade.php b/resources/views/auth/passwords/email.blade.php index a6cafb3e8..62a1213c1 100644 --- a/resources/views/auth/passwords/email.blade.php +++ b/resources/views/auth/passwords/email.blade.php @@ -21,16 +21,6 @@
- - @if(session('status')) -
-
- - {{ session('status') }} -
-
- @endif - @if(isset($errors) && $errors->any())
diff --git a/resources/views/auth/verify.blade.php b/resources/views/auth/verify.blade.php index b6cd67147..99c5e4a6a 100644 --- a/resources/views/auth/verify.blade.php +++ b/resources/views/auth/verify.blade.php @@ -16,17 +16,6 @@
- @if (session('resent')) -
-
- - - {{ __('A fresh verification link has been sent to your email address.') }} - -
-
- @endif -

{{ __('Before proceeding, please check your email for a verification link.') }}

diff --git a/resources/views/layouts/guest.blade.php b/resources/views/layouts/guest.blade.php index 224d66dd2..4ef7cd5c2 100644 --- a/resources/views/layouts/guest.blade.php +++ b/resources/views/layouts/guest.blade.php @@ -34,9 +34,35 @@ @endif + @php + $legacyMessage = session('message'); + $legacyType = session('message_type'); + $legacyType = is_string($legacyType) ? $legacyType : null; + $verificationResent = session('resent') ? __('A fresh verification link has been sent to your email address.') : null; + + $guestFlashMessages = [ + 'success' => session('success') + ?? session('status') + ?? $verificationResent + ?? ($legacyMessage && $legacyType === 'success' ? $legacyMessage : null), + 'error' => session('error') + ?? ($legacyMessage && in_array($legacyType, ['danger', 'error'], true) ? $legacyMessage : null), + 'warning' => session('warning') + ?? ($legacyMessage && $legacyType === 'warning' ? $legacyMessage : null), + 'info' => session('info') + ?? ($legacyMessage && ! in_array($legacyType, ['success', 'danger', 'error', 'warning'], true) ? $legacyMessage : null), + ]; + @endphp + @yield('content') @include('partials.back-to-top') + @include('partials.toast-notifications') + + @stack('scripts') diff --git a/tests/Feature/RegisterControllerTest.php b/tests/Feature/RegisterControllerTest.php index f13fd905f..c52704fff 100644 --- a/tests/Feature/RegisterControllerTest.php +++ b/tests/Feature/RegisterControllerTest.php @@ -128,8 +128,7 @@ class RegisterControllerTest extends TestCase ]); $response->assertRedirect(route('login')); - $response->assertSessionHas('message', 'Your account has been created. You will receive an account confirmation email shortly. Please verify your email address before logging in.'); - $response->assertSessionHas('message_type', 'info'); + $response->assertSessionHas('info', 'Your account has been created. You will receive an account confirmation email shortly. Please verify your email address before logging in.'); } public function test_registration_failure_shows_explanation_for_deactivated_account(): void