How to conditionally redirect users after login on Laravel Jetstream
In this article, you will learn how to conditionally redirect users after login to Laravel Jetstream and Fortify applications.
With the introduction of Laravel Jetstream, it is always tempting to setup Laravel projects with Laravel Jetstream and Fortify handling the authentication of your app.
It becomes challenging when your project requires custom redirection to a different route after a user successfully login to our application depending on certain conditions.
For instance, user roles, users permission, and so on. I remember working for a client and I was faced with such a challenge because I started the project with laravel and jetstream installed to handle authentication
I made lots of searches on google for possible solutions but couldn’t find a reasonable solution, so I dug into Jetstream and fortified the source code to see how the login functionality works.
Conditionally redirecting can easily be achieved on Laravel jetstream without much labour on your end by following the simple steps below.
I am believing that you already have your laravel project already setup with fortifying and jetstream installed and you have a basic knowledge of Laravel.
First, we are going to create a folder in our app/Http
folder called Responses, note that you can still place this any way within your application
Next, we are going to create a file LoginResponse.php
inside the Responses folder and add the following code to it
app/Http/Responses/LoginResponse.php
<?php
namespace App\Http\Responses;
use Laravel\Fortify\Contracts\LoginResponse as ContractsLoginResponse;
class LoginResponse implements ContractsLoginResponse
{
public function toResponse($request)
{
// here i am checking if the currently logout in users has a role_id of 2 which make him a regular user and then redirect to the users dashboard else the admin dashboard
if (auth()->user()->role_id == 2) {
return redirect()->intended(config('fortify.home'));
}
return redirect()->intended(config('fortify.admin.home'));
}
}
Here our LoginResponse class implements the Laravel\Fortify\Contracts\LoginResponse
and we then implement the method toResponse()
. Inside this method is where we write all our conditions for redirecting to any route in our application depending on certain conditions. Here i am redirecting to users homepage and admin homepage, so you app can be different here.
Now that our LoginResponse
class is ready, next, we will tell laravel to use this class we created instead of the default. We are now going to do this with one of the Service providers we have on our app. So will be registering this in our JetstreamServicePovider
boot() method, by adding below code.
app/Providers/JetstreamServiceProvider.php
$this->app->singleton(
\Laravel\Fortify\Contracts\LoginResponse::class,
\App\Http\Responses\LoginResponse::class
);
$this->app->singleton(
\Laravel\Fortify\Contracts\TwoFactorLoginResponse::class,
\App\Http\Responses\LoginResponse::class
);
The first line handles our normal login while the second line handles Two factor authentication login, our final code for JetstreamServiceProvider
class should now look like this:
app/Providers/JetstreamServiceProvider.php
<?php
namespace App\Providers;
use App\Actions\Jetstream\DeleteUser;
use Illuminate\Support\ServiceProvider;
use Laravel\Jetstream\Jetstream;
class JetstreamServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->configurePermissions();
Jetstream::deleteUsersUsing(DeleteUser::class);
// we now register our new classes to override the default classes for but normal login and two factor authentication
$this->app->singleton(
\Laravel\Fortify\Contracts\LoginResponse::class,
\App\Http\Responses\LoginResponse::class
);
$this->app->singleton(
\Laravel\Fortify\Contracts\TwoFactorLoginResponse::class,
\App\Http\Responses\LoginResponse::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 the code snippet above, We register our new classes to override the default classes for standard login and two-factor authentication. Looking at the above step, what would have caused your time to remove fortify and Jetstream or even start up a new project with a different login response has just been solved in few steps.