Installation:
composer require tfsthiagobr98/filament-2fa
php artisan vendor:publish --tag="filament-2fa-migrations"
php artisan migrate
Ensure filament >= 2.10.40 is installed.
User Model Setup: Add the trait to your user model:
use TFSThiagoBR98\FilamentTwoFactor\TwoFactorAuthenticatable;
class User extends Authenticatable { use TwoFactorAuthenticatable; }
Configure Filament:
Update config/filament.php to use the custom login page:
"auth" => [
"pages" => [
"login" => \TFSThiagoBR98\FilamentTwoFactor\Http\Livewire\Auth\Login::class,
],
],
First Use Case:
Login Flow:
hasTwoFactorEnabled()).2FA Setup:
<livewire:filament-two-factor-form> in a profile page (e.g., Jetstream theme).two_factor_backup_codes).<livewire:filament-two-factor-form
:user="$user"
wire:ignore
/>
Recovery Codes:
$recoveryCodes = $user->generateTwoFactorRecoveryCodes();
Middleware Integration:
TwoFactorAuthenticatable trait methods:
if ($user->hasTwoFactorEnabled()) {
$this->middleware('two-factor.auth');
}
Custom TOTP Providers:
Extend the TwoFactorAuthenticatable trait to support alternative providers (e.g., YubiKey):
use PragmaRX\Google2FA\Google2FA as Google2FA;
use PragmaRX\Google2FA\TwoFactorAuth;
protected function twoFactor(): TwoFactorAuth {
return new TwoFactorAuth(new Google2FA());
}
Conditional 2FA:
Skip 2FA for specific roles/IPs by overriding shouldUseTwoFactor():
public function shouldUseTwoFactor(): bool {
return $this->role === 'admin' && !request()->ip()->isLocal();
}
Event Hooks:
Listen for 2FA events (e.g., TwoFactorEnabled, TwoFactorDisabled) to log or notify:
event(new TwoFactorEnabled($user));
Migration Conflicts:
two_factor_secret and two_factor_backup_codes columns may conflict with existing migrations.php artisan vendor:publish --tag="filament-2fa-migrations" before other migrations.Caching Issues:
php artisan cache:clear
php artisan config:clear
Backup Code Security:
getTwoFactorBackupCodesAttribute() to encrypt them:
public function getTwoFactorBackupCodesAttribute($value) {
return encrypt($value);
}
Time Sync:
Filament Version Lock:
filament >= 2.10.40. Downgrading may break functionality.Verify TOTP Codes:
Use the verifyTwoFactorCode() method to debug:
$valid = $user->verifyTwoFactorCode('123456');
// Logs: `TwoFactorCodeVerified` or `TwoFactorCodeInvalid`
Check Enrollment:
Ensure hasTwoFactorEnabled() returns true after setup. If not, check:
two_factor_secret column is populated.Livewire Errors:
Wrap <livewire:filament-two-factor-form> in a @error block:
@error('two_factor_code')
<div class="text-red-500">{{ $message }}</div>
@enderror
Custom Views: Publish and override views:
php artisan vendor:publish --tag="filament-2fa-views"
Modify resources/views/vendor/filament-2fa/... to change UI (e.g., QR size, button labels).
Email Notifications:
Extend the TwoFactorEnabled event to send emails:
TwoFactorEnabled::listen(function ($user) {
Notification::route('mail', $user->email)
->notify(new TwoFactorEnabledNotification());
});
Rate Limiting:
Add rate limiting to the TOTP endpoint in app/Http/Middleware/ThrottleNodes.php:
protected function limits() {
return [
'two-factor' => ['max', 5, 1], // 5 attempts per minute
];
}
Multi-Factor Logic:
Combine with other auth packages (e.g., laravel-fortify) by extending the Login controller:
public function authenticate() {
if ($this->attemptLogin()) {
if ($this->user->hasTwoFactorEnabled()) {
return redirect()->route('two-factor.verify');
}
return redirect()->intended();
}
}
How can I help you explore Laravel packages today?