How to Set Up Email Authentication in Filament Admin Panel

Set Up Email Authentication in Filament Admin Panel to keep your admin panel secure. By sending a unique code to the user’s email at every login, you ensure only authorized users can access your system. This adds an extra layer of protection while keeping the login process simple and user-friendly.

Setup Filament

If you haven’t already, install Filament v4 using Composer:

composer require filament/filament:"^4.0"
php artisan filament:install --panels

And you can also create a filament user using following command:

php artisan make:filament-user

Enable User Profile

First of all, we need to enable the user profile in our admin panel. To enable that, we just need to chain profile() in our admin panel configurations.

public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->profile();
}

Set Up Email Authentication in Filament Admin Panel

Each time a user attempts to log in, an authentication code is sent to their email. Whenever users try to sign in, they receive a verification code in their email. To enable this feature, you must need to add a database column to determine wether or not email authentication is enabled.


Schema::table('users', function (Blueprint $table) {
    $table->boolean('has_email_authentication')->default(false);
});

// Above column determines weather or not email authentication is enabled for the users.

After that, we should implement the HasEmailAuthentication interface on User model.

class User extends Authenticatable implements FilamentUser, HasEmailAuthentication, MustVerifyEmail
{
    public function hasEmailAuthentication(): bool
    {
        // This method should return true if the user has enabled email authentication.
        
        return $this->has_email_authentication; //rename with your column
    }

    public function toggleEmailAuthentication(bool $condition): void
    {
        // This method should save whether or not the user has enabled email authentication.
    
        $this->has_email_authentication = $condition;
        $this->save();
    }

}

Finally, to activate this feature in our panel, we need to add:


public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->multiFactorAuthentication([
            EmailAuthentication::make(),
        ]);
}

Note: you can chain codeExpiryMinutes(2) method to set expiration time. In this way, we can Set Up Email Authentication in Filament Admin Panel

Read the official docs.

Check out more filament examples.

Leave a Reply

Your email address will not be published. Required fields are marked *