Skip to content

Commit 65e8687

Browse files
committed
Refactor authentication by replacing it with fortify
1 parent 6646aee commit 65e8687

File tree

8 files changed

+26
-198
lines changed

8 files changed

+26
-198
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: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
use Illuminate\Http\Request;
77
use Illuminate\Support\Facades\RateLimiter;
88
use Illuminate\Support\ServiceProvider;
9-
use Inertia\Inertia;
9+
use Illuminate\Support\Str;
10+
use Laravel\Fortify\Actions\RedirectIfTwoFactorAuthenticatable;
1011
use Laravel\Fortify\Fortify;
1112

1213
class FortifyServiceProvider extends ServiceProvider
@@ -24,11 +25,16 @@ public function register(): void
2425
*/
2526
public function boot(): void
2627
{
27-
Fortify::twoFactorChallengeView(fn () => Inertia::render('auth/TwoFactorChallenge'));
28-
Fortify::confirmPasswordView(fn () => Inertia::render('auth/ConfirmPassword'));
28+
Fortify::redirectUserForTwoFactorAuthenticationUsing(RedirectIfTwoFactorAuthenticatable::class);
2929

3030
RateLimiter::for('two-factor', function (Request $request) {
3131
return Limit::perMinute(5)->by($request->session()->get('login.id'));
3232
});
33+
34+
RateLimiter::for('login', function (Request $request) {
35+
$throttleKey = Str::transliterate(Str::lower($request->input(Fortify::username())).'|'.$request->ip());
36+
37+
return Limit::perMinute(5)->by($throttleKey);
38+
});
3339
}
3440
}

config/fortify.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
|
131131
*/
132132

133-
'views' => true,
133+
'views' => false,
134134

135135
/*
136136
|--------------------------------------------------------------------------

resources/js/pages/auth/Login.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<script setup lang="ts">
2-
import AuthenticatedSessionController from '@/actions/App/Http/Controllers/Auth/AuthenticatedSessionController';
32
import InputError from '@/components/InputError.vue';
43
import TextLink from '@/components/TextLink.vue';
54
import { Button } from '@/components/ui/button';
@@ -8,6 +7,7 @@ import { Input } from '@/components/ui/input';
87
import { Label } from '@/components/ui/label';
98
import AuthBase from '@/layouts/AuthLayout.vue';
109
import { register } from '@/routes';
10+
import { store } from '@/routes/login';
1111
import { request } from '@/routes/password';
1212
import { Form, Head } from '@inertiajs/vue3';
1313
import { LoaderCircle } from 'lucide-vue-next';
@@ -33,7 +33,7 @@ defineProps<{
3333
</div>
3434

3535
<Form
36-
v-bind="AuthenticatedSessionController.store.form()"
36+
v-bind="store.form()"
3737
:reset-on-success="['password']"
3838
v-slot="{ errors, processing }"
3939
class="flex flex-col gap-6"

routes/auth.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
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;
76
use App\Http\Controllers\Auth\PasswordResetLinkController;
87
use App\Http\Controllers\Auth\RegisteredUserController;
98
use App\Http\Controllers\Auth\VerifyEmailController;
9+
use Illuminate\Http\Request;
1010
use Illuminate\Support\Facades\Route;
11+
use Inertia\Inertia;
1112

1213
Route::middleware('guest')->group(function () {
14+
Route::get('login', fn (Request $request) => Inertia::render('auth/Login', [
15+
'canResetPassword' => Route::has('password.request'),
16+
'status' => $request->session()->get('status'),
17+
]))->name('login');
18+
1319
Route::get('register', [RegisteredUserController::class, 'create'])
1420
->name('register');
1521

1622
Route::post('register', [RegisteredUserController::class, 'store'])
1723
->name('register.store');
1824

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

@@ -47,6 +47,9 @@
4747
->middleware('throttle:6,1')
4848
->name('verification.send');
4949

50-
Route::post('logout', [AuthenticatedSessionController::class, 'destroy'])
51-
->name('logout');
50+
Route::get('user/confirm-password', fn () => Inertia::render('auth/ConfirmPassword'))
51+
->name('password.confirm');
52+
53+
Route::get('two-factor-challenge', fn () => Inertia::render('auth/TwoFactorChallenge'))
54+
->name('two-factor.login');
5255
});

tests/Feature/Auth/AuthenticationTest.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use App\Models\User;
66
use Illuminate\Foundation\Testing\RefreshDatabase;
7-
use Illuminate\Support\Facades\RateLimiter;
87
use Laravel\Fortify\Features;
98
use Tests\TestCase;
109

@@ -82,22 +81,4 @@ public function test_users_can_logout()
8281
$this->assertGuest();
8382
$response->assertRedirect(route('home'));
8483
}
85-
86-
public function test_users_are_rate_limited()
87-
{
88-
$user = User::factory()->create();
89-
90-
RateLimiter::increment(implode('|', [$user->email, '127.0.0.1']), amount: 10);
91-
92-
$response = $this->post(route('login.store'), [
93-
'email' => $user->email,
94-
'password' => 'wrong-password',
95-
]);
96-
97-
$response->assertSessionHasErrors('email');
98-
99-
$errors = session('errors');
100-
101-
$this->assertStringContainsString('Too many login attempts', $errors->first('email'));
102-
}
10384
}

tests/Feature/Auth/TwoFactorChallengeTest.php

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

55
use App\Models\User;
66
use Illuminate\Foundation\Testing\RefreshDatabase;
7-
use Inertia\Testing\AssertableInertia as Assert;
87
use Laravel\Fortify\Features;
98
use Tests\TestCase;
109

@@ -42,15 +41,11 @@ public function test_two_factor_challenge_can_be_rendered(): void
4241
'two_factor_confirmed_at' => now(),
4342
])->save();
4443

45-
$this->post(route('login'), [
44+
$response = $this->post(route('login'), [
4645
'email' => $user->email,
4746
'password' => 'password',
4847
]);
4948

50-
$this->get(route('two-factor.login'))
51-
->assertOk()
52-
->assertInertia(fn (Assert $page) => $page
53-
->component('auth/TwoFactorChallenge')
54-
);
49+
$response->assertRedirect(route('two-factor.login'));
5550
}
5651
}

0 commit comments

Comments
 (0)