Middleware not applying on all the routes

I have a middle ware that checks for role of the authenticated user and does some function.I want it to apply on all the routes in the app so i added it in the filament config file under 'base' array .However the middle ware works when i access the dashboard route only.
14 Replies
bwurtz999
bwurtz9992y ago
What version of Filament are you using? Have you added some multi-tenancy or changed the default routing at all?
lodeki
lodekiOP2y ago
I'm on v2 ...Haven't changed any of the default routing.
bwurtz999
bwurtz9992y ago
Can you share the middleware array from the config file? And the middleware you created?
lodeki
lodekiOP2y ago
'middleware' => [
'auth' => [
Authenticate::class,
],
'base' => [
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DispatchServingFilamentEvent::class,
MirrorConfigToSubpackages::class,
CheckRoleMiddleware::class, //THIS IS IT
],
],
'middleware' => [
'auth' => [
Authenticate::class,
],
'base' => [
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DispatchServingFilamentEvent::class,
MirrorConfigToSubpackages::class,
CheckRoleMiddleware::class, //THIS IS IT
],
],
And this is the middleware
public function handle(Request $request, Closure $next): Response
{
$isLoggedin = auth()->check();
$user = auth()->user();
if ($isLoggedin && $request->user()->hasRole('manager')) {
if (!$user->isVerified) {
return redirect()->route('manager.resources.biodata.index');
}
}}
public function handle(Request $request, Closure $next): Response
{
$isLoggedin = auth()->check();
$user = auth()->user();
if ($isLoggedin && $request->user()->hasRole('manager')) {
if (!$user->isVerified) {
return redirect()->route('manager.resources.biodata.index');
}
}}
bwurtz999
bwurtz9992y ago
It looks like this only applies the isVerified check if the authenticated user is a manager. Could that be causing the issue? Also, if you add verified to your auth array, Filament should automatically enforce verification for all users
'middleware' => [
'auth' => [
Authenticate::class,
'verified',
],
'middleware' => [
'auth' => [
Authenticate::class,
'verified',
],
bwurtz999
bwurtz9992y ago
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
lodeki
lodekiOP2y ago
the isVerified is a boolean property i added on the User model.
bwurtz999
bwurtz9992y ago
oh ok alright so this route definition return redirect()->route('manager.resources.biodata.index'); tells me that you're using multi-tenancy standard Filament routes look like filament.resources.biodata.index there should be a config/manager.php where a separate group of middleware is defined and that's the middleware applied to all manager routes
lodeki
lodekiOP2y ago
Yeap true...I tried also to add the middleware to the base array of the manager.php but still the same result.
bwurtz999
bwurtz9992y ago
What route are you loading where you expect to call this middleware?
lodeki
lodekiOP2y ago
I want it to apply to all the routes.
bwurtz999
bwurtz9992y ago
I'm assuming you are using this package: https://github.com/artificertech/filament-multi-context
GitHub
GitHub - artificertech/filament-multi-context: A package for adding...
A package for adding multiple contexts to the filament admin panel - GitHub - artificertech/filament-multi-context: A package for adding multiple contexts to the filament admin panel
bwurtz999
bwurtz9992y ago
Is that right? If that's the case, then you will need to apply it for each context. This package creates separate contexts and each context has it's own middleware
lodeki
lodekiOP2y ago
Yes. I tried to add the middleware in each of the contexts' config file .

Did you find this page helpful?