Laravel Jetstream: Redirect After Login

If you use Laravel Jetstream, you may want to customize where login form redirects to, after submission. For example, what if you want to redirect to different dashboards or URLs, depending on user's role?

Under the hood, Laravel Jetstream uses features from Laravel Fortify.

So, first, you need to have your custom logic.

Let's say that your logic will be inside of your User model like this:

app/Models/User.php:

class User extends Authenticatable
{
// ...
 
public function getRedirectRoute(): string
{
return match((int)$this->role_id) {
1 => 'student.dashboard',
2 => 'teacher.dashboard',
// ...
};
}
}

Next, we must bind the LoginResponse from Fortify to customize the redirect. Typically, this should be done using the register() method in the FortifyServiceProvider class.

app/Providers/FortifyServiceProvider.php:

use Illuminate\Http\RedirectResponse;
use Laravel\Fortify\Contracts\LoginResponse;
 
class FortifyServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->instance(LoginResponse::class, new class implements LoginResponse {
public function toResponse($request): RedirectResponse
{
return redirect(auth()->user()->getRedirectRoute());
}
});
}
 
// ...
}

If you use Breeze and want to redirect the user after login, you can check this tutorial: How to Change Redirect After Login/Register in Laravel Breeze.

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 59 courses (1057 lessons, total 42 h 44 min)
  • 78 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials