PHP code example of asubodh / filament-two-factor-auth
1. Go to this page and download the library: Download asubodh/filament-two-factor-auth library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
asubodh / filament-two-factor-auth example snippets
namespace App\Models;
use Asubodh\FilamentTwoFactorAuth\Contracts\TwoFactorAuthenticatable;
use Asubodh\FilamentTwoFactorAuth\Traits\HasTwoFactorAuth;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements TwoFactorAuthenticatable
{
use HasTwoFactorAuth;
// ... your existing code
}
namespace App\Providers\Filament;
use Asubodh\FilamentTwoFactorAuth\TwoFactorPlugin;
use Filament\Panel;
use Filament\PanelProvider;
class AdminPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
->default()
->id('admin')
->path('admin')
->login()
// ...
->plugin(
TwoFactorPlugin::make()
->showInNavigation(true)
->showInProfileMenu(true)
);
}
}
->plugin(
TwoFactorPlugin::make()
// The name displayed in the user's authenticator app
->issuer('My Amazing SaaS')
// Force all users to set up 2FA before accessing the panel.
// If they haven't set it up, they will be redirected to the setup page.
->enforceForAllUsers(true)
// Enable "remember this device" functionality (default: false)
->rememberDevice(enabled: true, days: 30)
// Navigation options
->showInNavigation(true)
->showInProfileMenu(true)
->navigationIcon('heroicon-o-shield-check')
->navigationGroup('Security')
->navigationSort(100)
)
namespace App\Listeners;
use Asubodh\FilamentTwoFactorAuth\Events\TwoFactorDisabled;
use Asubodh\FilamentTwoFactorAuth\Events\TwoFactorEnabled;
use Asubodh\FilamentTwoFactorAuth\Events\TwoFactorFailed;
use Asubodh\FilamentTwoFactorAuth\Events\TwoFactorVerified;
use Illuminate\Support\Facades\Log;
class TwoFactorEventSubscriber
{
public function handleTwoFactorEnabled(TwoFactorEnabled $event): void
{
Log::info("User ID {$event->user->id} enabled 2FA.");
}
public function handleTwoFactorDisabled(TwoFactorDisabled $event): void
{
Log::warning("User ID {$event->user->id} disabled 2FA.");
}
public function handleTwoFactorVerified(TwoFactorVerified $event): void
{
Log::info("User ID {$event->user->id} successfully passed the 2FA challenge.");
}
public function handleTwoFactorFailed(TwoFactorFailed $event): void
{
Log::alert("User ID {$event->user->id} failed 2FA challenge. Reason: {$event->reason}");
}
}