F
Filament2y ago
Vp

Multiple panel login

I have three types of user namely 1. admin 2. agent 3. merchant I create multiple panel like /admin, /agent and /merchant I want the users who have user_type = agent only can login in /agent and so on, so 1. Do I need to create multiple custom login page (custom) for each different user_type 2. Can I create one login page (custom) and check base on user_type, if so, how can I know user_type = agent is trying to login using /agent only and block (throw error) if trying to login in /admin and so on Thanks in advance
13 Replies
Dennis Koch
Dennis Koch2y ago
Do I need to create multiple custom login page (custom) for each different user_type
You need multiple auth guards which you can set on the panel ->authGuard(). Check Laravel docs and your config/auth.php file.
Can I create one login page (custom) and check base on user_type, if so, how can I know user_type = agent is trying to login using /agent only and block (throw error) if trying to login in /admin and so on
You probably can create your own login page, disable all the logins on the panels, and redirect on user type.
Vp
VpOP2y ago
Okay, understood.. thanks
krekas
krekas2y ago
For the second you can just add your own loginresponse And make checks there
Dennis Koch
Dennis Koch2y ago
Yeah right. Good call.
josef
josef2y ago
Or just adapt canAccessPanel() in the user model and check for panel ID and user_type (that's how I do it)
Dennis Koch
Dennis Koch2y ago
Instead of auth guards? Yeah might be the easier way.
josef
josef2y ago
Yep 🙂 guards work too, but might be a bit overkill? Geschmacksfrage I guess 😉
krekas
krekas2y ago
yeah canAccessPanel() would be easier
SoraKeyheart
SoraKeyheart17mo ago
May you kindly share a code example of this?
krekas
krekas17mo ago
ain't even trying🤦‍♂️
public function canAccessPanel(Panel $panel): bool
{
if ($panel->getId() === 'admin') {
return $this->hasRole('super_admin');
}

if ($panel->getId() === 'accountant') {
return $this->hasRole('accountant');
}
}
public function canAccessPanel(Panel $panel): bool
{
if ($panel->getId() === 'admin') {
return $this->hasRole('super_admin');
}

if ($panel->getId() === 'accountant') {
return $this->hasRole('accountant');
}
}
SoraKeyheart
SoraKeyheart17mo ago
I did it like this:
protected $emailList = [
'kaec.net',
'hsse.co'
];

public function getEmailDomain($email){
$array = explode('@', $email);
return $array[1];
}

public function canAccessPanel(Panel $panel): bool
{
// Access to Admin panel
if ($panel->getId() === 'admin' && auth()->user()->hasAnyRole('super_admin', 'admin')) {
return in_array($this->getEmailDomain($this->email),$this->emailList);
}
}
protected $emailList = [
'kaec.net',
'hsse.co'
];

public function getEmailDomain($email){
$array = explode('@', $email);
return $array[1];
}

public function canAccessPanel(Panel $panel): bool
{
// Access to Admin panel
if ($panel->getId() === 'admin' && auth()->user()->hasAnyRole('super_admin', 'admin')) {
return in_array($this->getEmailDomain($this->email),$this->emailList);
}
}
Dennis Koch
Dennis Koch17mo ago
I hope you verify those emailadresses 😅

Did you find this page helpful?