· yebor974 · Advanced Techniques, Tutorials, Integration Guides

Implementing Filament Auth Guard

Learn how to configure multi auth guards in Filament by creating a Member panel with a custom guard, provider, and password reset flow, linked to a Member Model

Implementing Filament Auth Guard - picture

Filament is highly flexible when it comes to authentication, making it easy to set up panels for different user groups with distinct authentication flows. In this article, we will walk through creating a Member panel with a custom member guard, tied to a Member model that inherits from User. This setup leverages Laravel Spatie Permissions for role management and includes defining the guard, provider, and password reset configuration in config/auth.php.

Defining the Guard in config/auth.php

In your config/auth.php file, add the member guard under the guards section:

'guards' => [
    'member' => [
        'driver' => 'session',
        'provider' => 'members',
    ],
    // other guards...
],

Then, define the members provider under the providers section:

'providers' => [
    'members' => [
        'driver' => 'eloquent',
        'model' => App\Models\Member::class,
    ],
    // other providers...
],

Finally, configure the password reset flow for members:

'passwords' => [
    'members' => [
        'provider' => 'members',
        'table' => 'password_reset_tokens',
        'expire' => 60,
        'throttle' => 60,
    ],
    // other password configurations...
],

Creating the Member Model

The Member model will inherit from the User model and apply a global scope to restrict it to users with the member role. We rely on Laravel Spatie Permissions to manage roles and permissions for this setup.

Here’s an example with using users table:

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;

class Member extends User
{
    protected $table = 'users';

    protected static function booted(): void
    {
        static::addGlobalScope('is_member', function (Builder $builder) {
            $builder->whereHas('roles', function ($query) {
                $query->where('name', 'member');
            });
        });
    }
}

This global scope ensures all Member model queries are limited to users with the member role.

Configuring the Member Panel

First, create your Member panel:

php artisan make:filament-panel member

Edit the MemberPanelProvider file generated in app/Providers/Filament/:

namespace App\Providers\Filament;

use Filament\PanelProvider;
use Filament\Facades\Filament;

class MemberPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->authGuard('member') // Use the custom guard
            ->login() // Enable login
            ->registration() // Enable user registration
            ->authPasswordBroker('members') // Configure the password reset broker
            ->passwordReset(); // Enable password reset
    }
}

This ensures that the Member panel is fully functional with custom guard and password reset configurations.

Conclusion

By implementing a multi auth guard in Filament with using Laravel Spatie Permissions for role management, you can create a dedicated panel for members with tailored authentication and password reset functionality. This approach ensures that only users with the member role can access the Member panel while keeping your authentication flow streamlined.

For additional customization, you can expand this setup to include permissions, custom middleware, or even advanced multi-tenancy configurations.

Avatar of yebor974
yebor974
Freelance - French IT Engineer passionate about Laravel and Filament PHP, creating innovative solutions to simplify web development.
React
Share post

Stay ahead in the Filament Mastery adventure

Join our community of Filament PHP enthusiasts! Get exclusive tips, tutorials, and updates delivered straight to your inbox.