1. Go to this page and download the library: Download vormkracht10/filament-2fa 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/ */
vormkracht10 / filament-2fa example snippets
use Vormkracht10\TwoFactorAuth\TwoFactorAuthPlugin;
// ...
->plugin(TwoFactorAuthPlugin::make())
class User extends Authenticatable implements FilamentUser
{
use HasApiTokens, HasFactory, Notifiable, TwoFactorAuthenticatable;
// ...
}
use Vormkracht10\TwoFactorAuth\Enums\TwoFactorType;
// ...
protected function casts(): array
{
return [
'two_factor_type' => TwoFactorType::class,
];
}
use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged;
use Laravel\Fortify\Events\TwoFactorAuthenticationEnabled;
use Vormkracht10\TwoFactorAuth\Listeners\SendTwoFactorCodeListener;
// ...
public function boot(): void
{
Event::listen([
TwoFactorAuthenticationChallenged::class,
TwoFactorAuthenticationEnabled::class
], SendTwoFactorCodeListener::class);
}
use Laravel\Fortify\Events\TwoFactorAuthenticationEnabled;
use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged;
use Vormkracht10\TwoFactorAuth\Listeners\SendTwoFactorCodeListener;
// ...
protected $listen = [
TwoFactorAuthenticationChallenged::class => [
SendTwoFactorCodeListener::class,
],
TwoFactorAuthenticationEnabled::class => [
SendTwoFactorCodeListener::class,
],
];
return [
'options' => [
TwoFactorType::authenticator,
TwoFactorType::email,
// TwoFactorType::phone,
],
'sms_service' => null, // For example 'vonage', 'twilio', 'nexmo', etc.
'send_otp_class' => null,
'phone_number_field' => 'phone', // The field name of the phone number in your user model
];
namespace App\Notifications;
use Vormkracht10\TwoFactorAuth\Notifications\SendOTP as NotificationsSendOTP;
use Illuminate\Notifications\Messages\VonageMessage;
class SendOTP extends NotificationsSendOTP
{
/**
* Get the Vonage / SMS representation of the notification.
*/
public function toVonage(mixed $notifiable): VonageMessage
{
return (new VonageMessage)
->content('Your OTP is: ' . $this->getTwoFactorCode($notifiable));
}
}