Is It Possible to Use a Single Login URL for All Roles in Filament Shield?
I am currently using Filament Shield with 3 roles: Admin, User, and Courier, and 2 panels: Admin and Courier. The login URLs are currently separated, such as https://url/admin/login for Admin and https://url/courier/login for Courier. I want to change it so that there is only one login URL, like https://url/login. After login, the system will check the user’s role and redirect them to the appropriate panel:
If the role is Admin, redirect to the Admin Panel.
If the role is Courier, redirect to the Courier Panel.
If the role is User, the login should be rejected.
Is this possible to achieve with Filament Shield?
23 Replies
yes.. just modify the login similar to this https://filamentphp.com/docs/3.x/panels/users#authentication-features
We have a tutorial about it: https://filamentexamples.com/tutorial/multiple-panels-sign-in-via-one-sign-in-page
Filament Examples
Filament Multiple Panels: Single Login Page for Admin/User Roles
Having multiple panels is excellent for separating users from other roles. However, managing each different login page can be tricky. So, let's make one login page for all panels and switch redirects based on roles.
Thank you for the information
I've seen the tutorial you provided
when I login using the courier account at url/admin/login still can't login. if I login using the admin account he can login.
Hard to comment without debugging the full code of your project, sorry
Based on the tutorial you provided, we only need to create a LoginResponse and register it in the AppServiceProvider, right?
I suspect this Singleton has not been registered properly, why I think so because, when the admin user logs out, he does not receive the Route [login] not defined error message. according to the tutorial
Yeah maybe in your case something else is required... in our testing that's the only thing we did. Maybe you're on some older Laravel version?
i am using laravel 11
Weird then, the syntax is correct: https://laravel.com/docs/11.x/providers#the-bindings-and-singletons-properties
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
What do you mean exactly by "courier can't login"? What error happens after login?
when I am a courier I try to login with the admin/login url
It error like this These credentials do not match our records.
but when the courier account tries to login with the courier/login url the courier account can enter the courier panel
I have made sure the email and password to login are correct
Is this because I am using filament-shield? I use filament shield to ensure that the courier account cannot access the admin panel.
Mmmm yeah, that may be a good point, in our tutorial we don't use Shield, so maybe that plugin adds extra check for the panel if it doesn't allow you to log in. Sorry, from here I can't help much without debugging your full code and experimenting with it.
Thank you very much for your reply. I have found the problem.
Curious what was the problem?
the problem is in public function canAccessPanel(Panel $panel): bool
Not related to this issue, but wanted to put it here since you guys are talking about it
That tutorial was fantastic and really helpful! ✨
Suggestion:
It would be great to see another follow-up tutorial that ensures the login page redirects to the correct panel when already logged in. Right now, it just goes to the default panel instead of the one set up in the responses.
Feel free to me PM if you need more details 😉
@Povilas K
@CodeWithDennis great idea, adding on my to-do list!
did you add a simple middleware for that or found a better way?
Haven't fixed it yet 👀
lol
I think middlewware is the aproach here. With a simple check and redirect
Probably!
I just didn't have time to work on it yet, but thanks for doing my job. 🤣
Set redirect intended on login success to the panel url
Or
Override response login
reposone login works when user logs in first time not when returns