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:
Use some debugging tools. Xdebug, Ray, dd() whatever. Check whether your condition applies and whether you reach the if statement
Jump to solution
19 Replies
Dennis Koch
Dennis Koch9mo ago
You can add a middleware that checks the current panel and redirects them if it's the wrong one
sadiqgoni13
sadiqgoni139mo ago
Please can you guide me through that
Dennis Koch
Dennis Koch9mo ago
- 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
sadiqgoni13
sadiqgoni139mo ago
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
Dennis Koch
Dennis Koch9mo ago
You need to figure out which redirect is causing too many redirects then
sadiqgoni13
sadiqgoni139mo ago
Okayyy But all the panels are showing same
Dennis Koch
Dennis Koch9mo ago
Showing same what?
sadiqgoni13
sadiqgoni139mo ago
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 test1@test.com with password and the department id registered is hr when the user logged in it logs into property dashboard instead of redirecting to hr
Dennis Koch
Dennis Koch9mo ago
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.
sadiqgoni13
sadiqgoni139mo ago
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 dashboard
Dennis Koch
Dennis Koch9mo ago
Select would require an extra step right? So best to redirect
sadiqgoni13
sadiqgoni139mo ago
This is my custom login but it is still not redirecting am i missing something
class Login extends BasePage
{
public function authenticate(): ?LoginResponse
{
try {
$this->rateLimit(5);
} catch (TooManyRequestsException $exception) {
Notification::make()
->title(__('filament-panels::pages/auth/login.notifications.throttled.title', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]))
->body(array_key_exists('body', __('filament-panels::pages/auth/login.notifications.throttled') ?: []) ? __('filament-panels::pages/auth/login.notifications.throttled.body', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]) : null)
->danger()
->send();

return null;
}

$data = $this->form->getState();

if (! Filament::auth()->attempt($this->getCredentialsFromFormData($data), $data['remember'] ?? false)) {
$this->throwFailureValidationException();
}

$user = Filament::auth()->user();

if (
($user instanceof FilamentUser) &&
(! $user->canAccessPanel(Filament::getCurrentPanel()))
) {
Filament::auth()->logout();

$this->throwFailureValidationException();
}
session()->regenerate();

$role = $user->department_id;
$panel = filament()->getCurrentPanel()->getId();

if ($role === 'hr' && $panel !== 'hr') {
return redirect()->route('filament.hr.pages.dashboard');
}
return app(LoginResponse::class);
}

}
class Login extends BasePage
{
public function authenticate(): ?LoginResponse
{
try {
$this->rateLimit(5);
} catch (TooManyRequestsException $exception) {
Notification::make()
->title(__('filament-panels::pages/auth/login.notifications.throttled.title', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]))
->body(array_key_exists('body', __('filament-panels::pages/auth/login.notifications.throttled') ?: []) ? __('filament-panels::pages/auth/login.notifications.throttled.body', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]) : null)
->danger()
->send();

return null;
}

$data = $this->form->getState();

if (! Filament::auth()->attempt($this->getCredentialsFromFormData($data), $data['remember'] ?? false)) {
$this->throwFailureValidationException();
}

$user = Filament::auth()->user();

if (
($user instanceof FilamentUser) &&
(! $user->canAccessPanel(Filament::getCurrentPanel()))
) {
Filament::auth()->logout();

$this->throwFailureValidationException();
}
session()->regenerate();

$role = $user->department_id;
$panel = filament()->getCurrentPanel()->getId();

if ($role === 'hr' && $panel !== 'hr') {
return redirect()->route('filament.hr.pages.dashboard');
}
return app(LoginResponse::class);
}

}
`
Dennis Koch
Dennis Koch9mo ago
Looks fine to me. Sorry, I cannot really debug this from here. Check whether the redirect is not firing.
sadiqgoni13
sadiqgoni139mo ago
okay but how do i do that please
Solution
Dennis Koch
Dennis Koch9mo ago
Use some debugging tools. Xdebug, Ray, dd() whatever. Check whether your condition applies and whether you reach the if statement
Dennis Koch
Dennis Koch9mo ago
Sorry, this is not really the place to explain how to debug PHP code.
sadiqgoni13
sadiqgoni139mo ago
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 Much
Dennis Koch
Dennis Koch9mo ago
Great! Hope you learned something and this helps you debug things in the future.
sadiqgoni13
sadiqgoni139mo ago
Yess 👍