Filament is a TALL stack admin panel for Laravel that doesn't come with built-in two-factor authentication (2FA) support. However, you can integrate 2FA into your Filament admin panel by using Laravel's built-in two-factor authentication services or by using a package like laravel/fortify which provides the backend controllers and routes necessary for 2FA.
Here's a general guide on how you can add 2FA to your Filament admin panel:
- Install Laravel Fortify (if you haven't already):
composer require laravel/fortify
- Publish Fortify's resources:
php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider"
-
Configure Fortify to use the views you want for 2FA by modifying the
fortify.phpconfig file. -
Implement the two-factor authentication routes in your
routes/web.phpfile:
use Laravel\Fortify\Http\Controllers\TwoFactorAuthenticatedSessionController;
Route::group(['middleware' => ['web', 'auth']], function () {
Route::post('/user/two-factor-authentication', [TwoFactorAuthenticatedSessionController::class, 'store'])
->name('two-factor.enable');
Route::delete('/user/two-factor-authentication', [TwoFactorAuthenticatedSessionController::class, 'destroy'])
->name('two-factor.disable');
// Other 2FA routes...
});
-
Update your User model to use the
TwoFactorAuthenticatabletrait:
use Laravel\Fortify\TwoFactorAuthenticatable;
class User extends Authenticatable
{
use TwoFactorAuthenticatable;
// ...
}
- Create the necessary database columns for 2FA by creating a new migration:
php artisan make:migration add_two_factor_columns_to_users_table --table=users
Then, in the migration file:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->text('two_factor_secret')->after('password')->nullable();
$table->text('two_factor_recovery_codes')->after('two_factor_secret')->nullable();
});
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('two_factor_secret', 'two_factor_recovery_codes');
});
}
Run the migration:
php artisan migrate
- Integrate 2FA into your Filament admin panel by overriding the login and two-factor challenge views with your own. You can publish Filament's views and modify them to include the 2FA fields:
php artisan vendor:publish --tag=filament-views
-
Handle the 2FA challenge after the user logs in. You may need to customize the login controller to redirect users to a 2FA challenge page if they have 2FA enabled.
-
Test your implementation thoroughly to ensure that 2FA is working as expected.
Please note that this is a high-level guide and assumes familiarity with Laravel, Filament, and Fortify. You may need to adjust the steps based on your specific setup and requirements. Always refer to the official documentation of Laravel, Fortify, and Filament for the most accurate and up-to-date instructions.