stephenjude/filament-two-factor-authentication
Install the Package
composer require stephenjude/filament-two-factor-authentication
Publish the migration and config:
php artisan vendor:publish --provider="StephenJude\FilamentTwoFactorAuthentication\FilamentTwoFactorAuthenticationServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="StephenJude\FilamentTwoFactorAuthentication\FilamentTwoFactorAuthenticationServiceProvider" --tag="config"
php artisan migrate
Register the Plugin
Add to app/Providers/Filament/AdminPanelProvider.php:
public function panel(Panel $panel): Panel
{
return $panel
->plugins([
\StephenJude\FilamentTwoFactorAuthentication\FilamentTwoFactorAuthenticationPlugin::make(),
]);
}
First Use Case: Enable 2FA for a User
Enabling 2FA for New Users
EnableTwoFactorAuthentication action in Filament’s user table.configure() method:
FilamentTwoFactorAuthenticationPlugin::make()
->enableForAllUsers() // Optional: Auto-enable for all users
->showOnNavigation() // Optional: Add to Filament sidebar
Passkey Authentication Flow
Passkey model events (created, verified) for custom logic:
\StephenJude\FilamentTwoFactorAuthentication\Models\Passkey::created(function ($passkey) {
// Log passkey creation or notify admins
});
Conditional 2FA Enforcement
public function check(): bool
{
return auth()->user()->hasTwoFactorEnabled();
}
Customizing the 2FA UI
php artisan vendor:publish --tag="filament-two-factor-authentication-views"
TwoFactorAuthPage class to modify behavior:
use StephenJude\FilamentTwoFactorAuthentication\Pages\TwoFactorAuthPage;
class CustomTwoFactorAuthPage extends TwoFactorAuthPage
{
protected static ?string $navigationIcon = 'heroicon-o-shield-check';
protected static string $navigationLabel = 'Custom 2FA';
}
Integrating with Laravel 13
config/app.php includes Laravel 13’s updated service providers and aliases.app()->runningInConsole() changes):
if (app()->runningInConsole()) {
// Laravel 13-specific console logic
}
Integrating with Existing Auth Logic
two-factor-authenticated event to trigger post-2FA actions:
\StephenJude\FilamentTwoFactorAuthentication\Events\TwoFactorAuthenticated::listen(function ($user) {
// Grant additional permissions or log the event
});
Migration Conflicts
users table, ensure the two_factor_secret and two_factor_recovery_codes columns exist. Override the migration:
use StephenJude\FilamentTwoFactorAuthentication\Database\Migrations\CreateTwoFactorAuthenticationTables;
class CustomMigration extends CreateTwoFactorAuthenticationTables
{
public function up()
{
$this->schema->table('users', function (Blueprint $table) {
$table->string('two_factor_secret')->nullable()->after('password');
$table->json('two_factor_recovery_codes')->nullable()->after('two_factor_secret');
});
}
}
Passkey Browser Support
if (!\StephenJude\FilamentTwoFactorAuthentication\Support\Passkey::isSupported()) {
// Fallback to TOTP or notify users
}
Recovery Codes Management
two_factor_recovery_codes column. To regenerate them:
$user->regenerateTwoFactorRecoveryCodes();
$user->save();
Laravel 13 Compatibility
bootstrap/app.php is updated to Laravel 13’s new structure:
// bootstrap/app.php
$app->bind(\Illuminate\Contracts\Http\Kernel::class, \App\Http\Kernel::class);
Caching Issues
php artisan filament:cache:clear
config/filament-two-factor-authentication.php:
'debug' => env('APP_DEBUG', false),
laravel-debugbar package to inspect the two_factor_secret during setup.$user->verifyTwoFactorRecoveryCode('123456'); // Replace with a valid code
Custom Authenticator Providers
Authenticator interface to support additional providers (e.g., Authy):
use StephenJude\FilamentTwoFactorAuthentication\Contracts\Authenticator;
class AuthyAuthenticator implements Authenticator
{
public function verify($secret, $code): bool
{
// Custom Authy verification logic
}
}
config/filament-two-factor-authentication.php:
'authenticators' => [
'google' => \StephenJude\FilamentTwoFactorAuthentication\Authenticators\GoogleAuthenticator::class,
'authy' => \App\Authenticators\AuthyAuthenticator::class,
],
Webhook Events
\StephenJude\FilamentTwoFactorAuthentication\Events\TwoFactorEnabled::listen(function ($user) {
// Send Slack notification or update CRM
});
Rate Limiting
app/Providers/AppServiceProvider.php:
use Illuminate\Support\Facades\RateLimiter;
public function boot()
{
RateLimiter::for('two-factor-attempts', function ($request) {
return Limit::perMinute(5)->by($request->user()->id);
});
}
Laravel 13-Specific Features
// Example: Adding a macro to the User model for 2FA checks
\App\Models\User::macro('isTwoFactorVerified', function () {
return $this->two_factor_confirmed_at !== null;
});
How can I help you explore Laravel packages today?