Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I integrate custom authentication classes with Fortify
    laravel Fortify


How do I integrate custom authentication classes with Fortify


To integrate custom authentication classes with Laravel Fortify, you can follow these steps:

1. Create a Custom Authentication Action:
- Create a new class in your `App\Actions\Fortify` namespace, for example, `CustomAuthenticationAction.php`.
- This class should have an `__invoke()` method that receives the incoming `Illuminate\Http\Request` and the `$next` variable.
- Inside the `__invoke()` method, you can add your custom authentication logic.

Example:

php
   namespace App\Actions\Fortify;

   use Illuminate\Http\Request;

   class CustomAuthenticationAction
   {
       public function __invoke(Request $request, $next)
       {
           // Perform custom authentication logic here
           // For example, you could check if the user is allowed to log in based on some custom criteria

           return $next($request);
       }
   }
   

2. Define the Custom Authentication Pipeline:
- In your `App\Providers\JetstreamServiceProvider` (or any other service provider), override the `boot()` method.
- Use the `Fortify::authenticateThrough()` method to define the custom authentication pipeline.
- Include your `CustomAuthenticationAction` class in the pipeline.

Example:

php
   namespace App\Providers;

   use App\Actions\Fortify\AttemptToAuthenticate;
   use App\Actions\Fortify\CustomAuthenticationAction;
   use App\Actions\Fortify\EnsureLoginIsNotThrottled;
   use App\Actions\Fortify\PrepareAuthenticatedSession;
   use App\Actions\Fortify\RedirectIfTwoFactorAuthenticatable;
   use Illuminate\Http\Request;
   use Laravel\Fortify\Fortify;
   use Laravel\Jetstream\Jetstream;

   class JetstreamServiceProvider extends \Laravel\Jetstream\JetstreamServiceProvider
   {
       public function boot()
       {
           $this->configurePermissions();

           Fortify::authenticateThrough(function (Request $request) {
               return array_filter([
                   config('fortify.limiters.login') ? null : EnsureLoginIsNotThrottled::class,
                   RedirectIfTwoFactorAuthenticatable::class,
                   AttemptToAuthenticate::class,
                   PrepareAuthenticatedSession::class,
                   CustomAuthenticationAction::class,
               ]);
           });
       }

       // ...
   }
   

3. Customize the Custom Authentication Action:
- In your `CustomAuthenticationAction` class, you can add your custom authentication logic.
- For example, you could perform additional checks, validations, or even completely override the authentication process.

Example:

php
   namespace App\Actions\Fortify;

   use Illuminate\Http\Request;

   class CustomAuthenticationAction
   {
       public function __invoke(Request $request, $next)
       {
           // Perform custom authentication logic
           if ($this->isUserAllowedToLogin($request)) {
               return $next($request);
           }

           // Return a custom response or redirect if the user is not allowed to log in
           return response()->json(['error' => 'You are not allowed to log in.'], 403);
       }

       protected function isUserAllowedToLogin(Request $request)
       {
           // Implement your custom logic to check if the user is allowed to log in
           // For example, you could check a custom user attribute or a permission
           return true;
       }
   }
   

By following these steps, you can integrate your custom authentication logic with Laravel Fortify. This allows you to add additional validation, checks, or completely override the authentication process to match your application's specific requirements.

Remember to thoroughly test your custom authentication logic to ensure that it works as expected and doesn't introduce any security vulnerabilities.

Citations:
[1] https://jetstream.laravel.com/features/authentication.html
[2] https://kinsta.com/blog/laravel-authentication/
[3] https://laracasts.com/discuss/channels/laravel/laravel-jetstream-login
[4] https://laravel.com/docs/11.x/authentication
[5] https://laravel.com/docs/7.x/authentication