Spatie Permissions / Laravel - Assigned Permission to Role, Role to User - Middleware doesn't pick up on permissions

254 Views Asked by At

I am using Spatie Permissions for my Laravel app. I have assigned permissions to a role, and the role to a user.

When I use $user->getAllPermissions() or $user->getPermissionsViaRoles() it displays them all in JSON format.

However, when I am ensuring they have a certain permission before viewing a route using Spatie middleware (permission/can) it says unauthorized.

Route::get('/admin/users', \App\Http\Livewire\Admin\User\UserList::class)->middleware('permission:view users')->name('admin.users');

I was wondering how I can make it so it knows the permission is assigned through the users role?

It says best practice is to do it how I've done, rather than assigning permissions direct to users.

I've tried using AI, googling, etc and found no answers.

1

There are 1 best solutions below

0
amac On

are you using the \Illuminate\Auth\Middleware\Authorize::class in your kernal.php? I believe that to be required for using the can middleware.