DEV Community

Cover image for How to override Fortify default login behavior in Jetstream
Snehal Rajeev Moon
Snehal Rajeev Moon

Posted on • Updated on

How to override Fortify default login behavior in Jetstream

Hello artisan!
In this blog post we are going to see how to override fortify default login behavior when used with jestream.

Jetstream uses Laravel Fortify under the hood. Basically fortify uses the pipelines to route each request through series of classes and this class will take care of single task for the authenticated user. It will check where to redirect the user after the successful login or registration.
In this blog we will add alert message when user is authenticated. We can also redirect to different routes base on type of the users.

Now we are going to see how to override Fortify login.

Laravel\Fortify\Contracts\LoginResponse class is the last step in authentication pipeline. Now we are going to override LoginResponse class with our own class.

To write our customized code first we need to create a Responses directory in App\Http\Responses. After that create LoginResponse this is the custom class in which we are going to override toResponse method.

Add below code in LoginResponse class

 <?php

namespace App\Http\Responses;

use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;

class LoginResponse implements LoginResponseContract
{
    /**
     * @param  $request
     * @return mixed
     */
    public function toResponse($request)
    {
        return redirect()->back()->with('status', 'Login Successfull');
    }
}
Enter fullscreen mode Exit fullscreen mode

After that we have to bind our custom LoginResponse in FortifyServiceProvider boot() method, so it can override the default behavior provided by fortify.

add below code

<?php
namespace App\Providers;

use App\Http\Responses\LoginResponse;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
class FortifyServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
       // example to show flash msg after successful login
        $this->app->singleton(LoginResponseContract::class, LoginResponse::class);

      /* but if you want to redirect the user on different blade 
 *file based on the user's type then you can refer below code
      */

      $home = auth()->user()->is_admin ?   
        '/admin-dashboard' : 
        '/dashboard';
      return redirect()->intended($home);
   }
}
Enter fullscreen mode Exit fullscreen mode

In the above we have seen two ways to override the custom LoginResponse.

now in your login.blade.php file add the below code, to show the flash/alert message after successful login.

 @if (session('status'))
            <div class="alert alert-success mb-3 rounded-0" role="alert">
                {{ session('status') }}
            </div>
 @endif
Enter fullscreen mode Exit fullscreen mode

We can also override/customized other fortify and jetstream feature in same way, the feature which can be customized is given in the vender directory. Go to App\Providers and open the FortifyServiceProvider registerResponseBindings() method you can see customizable features.

Refer the below code.

<?php

namespace Laravel\Fortify;

// ...

class FortifyServiceProvider extends ServiceProvider
{
    // ...

    /**
     * Register the response bindings.
     *
     * @return void
     */
    protected function registerResponseBindings()
    {
        $this->app->singleton(FailedPasswordConfirmationResponseContract::class, FailedPasswordConfirmationResponse::class);
        $this->app->singleton(FailedPasswordResetLinkRequestResponseContract::class, FailedPasswordResetLinkRequestResponse::class);
        $this->app->singleton(FailedPasswordResetResponseContract::class, FailedPasswordResetResponse::class);
        $this->app->singleton(FailedTwoFactorLoginResponseContract::class, FailedTwoFactorLoginResponse::class);
        $this->app->singleton(LockoutResponseContract::class, LockoutResponse::class);
        $this->app->singleton(LoginResponseContract::class, LoginResponse::class);
        $this->app->singleton(TwoFactorLoginResponseContract::class, TwoFactorLoginResponse::class);
        $this->app->singleton(LogoutResponseContract::class, LogoutResponse::class);
        $this->app->singleton(PasswordConfirmedResponseContract::class, PasswordConfirmedResponse::class);
        $this->app->singleton(PasswordResetResponseContract::class, PasswordResetResponse::class);
        $this->app->singleton(RegisterResponseContract::class, RegisterResponse::class);
        $this->app->singleton(SuccessfulPasswordResetLinkRequestResponseContract::class, SuccessfulPasswordResetLinkRequestResponse::class);
    }
}
Enter fullscreen mode Exit fullscreen mode

Thank You for reading!!
🦄 🦄 🦁

Top comments (0)