To redirect users to different URLs after login based on their user type in Laravel Fortify, you can follow these steps:
1. Customize the `LoginResponse` class:
- Create a new file called `LoginResponse.php` in your `app/Http/Responses` directory.
- Implement the `LoginResponseContract` interface and override the `toResponse` method.
- In the `toResponse` method, check the user's type and redirect them to the appropriate URL.
php
<?php
namespace App\Http\Responses;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
class LoginResponse implements LoginResponseContract
{
public function toResponse($request)
{
$user = $request->user();
if ($user->type === 'admin') {
return redirect()->intended(route('admin.dashboard'));
}
return redirect()->intended(route('user.dashboard'));
}
}
2. Register the custom `LoginResponse` class:
- Open your `app/Providers/FortifyServiceProvider.php` file.
- In the `boot` method, bind the `LoginResponseContract` to your custom `LoginResponse` class.
php
<?php
namespace App\Providers;
use App\Http\Responses\LoginResponse;
use Laravel\Fortify\Fortify;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
use Illuminate\Support\ServiceProvider;
class FortifyServiceProvider extends ServiceProvider
{
public function boot()
{
Fortify::createUsersUsing(CreateNewUser::class);
Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);
Fortify::resetUserPasswordsUsing(ResetUserPassword::class);
Fortify::loginView(function () {
return view('auth.login');
});
$this->app->bind(LoginResponseContract::class, LoginResponse::class);
}
}
3. Determine the user's type:
- In your `User` model, add a `type` attribute and set it to the appropriate value for each user (e.g., 'admin', 'user').
- You can do this manually, or you can create a custom registration process that sets the user's type based on the provided information.
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens;
use HasFactory;
use HasProfilePhoto;
use Notifiable;
use TwoFactorAuthenticatable;
protected $fillable = [
'name',
'email',
'password',
'type', // Add the 'type' attribute
];
}
With these changes, when a user logs in, the `LoginResponse` class will check the user's type and redirect them to the appropriate URL (e.g., `/admin/dashboard` for admins, `/user/dashboard` for regular users).
Make sure to create the necessary routes and controllers for the admin and user dashboards, and update your application's navigation and access control logic accordingly.
Citations:[1] https://laracasts.com/discuss/channels/laravel/redirect-after-login-according-to-user-role-laravel-fortify
[2] https://www.youtube.com/watch?v=ApYh614oS54
[3] https://laracasts.com/discuss/channels/laravel/redirect-after-login-or-register-in-laravel-8-fortify?reply=689012
[4] https://stackoverflow.com/questions/64074079/laravel-fortify-customize-authentication-redirect
[5] https://reniki.com/blog/redirect-back-previous-page-after-login-laravel-fortify