Skip to content

Instantly share code, notes, and snippets.

@POMXARK
Created May 21, 2024 14:06
Show Gist options
  • Save POMXARK/e916c25b5a7a8e9d1fc070b5f639b6c2 to your computer and use it in GitHub Desktop.
Save POMXARK/e916c25b5a7a8e9d1fc070b5f639b6c2 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Middleware;
use Filament\Facades\Filament;
use Filament\Models\Contracts\FilamentUser;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Support\Facades\Session;
class RedirectIfNotFilamentAdmin extends Middleware
{
protected function authenticate($request, array $guards): void
{
$auth = Filament::auth();
$user = $auth->user();
$panel = Filament::getCurrentPanel();
if (!($auth->check()
&& $user instanceof FilamentUser
&& $user->canAccessPanel($panel))) {
Session::flush();
$this->unauthenticated($request, $guards);
}
}
protected function redirectTo($request): ?string
{
return Filament::getLoginUrl();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment