PHPackages                             aurorawebsoftware/filament-2fa - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. aurorawebsoftware/filament-2fa

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

aurorawebsoftware/filament-2fa
==============================

This package helps you integrate Laravel Fortify with ease in your Filament apps.

v1.0.4(1y ago)135MITPHPPHP ^8.1

Since Jan 24Pushed 1y agoCompare

[ Source](https://github.com/AuroraWebSoftware/AFilament2fa)[ Packagist](https://packagist.org/packages/aurorawebsoftware/filament-2fa)[ Docs](https://github.com/aurorawebsoftware/filament-2fa)[ GitHub Sponsors](https://github.com/aurorawebsoftware)[ RSS](/packages/aurorawebsoftware-filament-2fa/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (14)Versions (8)Used By (0)

Filament 2FA plugin
===================

[](#filament-2fa-plugin)

Hi! We are a web development agency from Nijmegen in the Netherlands and we use Laravel for everything: advanced websites with a lot of bells and whitles and large web applications.

About the package
-----------------

[](#about-the-package)

This package adds Two Factor Authentication for your Laravel Filament app, using the first party package Laravel Fortify. We provide the views and logic to enable Two Factor Authentication (2FA) in your Filament app. Possible authentication methods are:

- Email
- SMS
- Authenticator app

Features and screenshots
------------------------

[](#features-and-screenshots)

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require aurorawebsoftware/filament-2fa
```

If you don't have [Laravel Fortify](https://laravel.com/docs/11.x/fortify) installed yet, you can install it by running the following commands:

```
composer require laravel/fortify
```

```
php artisan fortify:install
```

```
php artisan migrate
```

You can then easily install the plugin by running the following command:

```
php artisan filament-2fa:install
```

Note

If you used Laravel Fortify before, you probably already have users with 2FA enabled. In that case, you should let the install command set the default `two_factor_type` for existing users. Else you may run into issues.

Then add the plugin to your `PanelProvider`:

```
use AuroraWebSoftware\TwoFactorAuth\TwoFactorAuthPlugin;

// ...

->plugin(TwoFactorAuthPlugin::make())
```

Make sure your user uses the `TwoFactorAuthenticatable` trait:

```
class User extends Authenticatable implements FilamentUser
{
    use HasApiTokens, HasFactory, Notifiable, TwoFactorAuthenticatable;
    // ...
}
```

Also define the `two_factor_type` cast on your user model:

```
use AuroraWebSoftware\TwoFactorAuth\Enums\TwoFactorType;

// ...

protected function casts(): array
{
    return [
        'two_factor_type' => TwoFactorType::class,
    ];
}
```

Warning

When using `fillable` instead of `guarded` on your model, make sure to add `two_factor_type` to the `$fillable` array.

Also make sure to add the package files to your `vite.config.js` file:

```
// ...

export default defineConfig({
    plugins: [
        laravel({
            input: [
                // ...
            ],
            content: [
                "./vendor/aurorawebsoftware/filament-2fa/resources/**.*.blade.php",
            ],
            refresh: true,
        }),
    ],
});
```

### Register the event listener

[](#register-the-event-listener)

#### Laravel 11

[](#laravel-11)

In case you're using Laravel 11, you need to register the event listener in your `AppServiceProvider` boot method:

```
use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged;
use Laravel\Fortify\Events\TwoFactorAuthenticationEnabled;
use AuroraWebSoftware\TwoFactorAuth\Listeners\SendTwoFactorCodeListener;

// ...

public function boot(): void
{
    Event::listen([
        TwoFactorAuthenticationChallenged::class,
        TwoFactorAuthenticationEnabled::class
    ], SendTwoFactorCodeListener::class);
}
```

#### Laravel &lt; 11

[](#laravel--11)

In case you're not using Laravel 11 yet, you will probably need to manually register the event listener in your `EventServiceProvider`:

```
use Laravel\Fortify\Events\TwoFactorAuthenticationEnabled;
use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged;
use AuroraWebSoftware\TwoFactorAuth\Listeners\SendTwoFactorCodeListener;

// ...

protected $listen = [
    TwoFactorAuthenticationChallenged::class => [
        SendTwoFactorCodeListener::class,
    ],
    TwoFactorAuthenticationEnabled::class => [
        SendTwoFactorCodeListener::class,
    ],
];
```

If you want to customize the views (including email), you can publish them using the following command:

```
php artisan vendor:publish --tag=filament-2fa-views
```

Usage
-----

[](#usage)

### Configuration

[](#configuration)

The authentication methods can be configured in the `config/filament-2fa.php` file (which is published during the install command).

You can simply add or remove (comment) the methods you want to use:

```
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
];
```

If you want to use the SMS method, you need to provide an SMS service. You can check the [Laravel Notifications documentation](https://laravel-notification-channels.com/about/) for ready-to-use services.

#### Example with Vonage

[](#example-with-vonage)

Like the example in the [Laravel documentation](https://laravel.com/docs/11.x/notifications#formatting-sms-notifications) you need to create the `toVonage()` method in your notification class. That's why we recommend creating a custom notification class that extends the original `SendOTP` class from this package:

```
