Multi Panel
I have three different panels: 'hr', 'finance', and 'property,' each with its respective logins. My goal is to allow a superadmin to register new users with their email, password, and department. When a registered user logs in, they should be directed to their designated department panel.
Solution:Jump to solution
Use some debugging tools. Xdebug, Ray,
dd()
whatever. Check whether your condition applies and whether you reach the if statement19 Replies
You can add a middleware that checks the current panel and redirects them if it's the wrong one
Please can you guide me through that
- Create a Laravel Middleware
- Register it on your panels
->middleware()
- Check Filament::getCurrentPanel()->id
for the current panel and compare it to auth()->user()->department
Great this is how I did it
public function handle(Request $request, Closure $next): Response
{
$user = Auth::user();
$department = $user->department_id;
$panel = filament()->getCurrentPanel()->getId();
if ($panel === 'hr' && $department !== 'hr') {
return redirect()->route('filament.hr.pages.dashboard');
} else if ($panel === 'property' && $department !== 'property') {
return redirect()->route('filament.property.pages.dashboard');
}
// ... the rest conditions for other departments.
return $next($request);
}
}
but any time i tried to login it kept loading and it shows This page isn’t working127.0.0.1 redirected you too many times
You need to figure out which redirect is causing too many redirects then
Okayyy But all the panels are showing same
Showing same what?
This is my middleware
RedirectUser
public function handle(Request $request, Closure $next): Response
{
$department = auth()->user()->department_id;
if ($department == 'hr' && Route::current()->getName() !== 'filament.hr.pages.dashboard') {
return redirect()->route('filament.hr.pages.dashboard');
}
return $next($request);
}
And all my panels I registered the middleware like this ->authMiddleware([
// Authenticate::class,
RedirectUser::class,
]);
I am currently in property/login
the user email is [email protected]
with password
and the department id registered is hr
when the user logged in it logs into property dashboard
instead of redirecting to hr
Maybe it's a better option to overwrite the Login classes and put the redirect there. Check the Login Livewire component and make your own, then register them via
->login()
on your panels.Yes I know how to overide the Login , the issue is I litrally don't know which of the logic should I implement , is it the redirrection or to have add a
select
which should allow the user to choose his deperment and if it tallys then it should open his database else if it's not his dashboard it should show an error that he cant view the dashboardSelect would require an extra step right? So best to redirect
This is my custom login but it is still not redirecting am i missing something
`
Looks fine to me. Sorry, I cannot really debug this from here. Check whether the redirect is not firing.
okay but how do i do that please
Solution
Use some debugging tools. Xdebug, Ray,
dd()
whatever. Check whether your condition applies and whether you reach the if statementSorry, this is not really the place to explain how to debug PHP code.
Great Thankyou for the helping hand
I used the
dd()
debug and i found out that i was actually mistaking integer for string , so it finally works . Thankyou Very MuchGreat! Hope you learned something and this helps you debug things in the future.
Yess 👍