codecasts/laravel-jwt

Can this be used with multiple guard?

testlogin12 opened this issue · 5 comments

Not exactly an issue, but had to ask this question. Can this be used with multiple guards? If yes then how to configure it?

In my config/auth.php

    'guards' => [
        'user' => [
            'driver' => 'jwt',
            'provider' => 'users',
        ],

        'staff' => [
            'driver' => 'jwt',
            'provider' => 'staff',
        ],
    ],

I am trying to do something like this.

motia commented

Well, you can use it for multiple guards. I am using it for that. You are on the right way, now define the guard provider for your staff like:

'providers' => [
    ..
    'staff' => [
        'driver' => 'eloquent',
        'model' => \App\Models\Staff::class,
    ],
]

Thanks, I have configured everything required in config/auth.php file.
Where I am having a problem is how to select which guard to use during login.
I am trying to use it for an api and i need multiple guard like admin guard and staff guard.

@testlogin12 there is a middleware on this package that will match the guard name with the middleware group name.

It means if your middleware group is called api and your guard groups is called api, then it will match automatically.

I made that so multiple guards can exists

But, if that's not an options, you can always explicitly use a guard, with something like this:auth()->guard('my-jwt-guard')->{login,logout,check}()

Thanks it worked for me.
I went with using multiple middleware group.