Skip to content

Commit

Permalink
Add forced() method and middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeWithDennis committed Sep 9, 2024
1 parent 5ca9ae3 commit ffa4aa8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Http/Middleware/Force2Factor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Vormkracht10\TwoFactorAuth\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class Force2Factor
{
public function handle(Request $request, Closure $next): mixed
{
$user = filament()->auth()->user();

Check failure on line 12 in src/Http/Middleware/Force2Factor.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Filament\Contracts\Plugin|Filament\FilamentManager::auth().

if ($request->is('*/two-factor') || $request->is('*/logout')) {
return $next($request);
}

if (! $user->two_factor_confirmed_at) {

Check failure on line 18 in src/Http/Middleware/Force2Factor.php

View workflow job for this annotation

GitHub Actions / phpstan

Cannot access property $two_factor_confirmed_at on Illuminate\Foundation\Auth\User|null.
// TODO: Redirect to the two-factor page.
}

return $next($request);
}
}
21 changes: 21 additions & 0 deletions src/TwoFactorAuthPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,30 @@
use Filament\Contracts\Plugin;
use Filament\Navigation\MenuItem;
use Filament\Panel;
use Vormkracht10\TwoFactorAuth\Http\Middleware\Force2Factor;
use Vormkracht10\TwoFactorAuth\Pages\TwoFactor;

class TwoFactorAuthPlugin implements Plugin
{
private bool $forced = false;

public function getId(): string
{
return 'filament-two-factor-auth';
}

public function forced(bool $forced = true): self
{
$this->forced = $forced;

return $this;
}

public function isForced(): bool
{
return $this->forced;
}

public function register(Panel $panel): void
{
$panel
Expand All @@ -24,6 +39,12 @@ public function register(Panel $panel): void
])
->viteTheme('vendor/vormkracht10/filament-2fa/resources/dist/filament-two-factor-auth.css');

if ($this->isForced()) {
$panel->authMiddleware([
Force2Factor::class,
]);
}

if (! config('filament-two-factor-auth.enabled_features.multi_tenancy')) {
$panel->userMenuItems([
'two-factor-authentication' => MenuItem::make()
Expand Down

0 comments on commit ffa4aa8

Please sign in to comment.