Shared panel session

I want my AdminPanel to be the default one. and ClinicPanel should no be accessible not unless if someone logs in as admin, If the admin logs in with no SUPER_ADMIN role should be redirected and use the ClinicPanel
No description
No description
No description
4 Replies
rominronin
rominronin3w ago
Did you find a way to make this work?
IamJohnDev
IamJohnDev3w ago
Did you try to use the canUsePanel() from User model?
Alexandre
Alexandre2w ago
👋 I've similar case in my project (one panel for Users and another for admin and super_admin) and I wanted to use the same login form. Here's how I did it (it may not be the best way, but it works 😅) I use the method canAccessPanel in my User Model like this :
public function canAccessPanel(Panel $panel): bool
{
if ($panel->getId() === 'admin') {
return $this->hasAnyRole(['super_admin', 'admin']);
}

if ($panel->getId() === 'user') {
return $this->hasRole(['user']);
}

return false;
}
public function canAccessPanel(Panel $panel): bool
{
if ($panel->getId() === 'admin') {
return $this->hasAnyRole(['super_admin', 'admin']);
}

if ($panel->getId() === 'user') {
return $this->hasRole(['user']);
}

return false;
}
And in my web.php route file I've made a little change for the base URL :
Route::get('/', function () {
if (Filament::auth()->check()) {
$panelRedirect = Helpers::getPanelDashboardUrlFromUser();
if ($panelRedirect != null) {
return redirect()->to($panelRedirect);
}
}

return redirect()->to('/user/login');
})->name('home');

Route::get('/admin/login', function () {
return redirect()->to('/user/login');
})->name('filament.admin.auth.login');
Route::get('/', function () {
if (Filament::auth()->check()) {
$panelRedirect = Helpers::getPanelDashboardUrlFromUser();
if ($panelRedirect != null) {
return redirect()->to($panelRedirect);
}
}

return redirect()->to('/user/login');
})->name('home');

Route::get('/admin/login', function () {
return redirect()->to('/user/login');
})->name('filament.admin.auth.login');
And I've made a little helper to manage the redirection :
public static function getPanelDashboardUrlFromUser(): ?string
{
if (Filament::auth()->check()) {

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

$panelAdmin = Filament::getPanel('admin');
$panelUser = Filament::getPanel('user');

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

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

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

$panelAdmin = Filament::getPanel('admin');
$panelUser = Filament::getPanel('user');

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

return null;
}
So, from the same form and according to your role, you're directed to the right panel. 👍
ronssij
ronssij7d ago
Hi guys sorry for the late reply on this thread. In my case both are admin users but they vary on roles. What I did was the AdminPanelProvider will be the default provider and login() is enable on the panel. But on the ClinicPanelProvider the login() is not enabled. I added middleware for the admin panel AdminAccessMiddleware::class and ClinicAccessMiddleware::class for the clinic panel. so whenever a a users with a clinic admin roles logs in. the panel is smart enough to redirect them on the clinic panel which also handles the tenancy. Panel should share the same session if you use the same auth guard which in my case is.
"cms_admin" => [
"driver" => "session",
"provider" => "cms_admins",
],
"cms_admin" => [
"driver" => "session",
"provider" => "cms_admins",
],
Want results from more Discord servers?
Add your server
More Posts