Skip to content
Advertisement

Laravel 5.7 + Spatie Permissions + JWT auth

I’m setting up a REST API using Laravel 5.7. To validate authentication I JWT-auth and for permissions and roles I use Spatie.

My problem: when trying to link a role to a user I get the following error

Spatie  Permission  Exceptions  RoleDoesNotExist
There is no role named admin.

The role do exist in the database: enter image description here

This is how I’m trying to assign a role to the user:

$user = User::findOrFail(1);
$user->assignRole('admin');

As I’m new to Laravel I’m not sure if it’s relevant, but setting the JWT I had to change the driver of the guard in the config/auth.php to jwt

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'jwt',
        'provider' => 'users',
    ],
],

I can’t see what I’m doing wrong. I added the roles and then tried to add the role to a user.

Advertisement

Answer

Check your app namespace. If you updated it from App, be sure to update it in config/auth.php.

On the other hand, if you didn’t update the App namespace, try clearing your cache and re-seed the database tables.

php artisan config:cache
php artisan cache:clear

Also check the user model if you have protected $guard_name = 'api'; in there.

Hope this helps. Cheers!

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement