From edb0b540aaeff311a17e131bdd13baabbefe46a6 Mon Sep 17 00:00:00 2001
From: Pushpak Chhajed
Date: Mon, 13 Oct 2025 12:26:39 +0530
Subject: [PATCH 1/3] Replace Login and Logout by replacing it with fortify
---
.../Auth/AuthenticatedSessionController.php | 63 -------------
app/Http/Requests/Auth/LoginRequest.php | 94 -------------------
app/Providers/FortifyServiceProvider.php | 29 ++++++
resources/js/pages/auth/login.tsx | 4 +-
routes/auth.php | 10 --
tests/Feature/Auth/AuthenticationTest.php | 8 +-
tests/Feature/Auth/TwoFactorChallengeTest.php | 10 +-
7 files changed, 35 insertions(+), 183 deletions(-)
delete mode 100644 app/Http/Controllers/Auth/AuthenticatedSessionController.php
delete mode 100644 app/Http/Requests/Auth/LoginRequest.php
diff --git a/app/Http/Controllers/Auth/AuthenticatedSessionController.php b/app/Http/Controllers/Auth/AuthenticatedSessionController.php
deleted file mode 100644
index 80da6826b..000000000
--- a/app/Http/Controllers/Auth/AuthenticatedSessionController.php
+++ /dev/null
@@ -1,63 +0,0 @@
- Route::has('password.request'),
- 'status' => $request->session()->get('status'),
- ]);
- }
-
- /**
- * Handle an incoming authentication request.
- */
- public function store(LoginRequest $request): RedirectResponse
- {
- $user = $request->validateCredentials();
-
- if (Features::enabled(Features::twoFactorAuthentication()) && $user->hasEnabledTwoFactorAuthentication()) {
- $request->session()->put([
- 'login.id' => $user->getKey(),
- 'login.remember' => $request->boolean('remember'),
- ]);
-
- return to_route('two-factor.login');
- }
-
- Auth::login($user, $request->boolean('remember'));
-
- $request->session()->regenerate();
-
- return redirect()->intended(route('dashboard', absolute: false));
- }
-
- /**
- * Destroy an authenticated session.
- */
- public function destroy(Request $request): RedirectResponse
- {
- Auth::guard('web')->logout();
-
- $request->session()->invalidate();
- $request->session()->regenerateToken();
-
- return redirect('/');
- }
-}
diff --git a/app/Http/Requests/Auth/LoginRequest.php b/app/Http/Requests/Auth/LoginRequest.php
deleted file mode 100644
index d426f112c..000000000
--- a/app/Http/Requests/Auth/LoginRequest.php
+++ /dev/null
@@ -1,94 +0,0 @@
-|string>
- */
- public function rules(): array
- {
- return [
- 'email' => ['required', 'string', 'email'],
- 'password' => ['required', 'string'],
- ];
- }
-
- /**
- * Validate the request's credentials and return the user without logging them in.
- *
- * @throws \Illuminate\Validation\ValidationException
- */
- public function validateCredentials(): User
- {
- $this->ensureIsNotRateLimited();
-
- /** @var User|null $user */
- $user = Auth::getProvider()->retrieveByCredentials($this->only('email', 'password'));
-
- if (! $user || ! Auth::getProvider()->validateCredentials($user, $this->only('password'))) {
- RateLimiter::hit($this->throttleKey());
-
- throw ValidationException::withMessages([
- 'email' => __('auth.failed'),
- ]);
- }
-
- RateLimiter::clear($this->throttleKey());
-
- return $user;
- }
-
- /**
- * Ensure the login request is not rate limited.
- *
- * @throws \Illuminate\Validation\ValidationException
- */
- public function ensureIsNotRateLimited(): void
- {
- if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) {
- return;
- }
-
- event(new Lockout($this));
-
- $seconds = RateLimiter::availableIn($this->throttleKey());
-
- throw ValidationException::withMessages([
- 'email' => __('auth.throttle', [
- 'seconds' => $seconds,
- 'minutes' => ceil($seconds / 60),
- ]),
- ]);
- }
-
- /**
- * Get the rate-limiting throttle key for the request.
- */
- public function throttleKey(): string
- {
- return $this->string('email')
- ->lower()
- ->append('|'.$this->ip())
- ->transliterate()
- ->value();
- }
-}
diff --git a/app/Providers/FortifyServiceProvider.php b/app/Providers/FortifyServiceProvider.php
index c13f6ee17..56fface82 100644
--- a/app/Providers/FortifyServiceProvider.php
+++ b/app/Providers/FortifyServiceProvider.php
@@ -6,7 +6,9 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
+use Illuminate\Support\Str;
use Inertia\Inertia;
+use Laravel\Fortify\Features;
use Laravel\Fortify\Fortify;
class FortifyServiceProvider extends ServiceProvider
@@ -24,11 +26,38 @@ public function register(): void
*/
public function boot(): void
{
+ $this->configureViews();
+ $this->configureRateLimiting();
+ }
+
+ /**
+ * Configure Fortify views.
+ */
+ private function configureViews(): void
+ {
+ Fortify::loginView(fn (Request $request) => Inertia::render('auth/login', [
+ 'canResetPassword' => Features::enabled(Features::resetPasswords()),
+ 'status' => $request->session()->get('status'),
+ ]));
+
Fortify::twoFactorChallengeView(fn () => Inertia::render('auth/two-factor-challenge'));
+
Fortify::confirmPasswordView(fn () => Inertia::render('auth/confirm-password'));
+ }
+ /**
+ * Configure rate limiting.
+ */
+ private function configureRateLimiting(): void
+ {
RateLimiter::for('two-factor', function (Request $request) {
return Limit::perMinute(5)->by($request->session()->get('login.id'));
});
+
+ RateLimiter::for('login', function (Request $request) {
+ $throttleKey = Str::transliterate(Str::lower($request->input(Fortify::username())).'|'.$request->ip());
+
+ return Limit::perMinute(5)->by($throttleKey);
+ });
}
}
diff --git a/resources/js/pages/auth/login.tsx b/resources/js/pages/auth/login.tsx
index d52765bd4..7fff5f54c 100644
--- a/resources/js/pages/auth/login.tsx
+++ b/resources/js/pages/auth/login.tsx
@@ -1,4 +1,3 @@
-import AuthenticatedSessionController from '@/actions/App/Http/Controllers/Auth/AuthenticatedSessionController';
import InputError from '@/components/input-error';
import TextLink from '@/components/text-link';
import { Button } from '@/components/ui/button';
@@ -7,6 +6,7 @@ import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import AuthLayout from '@/layouts/auth-layout';
import { register } from '@/routes';
+import { store } from '@/routes/login';
import { request } from '@/routes/password';
import { Form, Head } from '@inertiajs/react';
import { LoaderCircle } from 'lucide-react';
@@ -25,7 +25,7 @@ export default function Login({ status, canResetPassword }: LoginProps) {