F
Filament4mo ago
jabs

Multiple Panels, 1 Login Screen

Hi, I am building filament v3 app that has multiple panels. Instead of having different login urls, i want to have 1 login url, and based on the user_type (stored in the users table) it needs to redirect to the correct panel. I tried creating a seperate panel, which seems to kind of work, but I cant get the redirection to work. Anyone that can push me into the right direction? Or is there a better practice? I tried overriding the LoginResponse with a custom one. But also doesnt seem to work.
5 Replies
GavTheDev
GavTheDev4mo ago
I would like to know also 😎
Tieme
Tieme4mo ago
@jabs @GavTheDev I have a setup like this, and use below code. Don't think it is the best code solution, but it works for me till i see a better method for doing this. Helper Function
public static function getPanelDashboardUrlFromUser()
{
if (Filament::auth()->check()) {

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

$panelAdmin = Filament::getPanel('admin');
$panelOther = Filament::getPanel('other');

if ($user->canAccessPanel($panelAdmin)) {
return route('filament.admin.pages.dashboard');
} elseif ($user->canAccessPanel($panelOther)) {
return route('filament.other.pages.dashboard');
} else {
return null;
}
}

return null;
}
public static function getPanelDashboardUrlFromUser()
{
if (Filament::auth()->check()) {

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

$panelAdmin = Filament::getPanel('admin');
$panelOther = Filament::getPanel('other');

if ($user->canAccessPanel($panelAdmin)) {
return route('filament.admin.pages.dashboard');
} elseif ($user->canAccessPanel($panelOther)) {
return route('filament.other.pages.dashboard');
} else {
return null;
}
}

return null;
}
Default Panel (also login panel)
return $panel
->id('main')
->path('app')
->login()
return $panel
->id('main')
->path('app')
->login()
Other Panel.
return $panel
->id('other')
->path('other')
->login(fn () => redirect(Filament::getPanel('main')->login()->getUrl()))
return $panel
->id('other')
->path('other')
->login(fn () => redirect(Filament::getPanel('main')->login()->getUrl()))
Admin Panel.
return $panel
->id('admin')
->path('admin')
->login(fn () => redirect(Filament::getPanel('main')->login()->getUrl()))
return $panel
->id('admin')
->path('admin')
->login(fn () => redirect(Filament::getPanel('main')->login()->getUrl()))
Class LoginResponse
class LoginResponse implements Responsable
{
public function toResponse($request): RedirectResponse|Redirector
{
$panelRedirct = Helpers::getPanelDashboardUrlFromUser();
if ($panelRedirct != null) {
return redirect()->to($panelRedirct);
}

return redirect()->intended(Filament::getUrl());
}
}
class LoginResponse implements Responsable
{
public function toResponse($request): RedirectResponse|Redirector
{
$panelRedirct = Helpers::getPanelDashboardUrlFromUser();
if ($panelRedirct != null) {
return redirect()->to($panelRedirct);
}

return redirect()->intended(Filament::getUrl());
}
}
Main Route
Route::get('/', function () {

if (Filament::auth()->check()) {
$panelRedirct = Helpers::getPanelDashboardUrlFromUser();
if ($panelRedirct != null) {
return redirect()->to($panelRedirct);
}
}

return redirect('app');
});
Route::get('/', function () {

if (Filament::auth()->check()) {
$panelRedirct = Helpers::getPanelDashboardUrlFromUser();
if ($panelRedirct != null) {
return redirect()->to($panelRedirct);
}
}

return redirect('app');
});
GavTheDev
GavTheDev4mo ago
Top! @Tieme
Tieme
Tieme4mo ago
And the logic for your own canAccessPanel in de model. I know it works with Tenacy on the main panel, dont know if this works if you have tenacy on the other panels. Als if you have acces to multiple panels, i use the plugin https://filamentphp.com/plugins/bezhansalleh-panel-switch to add a button so you can switch between them without changing the url.
jabs
jabs4mo ago
Thank you @Tieme !