DEV Community

Cover image for Implementing Filament Auth Guard
yebor974 for Filament Mastery

Posted on • Originally published at filamentmastery.com on

Implementing Filament Auth Guard

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...
],

Enter fullscreen mode Exit fullscreen mode

Then, define the members provider under the providers section:

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

Enter fullscreen mode Exit fullscreen mode

Finally, configure the password reset flow for members:

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

Enter fullscreen mode Exit fullscreen mode

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');
            });
        });
    }
}

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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
    }
}

Enter fullscreen mode Exit fullscreen mode

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

Make sure to assign the member role during the user registration process, either with a listener or by extending the Register page.

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.

📬 Join the community on filamentmastery.com — it's free!

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.