Jwt-auth: Exception TokenBlacklistedException not working

Created on 28 Jan 2016  路  6Comments  路  Source: tymondesigns/jwt-auth

I am using Laravel 5.1 and I a'm trying to use Exception: TokenBlacklistedException and TokenExpiredException but it does not work. Rest Using Advanced Client for Chrome,
returns the following error.
screen shot 2016-01-28 at 3 28 36 pm
does not return the message in the catch.

try {

        if (! $user = JWTAuth::parseToken()->authenticate()) {
            return response()->json(['user_not_found'], 404);
        }

    } catch (Tymon\JWTAuth\Exceptions\TokenBlacklistedException $e) {

        return response()->json(['token_expired'], $e->getStatusCode());

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

        return response()->json(['token_expired'], $e->getStatusCode());

    } catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {

        return response()->json(['token_invalid'], $e->getStatusCode());

    } catch (Tymon\JWTAuth\Exceptions\JWTException $e) {

        return response()->json(['token_absent'], $e->getStatusCode());

    }

Most helpful comment

Thanks for you comment. Problem solved

I am use:

use TymonJWTAuthExceptionsJWTException;
use TymonJWTAuthExceptionsTokenExpiredException;
use TymonJWTAuthExceptionsTokenInvalidException;
use TymonJWTAuthExceptionsTokenBlacklistedException;

and:

try {

        if (! $user = JWTAuth::parseToken()->authenticate()) {
            return response()->json(['user_not_found'], 404);
        }

    } catch (TokenBlacklistedException $e) {

        return response()->json(['token_expired'], $e->getStatusCode());

    }catch (TokenExpiredException $e) {

        return response()->json(['token_expired'], $e->getStatusCode());

    } catch (TokenInvalidException $e) {

        return response()->json(['token_invalid'], $e->getStatusCode());

    } catch (JWTException $e) {

        return response()->json(['token_absent'], $e->getStatusCode());

    }

All 6 comments

Are you in a namespace? You'll either need leading slashes on the exceptions to escape them to the global namespace, or else use them so they're available in the file.

Yes, I am using:

use JWTAuth;
use TymonJWTAuthExceptionsJWTException;
use TymonJWTAuthExceptionsTokenExpiredException;
use TymonJWTAuthExceptionsTokenInvalidException;
use TymonJWTAuthExceptionsTokenBlacklistedException;

I'm not sure if I will need something to set

Thanks for you comment. Problem solved

I am use:

use TymonJWTAuthExceptionsJWTException;
use TymonJWTAuthExceptionsTokenExpiredException;
use TymonJWTAuthExceptionsTokenInvalidException;
use TymonJWTAuthExceptionsTokenBlacklistedException;

and:

try {

        if (! $user = JWTAuth::parseToken()->authenticate()) {
            return response()->json(['user_not_found'], 404);
        }

    } catch (TokenBlacklistedException $e) {

        return response()->json(['token_expired'], $e->getStatusCode());

    }catch (TokenExpiredException $e) {

        return response()->json(['token_expired'], $e->getStatusCode());

    } catch (TokenInvalidException $e) {

        return response()->json(['token_invalid'], $e->getStatusCode());

    } catch (JWTException $e) {

        return response()->json(['token_absent'], $e->getStatusCode());

    }

Yep sorry I wasn't clear. That's what I meant by the second option. :wink: Glad it worked out!

Nice working for me

great answer

Was this page helpful?
0 / 5 - 0 ratings