Skip to content

Commit edb0b54

Browse files
committed
Replace Login and Logout by replacing it with fortify
1 parent 196757d commit edb0b54

File tree

7 files changed

+35
-183
lines changed

7 files changed

+35
-183
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';
@@ -7,6 +6,7 @@ import { Input } from '@/components/ui/input';
76
import { Label } from '@/components/ui/label';
87
import AuthLayout from '@/layouts/auth-layout';
98
import { register } from '@/routes';
9+
import { store } from '@/routes/login';
1010
import { request } from '@/routes/password';
1111
import { Form, Head } from '@inertiajs/react';
1212
import { LoaderCircle } from 'lucide-react';
@@ -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
}

tests/Feature/Auth/TwoFactorChallengeTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use App\Models\User;
66
use Illuminate\Foundation\Testing\RefreshDatabase;
7-
use Inertia\Testing\AssertableInertia as Assert;
7+
use Inertia\Testing\AssertableInertia;
88
use Laravel\Fortify\Features;
99
use Tests\TestCase;
1010

@@ -36,20 +36,14 @@ public function test_two_factor_challenge_can_be_rendered(): void
3636

3737
$user = User::factory()->create();
3838

39-
$user->forceFill([
40-
'two_factor_secret' => encrypt('test-secret'),
41-
'two_factor_recovery_codes' => encrypt(json_encode(['code1', 'code2'])),
42-
'two_factor_confirmed_at' => now(),
43-
])->save();
44-
4539
$this->post(route('login'), [
4640
'email' => $user->email,
4741
'password' => 'password',
4842
]);
4943

5044
$this->get(route('two-factor.login'))
5145
->assertOk()
52-
->assertInertia(fn (Assert $page) => $page
46+
->assertInertia(fn (AssertableInertia $page) => $page
5347
->component('auth/two-factor-challenge')
5448
);
5549
}

0 commit comments

Comments
 (0)