Skip to content

Commit

Permalink
Set two factor type for users that already enabled 2FA (#49)
Browse files Browse the repository at this point in the history
* Set two factor type for users that already enabled 2FA

* Fix styling

* wip

* Fix styling

* Update README

---------

Co-authored-by: Baspa <[email protected]>
  • Loading branch information
Baspa and Baspa authored Oct 4, 2024
1 parent e43c91b commit 133f1bd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ You can then easily install the plugin by running the following command:
php artisan filament-two-factor-auth: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`:

```php
Expand Down
50 changes: 46 additions & 4 deletions src/TwoFactorAuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
use Laravel\Fortify\Contracts\TwoFactorLoginResponse as TwoFactorLoginResponseContract;
Expand All @@ -25,6 +27,7 @@
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Vormkracht10\TwoFactorAuth\Commands\TwoFactorAuthCommand;
use Vormkracht10\TwoFactorAuth\Enums\TwoFactorType;
use Vormkracht10\TwoFactorAuth\Http\Responses\LoginResponse;
use Vormkracht10\TwoFactorAuth\Http\Responses\TwoFactorChallengeViewResponse;
use Vormkracht10\TwoFactorAuth\Http\Responses\TwoFactorLoginResponse;
Expand All @@ -45,11 +48,50 @@ public function configurePackage(Package $package): void
*/
$package->name(static::$name)
->hasCommands($this->getCommands())
->hasInstallCommand(function (InstallCommand $command) {
->hasInstallCommand(function (InstallCommand $command) use ($package) {
$command
->publishConfigFile()
->publishMigrations()
->askToRunMigrations()
->startWith(function (InstallCommand $command) use ($package) {
if ($command->confirm('Would you like to publish the config file?', true)) {
$command->comment('Publishing config...');
$command->callSilently('vendor:publish', [
'--tag' => "{$package->shortName()}-config",
]);
}

if ($command->confirm('Would you like to publish the migrations?', true)) {
$command->comment('Publishing migrations...');
$command->callSilently('vendor:publish', [
'--tag' => "{$package->shortName()}-migrations",
]);
}

if ($command->confirm('Would you like to run the migrations now?', true)) {
$command->comment('Running migrations...');

$command->call('migrate');

if ($command->confirm('Would you like us to set the two factor type to "authenticator" for existing users?', true)) {

if (! Schema::hasTable('users')) {
$command->error('Table users does not exist.');

return;
}

if (! Schema::hasColumn('users', 'two_factor_type')) {
$command->error('Column two_factor_type does not exist in table users. Please run the migrations first.');

return;
}

$command->comment('Setting two factor type to "authenticator" for existing users...');

DB::table('users')
->where('two_factor_confirmed_at', '!=', null)
->update(['two_factor_type' => TwoFactorType::authenticator]);
}
}
})
->askToStarRepoOnGitHub('vormkracht10/filament-two-factor-auth');
});

Expand Down

0 comments on commit 133f1bd

Please sign in to comment.