Skip to content

Commit f16a6f7

Browse files
Merge pull request #185 from laravel/replace_login_with_fortify
Replace Login and Logout by replacing it with fortify
2 parents 610b228 + 3621257 commit f16a6f7

File tree

6 files changed

+33
-175
lines changed

6 files changed

+33
-175
lines changed

app/Http/Controllers/Auth/AuthenticatedSessionController.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

app/Http/Requests/Auth/LoginRequest.php

Lines changed: 0 additions & 94 deletions
This file was deleted.

app/Providers/FortifyServiceProvider.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use Illuminate\Http\Request;
77
use Illuminate\Support\Facades\RateLimiter;
88
use Illuminate\Support\ServiceProvider;
9+
use Illuminate\Support\Str;
910
use Inertia\Inertia;
11+
use Laravel\Fortify\Features;
1012
use Laravel\Fortify\Fortify;
1113

1214
class FortifyServiceProvider extends ServiceProvider
@@ -24,11 +26,38 @@ public function register(): void
2426
*/
2527
public function boot(): void
2628
{
29+
$this->configureViews();
30+
$this->configureRateLimiting();
31+
}
32+
33+
/**
34+
* Configure Fortify views.
35+
*/
36+
private function configureViews(): void
37+
{
38+
Fortify::loginView(fn (Request $request) => Inertia::render('auth/login', [
39+
'canResetPassword' => Features::enabled(Features::resetPasswords()),
40+
'status' => $request->session()->get('status'),
41+
]));
42+
2743
Fortify::twoFactorChallengeView(fn () => Inertia::render('auth/two-factor-challenge'));
44+
2845
Fortify::confirmPasswordView(fn () => Inertia::render('auth/confirm-password'));
46+
}
2947

48+
/**
49+
* Configure rate limiting.
50+
*/
51+
private function configureRateLimiting(): void
52+
{
3053
RateLimiter::for('two-factor', function (Request $request) {
3154
return Limit::perMinute(5)->by($request->session()->get('login.id'));
3255
});
56+
57+
RateLimiter::for('login', function (Request $request) {
58+
$throttleKey = Str::transliterate(Str::lower($request->input(Fortify::username())).'|'.$request->ip());
59+
60+
return Limit::perMinute(5)->by($throttleKey);
61+
});
3362
}
3463
}

resources/js/pages/auth/login.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import AuthenticatedSessionController from '@/actions/App/Http/Controllers/Auth/AuthenticatedSessionController';
21
import InputError from '@/components/input-error';
32
import TextLink from '@/components/text-link';
43
import { Button } from '@/components/ui/button';
@@ -8,6 +7,7 @@ import { Label } from '@/components/ui/label';
87
import { Spinner } from '@/components/ui/spinner';
98
import AuthLayout from '@/layouts/auth-layout';
109
import { register } from '@/routes';
10+
import { store } from '@/routes/login';
1111
import { request } from '@/routes/password';
1212
import { Form, Head } from '@inertiajs/react';
1313

@@ -25,7 +25,7 @@ export default function Login({ status, canResetPassword }: LoginProps) {
2525
<Head title="Log in" />
2626

2727
<Form
28-
{...AuthenticatedSessionController.store.form()}
28+
{...store.form()}
2929
resetOnSuccess={['password']}
3030
className="flex flex-col gap-6"
3131
>

routes/auth.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
use App\Http\Controllers\Auth\AuthenticatedSessionController;
43
use App\Http\Controllers\Auth\EmailVerificationNotificationController;
54
use App\Http\Controllers\Auth\EmailVerificationPromptController;
65
use App\Http\Controllers\Auth\NewPasswordController;
@@ -16,12 +15,6 @@
1615
Route::post('register', [RegisteredUserController::class, 'store'])
1716
->name('register.store');
1817

19-
Route::get('login', [AuthenticatedSessionController::class, 'create'])
20-
->name('login');
21-
22-
Route::post('login', [AuthenticatedSessionController::class, 'store'])
23-
->name('login.store');
24-
2518
Route::get('forgot-password', [PasswordResetLinkController::class, 'create'])
2619
->name('password.request');
2720

@@ -46,7 +39,4 @@
4639
Route::post('email/verification-notification', [EmailVerificationNotificationController::class, 'store'])
4740
->middleware('throttle:6,1')
4841
->name('verification.send');
49-
50-
Route::post('logout', [AuthenticatedSessionController::class, 'destroy'])
51-
->name('logout');
5242
});

tests/Feature/Auth/AuthenticationTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,13 @@ public function test_users_are_rate_limited()
8787
{
8888
$user = User::factory()->create();
8989

90-
RateLimiter::increment(implode('|', [$user->email, '127.0.0.1']), amount: 10);
90+
RateLimiter::increment(md5('login'.implode('|', [$user->email, '127.0.0.1'])), amount: 5);
9191

9292
$response = $this->post(route('login.store'), [
9393
'email' => $user->email,
9494
'password' => 'wrong-password',
9595
]);
9696

97-
$response->assertSessionHasErrors('email');
98-
99-
$errors = session('errors');
100-
101-
$this->assertStringContainsString('Too many login attempts', $errors->first('email'));
97+
$response->assertTooManyRequests();
10298
}
10399
}

0 commit comments

Comments
 (0)