Tricks

Authorizing access to the admin panel with multiple email domains

Oct 9, 2022
mikrosmile
Admin panel, FAQ

Sometimes you may need to assign multiple email domains to access to Filament/Admin, if so, you can get access by this small trick.

Create an array of email addresses you need

<?php
 
namespace App\Models;
 
use Filament\Models\Contracts\FilamentUser;
use Illuminate\Foundation\Auth\User as Authenticatable;
 
class User extends Authenticatable implements FilamentUser
{
// ...
 
protected $emailList = [
'email1.com',
'email2.com'
];
 
// ...
 
}

Create a function which will split full email address to before @ and after

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

In function canAccessFilament() change to in_array() method

// ...
public function canAccessFilament(): bool
{
return in_array($this->getEmailDomain($this->email),$this->emailList);
}

That's all. Also, you can create a resource to control the domain list and retrieve such configuration dynamically from Db without hardcoding it in class.

No comments yet…