Jwt-auth: catch exception to json

Created on 14 Apr 2016  路  8Comments  路  Source: tymondesigns/jwt-auth

Hi everybody,
I have installed a fresh laravel installation V5.2, and implemented jwt. Everything works fine for testing credential of user and returning a valid token.
My problem is when i try to check the token following the documentation

`try {
if (!$user = JWTAuth::parseToken()->authenticate()) {
return response()->json('user_not_found');
}

    } catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
        return response()->json('token_expired');

    } catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
        return response()->json('token_invalid');

    } catch (Tymon\JWTAuth\Exceptions\JWTException $e) {
        return response()->json('token_absent');
    }

    // the token is valid and we have found the user via the sub claim
    return response()->json(compact('user'));`

All the response are exception and no one of this response are a json. Even when putting the exception on the Handler the responses aren't on a json format.
Can someone help me please.

Most helpful comment

i finally found the solution i'm missing a root slash when calling the exception so the catch became

catch (\Tymon\JWTAuth\Exceptions\TokenExpiredException $e)

All 8 comments

i finally found the solution i'm missing a root slash when calling the exception so the catch became

catch (\Tymon\JWTAuth\Exceptions\TokenExpiredException $e)

Wow it helps me. Thanks @unabomber87

Thanks! @unabomber87

Thank you for posting your solution, it really helped me!

thank you @unabomber87, it's really helped me

Can i see your route? are you using BaseMiddleware? @unabomber87

I'm facing the same issue I'm not sure why it's not catching the Exception
using laravel 5.6

I'm using Laravel 5.6 too.
And this : "catch (\Tymon\JWTAuth\Exceptions\TokenExpiredException $e)" is not working for me

Was this page helpful?
0 / 5 - 0 ratings