Laravel Tests - changing Sanctum default guard
// Authenticating w. Sanctum on Test Env. $user = User::factory()->create(); Sanctum::actingAs($user);
But, by default, when using this method, the user will be authenticated using the "sanctum" guard. And there are cases in that you'll need to change it.
The given role or permission should use guard `web, api` instead of `sanctum`.
It happens because, by default, the Laravel Permissions package will use the application predefined guards (web, API), but Sanctum is using the "sanctum" guard instead.
To solve it, just change the Sanctum authentication to use the following code:
// The empty array can be filled with token abilities Sanctum::actingAs($user, , 'web');
That is it!
With this simple change, you can define the correct guard when using Sanctum authentication on your tests.
I hope this post is helpful for you. Cheers!