
Role-Based Redirects in Laravel Fortify with Spatie Permissions
In this guide, you'll learn how to implement role-based redirects in Laravel Fortify using Spatie's permissions package. First, we'll show you how to use an anonymous class to customize the login response. This means we can redirect users to different URLs after they log in, depending on their roles.Then, we'll explain how to create a dedicated class for this purpose. This can make your code more organized and easier to maintain in the long run.
By the end of this tutorial, you'll be able to redirect users to different URLs based on their roles after they log in.
1. Setting Up Spatie Permissions
Before we start, ensure you have the Spatie permissions package installed and configured in your Laravel project. If not, you can install it using Composer:
Configure your User model to use the HasRoles trait:
2. Creating a Custom Login Response
Using an Anonymous Class : In your FortifyServiceProvider, you can define a custom login response using an anonymous class. This allows you to implement role-based redirection logic directly within the service provider.
In your app/Providers/FortifyServiceProvider.php, add the following code:
Register Method: Here, we use an anonymous class to implement the LoginResponse interface. The toResponse method checks the user's role and redirects them accordingly.
Boot Method: This method customizes the authentication process by checking the user's credentials
Ensure Fortify is Registered
Make sure that FortifyServiceProvider is included in your config/app.php under the providers array:
3. Dedicated Class for Custom Login Response
If you prefer to keep your code clean and maintainable, you can create a dedicated class for the custom login response.
Create Custom Login Response Class : First, create a new class to handle the login response:
Update FortifyServiceProvider
Next, update your FortifyServiceProvider to use this custom login response class:
Explanation
Custom Login Response Class: This class implements Fortify's LoginResponse contract and defines the redirection logic based on user roles.
FortifyServiceProvider: Registers the custom login response class in the register method.