Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Laravel fortify multiple guards


Laravel fortify multiple guards


To handle multiple guards with Laravel Fortify, you can follow these steps:

1. Configure Fortify to support multiple guards:
- Open your `config/fortify.php` file.
- Update the `guard` configuration to use the guard you want to authenticate with:

php
   'guard' => 'web',
   

- If you have multiple guards, you can specify the guard for each Fortify feature:

php
   'guard' => [
       'login' => 'web',
       'registration' => 'web',
       'reset-password' => 'web',
       'update-password' => 'web',
       'two-factor-authentication' => 'web',
   ],
   

2. Customize the Fortify service provider:
- Open your `FortifyServiceProvider` located in `app/Providers/FortifyServiceProvider.php`.
- In the `boot` method, you can customize the Fortify features for each guard:

php
   public function boot()
   {
       Fortify::createUsersUsing(CreateNewUser::class);
       Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
       Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);
       Fortify::resetUserPasswordsUsing(ResetUserPassword::class);

       Fortify::loginView(function () {
           return view('auth.login');
       });

       Fortify::registerView(function () {
           return view('auth.register');
       });

       Fortify::requestPasswordResetLinkView(function () {
           return view('auth.forgot-password');
       });

       Fortify::resetPasswordView(function () {
           return view('auth.reset-password');
       });
   }
   

3. Customize the Fortify actions:
- Create custom actions for each Fortify feature, such as `CreateNewUser`, `UpdateUserPassword`, and `ResetUserPassword`.
- In these actions, you can handle the logic for the specific guard.

php
   namespace App\Actions\Fortify;

   use App\Models\User;
   use Illuminate\Support\Facades\Hash;
   use Illuminate\Support\Facades\Validator;
   use Laravel\Fortify\Contracts\CreatesNewUsers;
   use Laravel\Jetstream\Jetstream;

   class CreateNewUser implements CreatesNewUsers
   {
       use PasswordValidationRules;

       public function create(array $input)
       {
           Validator::make($input, [
               'name' => ['required', 'string', 'max:255'],
               'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
               'password' => $this->passwordRules(),
               'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['required', 'accepted'] : '',
           ])->validate();

           return User::create([
               'name' => $input['name'],
               'email' => $input['email'],
               'password' => Hash::make($input['password']),
           ]);
       }
   }
   

4. Use the custom Fortify actions:
- In your application, use the custom Fortify actions instead of the default ones.

php
   use App\Actions\Fortify\CreateNewUser;

   $user = app(CreateNewUser::class)->create($input);
   

By following these steps, you can configure Fortify to work with multiple guards in your Laravel application. This allows you to have different authentication flows and settings for different user types or roles.

Remember to update your views, routes, and any other related components to match the customized Fortify configuration and actions.

Citations:
[1] https://laravel.com/docs/11.x/fortify
[2] https://github.com/laravel/fortify
[3] https://github.com/laravel/fortify/actions/runs/8832562441/job/24250119062
[4] https://github.com/qirolab/laravel-fortify-example
[5] https://laracasts.com/discuss/channels/laravel/laravel-fortify-post-request-to-reset-password-not-working