|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Providers; |
| 4 | + |
| 5 | +use App\Actions\Fortify\CreateNewUser; |
| 6 | +use App\Actions\Fortify\ResetUserPassword; |
| 7 | +use Illuminate\Cache\RateLimiting\Limit; |
| 8 | +use Illuminate\Http\Request; |
| 9 | +use Illuminate\Support\Facades\RateLimiter; |
| 10 | +use Illuminate\Support\ServiceProvider; |
| 11 | +use Illuminate\Support\Str; |
| 12 | +use Inertia\Inertia; |
| 13 | +use Laravel\Fortify\Features; |
| 14 | +use Laravel\Fortify\Fortify; |
| 15 | + |
| 16 | +class FortifyServiceProvider extends ServiceProvider |
| 17 | +{ |
| 18 | + /** |
| 19 | + * Register any application services. |
| 20 | + */ |
| 21 | + public function register(): void |
| 22 | + { |
| 23 | + // |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Bootstrap any application services. |
| 28 | + */ |
| 29 | + public function boot(): void |
| 30 | + { |
| 31 | + $this->configureActions(); |
| 32 | + $this->configureViews(); |
| 33 | + $this->configureRateLimiting(); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Configure Fortify actions. |
| 38 | + */ |
| 39 | + private function configureActions(): void |
| 40 | + { |
| 41 | + Fortify::resetUserPasswordsUsing(ResetUserPassword::class); |
| 42 | + Fortify::createUsersUsing(CreateNewUser::class); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Configure Fortify views. |
| 47 | + */ |
| 48 | + private function configureViews(): void |
| 49 | + { |
| 50 | + Fortify::loginView(fn (Request $request) => Inertia::render('auth/Login', [ |
| 51 | + 'canResetPassword' => Features::enabled(Features::resetPasswords()), |
| 52 | + 'canRegister' => Features::enabled(Features::registration()), |
| 53 | + 'status' => $request->session()->get('status'), |
| 54 | + ])); |
| 55 | + |
| 56 | + Fortify::resetPasswordView(fn (Request $request) => Inertia::render('auth/ResetPassword', [ |
| 57 | + 'email' => $request->email, |
| 58 | + 'token' => $request->route('token'), |
| 59 | + ])); |
| 60 | + |
| 61 | + Fortify::requestPasswordResetLinkView(fn (Request $request) => Inertia::render('auth/ForgotPassword', [ |
| 62 | + 'status' => $request->session()->get('status'), |
| 63 | + ])); |
| 64 | + |
| 65 | + Fortify::verifyEmailView(fn (Request $request) => Inertia::render('auth/VerifyEmail', [ |
| 66 | + 'status' => $request->session()->get('status'), |
| 67 | + ])); |
| 68 | + |
| 69 | + Fortify::registerView(fn () => Inertia::render('auth/Register')); |
| 70 | + |
| 71 | + Fortify::twoFactorChallengeView(fn () => Inertia::render('auth/TwoFactorChallenge')); |
| 72 | + |
| 73 | + Fortify::confirmPasswordView(fn () => Inertia::render('auth/ConfirmPassword')); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Configure rate limiting. |
| 78 | + */ |
| 79 | + private function configureRateLimiting(): void |
| 80 | + { |
| 81 | + RateLimiter::for('two-factor', function (Request $request) { |
| 82 | + return Limit::perMinute(5)->by($request->session()->get('login.id')); |
| 83 | + }); |
| 84 | + |
| 85 | + RateLimiter::for('login', function (Request $request) { |
| 86 | + $throttleKey = Str::transliterate(Str::lower($request->input(Fortify::username())).'|'.$request->ip()); |
| 87 | + |
| 88 | + return Limit::perMinute(5)->by($throttleKey); |
| 89 | + }); |
| 90 | + } |
| 91 | +} |
0 commit comments