FilamentF
Filament10mo ago
Pritbor

canAccessPanel() Not being called

I am triying to secure access but my canAccessPanel() is not being called. I am using FilamentUser contract in User. And below is my code. to test i am always retirnung false but i am still able to access panel. I cleared all caches.
    public function canAccessPanel(Panel $panel): bool
    {
        dd('canAccessPanel is being called!');
        return false;

        $panelId = $panel->getId();
        if ($panelId === 'labaiq-team') {
            return $this->user_type === 'platform_team_member' && str_ends_with($this->email, '@labaiq.com') && $this->email_verified_at;
        }

        if($panelId === 'user' || $panelId === 'company'){

            return $this->user_type === 'general_user';
        }


        return false;
    }
Solution
Error was due to using the Custom authentication middleware in all my panels. I had commented out Authenticate::class from all auth middleware. Filament native authenticate middleware calls canAccessPanel() method directly. Whereas I implemented CustomAuthenticate::class and removed filament one which caused and issue. Fix was to include this below peice of code in my CustomAuthentciate.
$panel = Filament::getCurrentPanel();

        abort_if(
            $user instanceof FilamentUser ?
                (! $user->canAccessPanel($panel)) :
                (config('app.env') !== 'local'),
            403,
        );
Was this page helpful?