1 month ago

L8, Jetstream, spatie/laravel-permission - Guard switching to Sanctum

Posted 1 month ago by RichardJeffery


Laravel 8 Jetstream (Teams and API features disabled) Spatie's laravel-permissions package (Role and Permission models come from here)


When writing a test for creating a Role, a Permission created in the testing setup is given the 'web' guard_name as expected however when I'm then 'actingAs()' a user and try to create a Role within a controller, the 'sanctum' guard_name is automatically applied to the new Role. If I then try to $role->syncPermissions() with the permissions I created in the test setup, I get an error about a guard mismatch.

My solution:

composer remove laravel/sanctum

I then removed the reference to Sanctum in the middleware for the route (from 'auth:sanctum' to just 'auth').


Does anyone foresee any issues with this method? I know I could manually set a 'guard_name' for every Permission/Role I create but I wanted to avoid this if I could. I do wonder if simply changing the route middleware from 'auth:sanctum' to just 'auth' would have solved it, without having to remove Sanctum entirely?

Please sign in or create an account to participate in this conversation.