Jwt-auth: Cannot use both Session and JWT Auth in Laravel 5.5

Created on 21 Nov 2017  路  12Comments  路  Source: tymondesigns/jwt-auth

Hello, I am using Laravel 5.5 with JWT dev-develop. I have also set my auth drivers for web.

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

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

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

But, the above configuration doesn't give me JWT Bearer Token. When I replace web with api, Auth::attempt($credentials) gives me JWT token instead of session.

Any ideas how to use both at the same time?

Most helpful comment

Specify the guard name like so (straight from the docs):
Auth::guard('api')->attempt($credentials)

All 12 comments

Specify the guard name like so (straight from the docs):
Auth::guard('api')->attempt($credentials)

@artaex Thank you so much

First: Apologize for my question. Where do I write the line?
I have the same problem.
Thanks.

@thewasta it means when you want to authenticate a user when login in e.g

Auth::guard('api')->attempt(['email' => '[email protected]', 'password' => 'reallyStrongPassword'])

@tijanmdr and @omitobi How did you later solve it, you change your guard to api and then used this Auth::guard('api')->attempt($credentials) for the api login right? Does that mean you created two routes for login, one for the site and one for as an api endpoint.
I also have the same issue.

@koiki13 that would be the ideal thing. To separate Web login from Api login

@omitobi Now one last thing, what would guard be set to, api or web. That is another decision I'm not sure of.

As I can tell,

api -> jwt
web -> session

@koiki13 If you have more routes that should be web, then you can set api guard when you need to do with any API endpoint.
But if you have more routes on each side, then using a middleware would be the best option.

You can just create api middleware, declare all api routes in a group attached with that middleware. In the middleware it should be possible to set the request guard there.

Thanks for your help but, I don't quite understand the last part. "Setting request guard in middleware". Will appreciate if you can explain here or give a link where I can read that up.

@koiki13 Sure. https://laravel.com/docs/7.x/middleware#middleware-groups

Create a middleware group using that link. The idea is to attach a middleware to the group. Create a middleware and set the default guard there.

I think you could use this in the middleware$this->auth->shouldUse('api'); Just check Laravel doc for more info.

@omitobi Thanks, this is nice. Appreciate it 馃憤

Specify the guard name like so (straight from the docs):
Auth::guard('api')->attempt($credentials)

this does not work in me, do you have any idea what is the issue is?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alihossein picture alihossein  路  24Comments

johncloud200 picture johncloud200  路  32Comments

vsecades picture vsecades  路  26Comments

marijang picture marijang  路  53Comments

frenetic picture frenetic  路  26Comments