tymondesigns / jwt-auth

🔐 JSON Web Token Authentication for Laravel & Lumen
https://jwt-auth.com
MIT License
11.3k stars 1.54k forks source link

[Question/Help] Middleware + custom guards/table. How? #1312

Open xxRockOnxx opened 7 years ago

xxRockOnxx commented 7 years ago

I need a way to specify a guard to be used aside from setting a default guard. Haven't find anything helpful yet.

taitai42 commented 7 years ago

if you use laravel 5.5, i and 1.0-rc1 i actually did it by renaming my middleware alias in something else than jwt.auth because it's used by laravel service provider to set the basemiddleware instead.

then in your own middleware you can decode the token and use the guard you want :

            $type = JWTAuth::parseToken()->getPayload()->get('type');
            $id = JWTAuth::parseToken()->getPayload()->get('sub');

            if (! $user = Auth::guard(lcfirst($type))->loginUsingId($id)) {
                return response()->json(['user_not_found'], 404);
            }

            Auth::setUser($user);