Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I create a custom login pipeline in Jetstream


How can I create a custom login pipeline in Jetstream


To create a custom login pipeline in Laravel Jetstream, you can use the `Fortify::authenticateThrough()` method. This method allows you to define a custom pipeline of classes that the login request should be piped through.

Here's an example of how you can create a custom login pipeline in your `JetstreamServiceProvider`:

php
<?php

namespace App\Providers;

use App\Actions\Fortify\AttemptToAuthenticate;
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
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    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,
                // Add your custom authentication actions here
                App\Actions\Fortify\CustomAuthenticationAction::class,
            ]);
        });
    }

    /**
     * Configure the permissions that are available within the application.
     *
     * @return void
     */
    protected function configurePermissions()
    {
        Jetstream::defaultApiTokenPermissions(['read']);

        Jetstream::permissions([
            'create',
            'read',
            'update',
            'delete',
        ]);
    }
}

In this example, we're using the `Fortify::authenticateThrough()` method to define a custom pipeline of authentication actions. The pipeline includes the default actions provided by Fortify, and we've added a custom action called `App\Actions\Fortify\CustomAuthenticationAction`.

The `CustomAuthenticationAction` class should have an `__invoke()` method that receives the incoming `Illuminate\Http\Request` instance and the `$next` variable, which is used to pass the request to the next class in the pipeline.

Here's an example of what the `CustomAuthenticationAction` class might look like:

php
<?php

namespace App\Actions\Fortify;

use Illuminate\Http\Request;

class CustomAuthenticationAction
{
    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    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);
    }
}

By defining a custom authentication pipeline, you can add additional validation, checks, or logic to the login process. This can be useful if you have specific requirements or business rules that need to be enforced during the authentication process.

Remember to thoroughly test your custom authentication pipeline 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://laracasts.com/discuss/channels/laravel/laravel-jetstream-login
[3] https://www.youtube.com/watch?v=g8eoTYQG288
[4] https://www.itsolutionstuff.com/post/laravel-8-authentication-using-jetstream-exampleexample.html
[5] https://jetstream.laravel.com/features/two-factor-authentication.html