Your Filament application has two panels: /admin
and /user
. Would you want to have ONE login page which would redirect the user to a different panel, based on their role? Let me show you.
Imagine that in the users
table, you have a is_admin
field that determines whether a user is an admin or not.
First, you must create a class that extends the Filament\Http\Responses\Auth\Contracts\LoginResponse
. I will create it in App\Http\Responses
and call it LoginResponse
. Inside this class, there is one method, toResponse()
. In this method, we can check if the user is an admin and then they are redirected to the admin panel.
app/Http/Respones/LoginResponse.php:
use Filament\Pages\Dashboard;use Illuminate\Http\RedirectResponse;use Livewire\Features\SupportRedirects\Redirector;use Filament\Http\Responses\Auth\LoginResponse as BaseLoginResponse; class LoginResponse extends BaseLoginResponse{ public function toResponse($request): RedirectResponse|Redirector { if (auth()->user()->is_admin) { return redirect()->to(Dashboard::getUrl(panel: 'admin')); } return parent::toResponse($request); }}
Then, all we have to do is register the Singleton:
app/Providers/AppServiceProvider.php:
class AppServiceProvider extends ServiceProvider{ public $singletons = [ \Filament\Http\Responses\Auth\Contracts\LoginResponse::class => \App\Http\Responses\LoginResponse::class, ]; // ...}
That's it! When you log in, Filament will attempt to return LoginResponse
internally, but we just changed the implementation to our own, which redirects to the panel based on the user role.
With this solution, when the admin user logs out, he receives an error message Route [login] not defined
. This is because the admin panel doesn't have a login route.
However, we can create a LogoutResponse
and redirect it to the existing login page.
app/Http/Responses/LogoutResponse.php:
use Filament\Facades\Filament;use Illuminate\Http\RedirectResponse;use Filament\Http\Responses\Auth\LogoutResponse as BaseLogoutResponse; class LogoutResponse extends BaseLogoutResponse{ public function toResponse($request): RedirectResponse { if (Filament::getCurrentPanel()->getId() === 'admin') { return redirect()->to(Filament::getLoginUrl()); } return parent::toResponse($request); }}
And, of course, we must register the Singleton.
app/Providers/AppServiceProvider.php:
class AppServiceProvider extends ServiceProvider{ public $singletons = [ \Filament\Http\Responses\Auth\Contracts\LoginResponse::class => \App\Http\Responses\LoginResponse::class, \Filament\Http\Responses\Auth\Contracts\LogoutResponse::class => \App\Http\Responses\LogoutResponse::class, ]; // ...}
One more problem: what happens when a user returns to the application? If a user is logged in but visits the /user
panel, they will not be redirected to the /admin
panel. We can fix that by adding a Middleware to the user panel.
php artisan make:middleware RedirectToProperPanelMiddleware
app/Http/Middleware/RedirectToProperPanelMiddleware.php:
use Closure;use Illuminate\Http\Request;use Filament\Pages\Dashboard;class RedirectToProperPanelMiddleware{ public function handle(Request $request, Closure $next) { if (auth()->check() && auth()->user()->is_admin) { return redirect()->to(Dashboard::getUrl(panel: 'admin')); } return $next($request); }}
app/Providers/Filament/UserPanelProvider.php:
use App\Http\Middleware\RedirectToProperPanelMiddleware;class UserPanelProvider extends PanelProvider{ public function panel(Panel $panel): Panel { return $panel // ... ->authMiddleware([ RedirectToProperPanelMiddleware::class, Authenticate::class, ]); }}
A few of our Premium Examples: