Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set two factor type for users that already enabled 2FA #49

Merged
merged 7 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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