{"id":19756408,"url":"https://github.com/vormkracht10/filament-2fa","last_synced_at":"2025-04-07T12:01:50.248Z","repository":{"id":253404918,"uuid":"843293161","full_name":"vormkracht10/filament-2fa","owner":"vormkracht10","description":"Integrate Laravel Fortify easily in your Filament apps. ","archived":false,"fork":false,"pushed_at":"2024-10-22T13:40:31.000Z","size":1020,"stargazers_count":39,"open_issues_count":5,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-10-25T02:44:11.841Z","etag":null,"topics":["2fa","2fa-security","2factor","filament","filament-plugin","filamentphp","filamentphp-plugin","fortify","laravel","laravel-fortify","laravel-package"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vormkracht10.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"vormkracht10"}},"created_at":"2024-08-16T07:40:08.000Z","updated_at":"2024-10-24T16:24:43.000Z","dependencies_parsed_at":"2024-08-27T15:58:55.359Z","dependency_job_id":"4ddeca0b-5164-43c6-a144-99c959176e5e","html_url":"https://github.com/vormkracht10/filament-2fa","commit_stats":null,"previous_names":["vormkracht10/filament-two-factor-auth","vormkracht10/filament-2fa"],"tags_count":66,"template":false,"template_full_name":"filamentphp/plugin-skeleton","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vormkracht10%2Ffilament-2fa","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vormkracht10%2Ffilament-2fa/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vormkracht10%2Ffilament-2fa/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vormkracht10%2Ffilament-2fa/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vormkracht10","download_url":"https://codeload.github.com/vormkracht10/filament-2fa/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247648874,"owners_count":20972944,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["2fa","2fa-security","2factor","filament","filament-plugin","filamentphp","filamentphp-plugin","fortify","laravel","laravel-fortify","laravel-package"],"created_at":"2024-11-12T03:15:49.822Z","updated_at":"2025-04-07T12:01:50.215Z","avatar_url":"https://github.com/vormkracht10.png","language":"PHP","funding_links":["https://github.com/sponsors/vormkracht10"],"categories":[],"sub_categories":[],"readme":"# Filament 2FA plugin\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/vormkracht10/filament-2fa.svg?style=flat-square)](https://packagist.org/packages/vormkracht10/filament-2fa)\n[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/vormkracht10/filament-2fa/run-tests.yml?branch=main\u0026label=tests\u0026style=flat-square)](https://github.com/vormkracht10/filament-2fa/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![PHPStan](https://img.shields.io/github/actions/workflow/status/vormkracht10/filament-2fa/phpstan.yml?branch=main\u0026label=phpstan\u0026style=flat-square)](https://github.com/vormkracht10/filament-2fa/actions/workflows/phpstan.yml)\n[![Total Downloads](https://img.shields.io/packagist/dt/vormkracht10/filament-2fa.svg?style=flat-square)](https://packagist.org/packages/vormkracht10/filament-2fa)\n\n## Nice to meet you, we're [Vormkracht10](https://vormkracht10.nl)\n\nHi! 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.\n\n## About the package\n\nThis 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:\n\n-   Email\n-   SMS\n-   Authenticator app\n\n## Features and screenshots\n\n### Enable Two Factor Authentication (2FA)\n\n![Enable Two Factor Authentication (2FA)](https://raw.githubusercontent.com/vormkracht10/filament-2fa/main/docs/two-factor-page.png)\n\n### Using authenticator app as two factor method\n\n![Authenticator app](https://raw.githubusercontent.com/vormkracht10/filament-2fa/main/docs/authenticator-app.png)\n\n### Using email or SMS as two factor method\n\n![Email or SMS](https://raw.githubusercontent.com/vormkracht10/filament-2fa/main/docs/email-or-sms.png)\n\n### Recovery codes\n\n![Recovery codes](https://raw.githubusercontent.com/vormkracht10/filament-2fa/main/docs/recovery-codes.png)\n\n### Two Factor authentication challenge\n\n![Two Factor challenge](https://raw.githubusercontent.com/vormkracht10/filament-2fa/main/docs/code-challenge.png)\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require vormkracht10/filament-2fa\n```\n\nIf you don't have [Laravel Fortify](https://laravel.com/docs/11.x/fortify) installed yet, you can install it by running the following commands:\n\n```bash\ncomposer require laravel/fortify\n```\n\n```bash\nphp artisan fortify:install\n```\n\n```bash\nphp artisan migrate\n```\n\nYou can then easily install the plugin by running the following command:\n\n```bash\nphp artisan filament-2fa:install\n```\n\n\u003e [!NOTE]\n\u003e 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.\n\nThen add the plugin to your `PanelProvider`:\n\n```php\nuse Vormkracht10\\TwoFactorAuth\\TwoFactorAuthPlugin;\n\n// ...\n\n-\u003eplugin(TwoFactorAuthPlugin::make())\n```\n\nMake sure your user uses the `TwoFactorAuthenticatable` trait:\n\n```php\nclass User extends Authenticatable implements FilamentUser\n{\n    use HasApiTokens, HasFactory, Notifiable, TwoFactorAuthenticatable;\n    // ...\n}\n```\n\nAlso define the `two_factor_type` cast on your user model:\n\n```php\nuse Vormkracht10\\TwoFactorAuth\\Enums\\TwoFactorType;\n\n// ...\n\nprotected function casts(): array\n{\n    return [\n        'two_factor_type' =\u003e TwoFactorType::class,\n    ];\n}\n```\n\n\u003e [!WARNING]\n\u003e When using `fillable` instead of `guarded` on your model, make sure to add `two_factor_type` to the `$fillable` array.\n\nAlso make sure to add the package files to your `vite.config.js` file:\n\n```js\n// ...\n\nexport default defineConfig({\n    plugins: [\n        laravel({\n            input: [\n                // ...\n            ],\n            content: [\n                \"./vendor/vormkracht10/filament-2fa/resources/**.*.blade.php\",\n            ],\n            refresh: true,\n        }),\n    ],\n});\n```\n\n### Register the event listener\n\n#### Laravel 11\n\nIn case you're using Laravel 11, you need to register the event listener in your `AppServiceProvider` boot method:\n\n```php\nuse Laravel\\Fortify\\Events\\TwoFactorAuthenticationChallenged;\nuse Laravel\\Fortify\\Events\\TwoFactorAuthenticationEnabled;\nuse Vormkracht10\\TwoFactorAuth\\Listeners\\SendTwoFactorCodeListener;\n\n// ...\n\npublic function boot(): void\n{\n    Event::listen([\n        TwoFactorAuthenticationChallenged::class,\n        TwoFactorAuthenticationEnabled::class\n    ], SendTwoFactorCodeListener::class);\n}\n```\n\n#### Laravel \u003c 11\n\nIn case you're not using Laravel 11 yet, you will probably need to manually register the event listener in your `EventServiceProvider`:\n\n```php\nuse Laravel\\Fortify\\Events\\TwoFactorAuthenticationEnabled;\nuse Laravel\\Fortify\\Events\\TwoFactorAuthenticationChallenged;\nuse Vormkracht10\\TwoFactorAuth\\Listeners\\SendTwoFactorCodeListener;\n\n// ...\n\nprotected $listen = [\n    TwoFactorAuthenticationChallenged::class =\u003e [\n        SendTwoFactorCodeListener::class,\n    ],\n    TwoFactorAuthenticationEnabled::class =\u003e [\n        SendTwoFactorCodeListener::class,\n    ],\n];\n```\n\nIf you want to customize the views (including email), you can publish them using the following command:\n\n```bash\nphp artisan vendor:publish --tag=filament-2fa-views\n```\n\n## Usage\n\n### Configuration\n\nThe authentication methods can be configured in the `config/filament-2fa.php` file (which is published during the install command).\n\nYou can simply add or remove (comment) the methods you want to use:\n\n```php\nreturn [\n    'options' =\u003e [\n        TwoFactorType::authenticator,\n        TwoFactorType::email,\n        // TwoFactorType::phone,\n    ],\n\n    'sms_service' =\u003e null, // For example 'vonage', 'twilio', 'nexmo', etc.\n    'send_otp_class' =\u003e null,\n    'phone_number_field' =\u003e 'phone', // The field name of the phone number in your user model\n];\n```\n\nIf 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.\n\n#### Example with Vonage\n\nLike 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:\n\n```php\n\u003c?php\n\nnamespace App\\Notifications;\n\nuse Vormkracht10\\TwoFactorAuth\\Notifications\\SendOTP as NotificationsSendOTP;\nuse Illuminate\\Notifications\\Messages\\VonageMessage;\n\nclass SendOTP extends NotificationsSendOTP\n{\n    /**\n     * Get the Vonage / SMS representation of the notification.\n     */\n    public function toVonage(mixed $notifiable): VonageMessage\n    {\n        return (new VonageMessage)\n            -\u003econtent('Your OTP is: ' . $this-\u003egetTwoFactorCode($notifiable));\n    }\n}\n```\n\nYou can get the two factor code for the user by calling the `getTwoFactorCode` method on the notification class.\n\nThen you need to set the `send_otp_class` in the `config/filament-2fa.php` file:\n\n```php\nreturn [\n    // ...\n\n    'sms_service' =\u003e 'vonage',\n    'send_otp_class' =\u003e App\\Notifications\\SendOTP::class,\n];\n```\n\n\u003e [!NOTE]\n\u003e Make sure your user or notifiable model has a `routeNotificationForVonage` method that returns the phone number. Please check the documentation of the SMS service you're using for more information.\n\n### Customization\n\nIf you want to fully customize the pages, you can override the classes in the `config/filament-2fa.php` file:\n\n```php\nreturn [\n    // ...\n\n    'login' =\u003e Login::class,\n    'register' =\u003e Register::class,\n    'challenge' =\u003e LoginTwoFactor::class,\n    'two_factor_settings' =\u003e TwoFactor::class,\n    'password_reset' =\u003e PasswordReset::class,\n    'password_confirmation' =\u003e PasswordConfirmation::class,\n    'request_password_reset' =\u003e RequestPasswordReset::class,\n];\n```\n\nMake sure you extend the original classes from the package.\n\n### Multi-tenant setup\n\nIf you're using Filament in a multi-tenant setup, you need to set the `tenant` option to `true` in the `config/filament-2fa.php` file. You also need to set the `userMenuItems` in your panel config. Take a look at the example below:\n\n```php\nuse Vormkracht10\\TwoFactorAuth\\Pages\\TwoFactor;\n\n// ...\n\n-\u003euserMenuItems([\n    // ...\n    '2fa' =\u003e MenuItem::make()\n        -\u003eicon('heroicon-o-lock-closed')\n        -\u003elabel(__('Two-Factor Authentication'))\n        -\u003eurl(fn(): string =\u003e TwoFactor::getUrl(['tenant' =\u003e auth()-\u003euser()-\u003eorganization-\u003egetRouteKey()])),\n])\n```\n\n### Forcing Two Factor Authentication\n\nIf you want to force users to enable Two Factor Authentication, you can add this to your `PanelProvider`:\n\n```php\n-\u003eplugins([\n    TwoFactorAuthPlugin::make()-\u003eforced(),\n])\n```\n\n### Prevent showing the Two Factor Authentication page in user menu\n\nIf you want to prevent showing the Two Factor Authentication page in the user menu, you can add this to your `PanelProvider`:\n\n```php\n-\u003eplugins([\n    TwoFactorAuthPlugin::make()-\u003ehideFromMenu(),\n])-\u003eshowInUserMenu(false)\n```\n\n\u003e [!WARNING]\n\u003e When you're using the `forced` method, make sure to set the `multi_tenancy` option to `true` in the `filament-2fa.php` config file when you're using a multi-tenant setup. Otherwise, the forced setting will not work. We cannot check the tenant in the `PanelProvider` because the user is not authenticated yet.\n\n#### Customizing the forced message\n\nIf you want to customize the forced message, you can publish the language file:\n\n```bash\nphp artisan vendor:publish --tag=\"filament-2fa-translations\"\n```\n\nThen you can customize the message in the `lang/vendor/filament-2fa/en.json` file. You should change the following keys:\n\n```json\n{\n    \"Your administrator requires you to enable two-factor authentication.\": \"Your custom message here.\",\n    \"Two-Factor Authentication mandatory\": \"Your custom title here.\"\n}\n```\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n-   [Baspa](https://github.com/vormkracht10)\n-   [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvormkracht10%2Ffilament-2fa","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvormkracht10%2Ffilament-2fa","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvormkracht10%2Ffilament-2fa/lists"}