Jwt-auth: Can't handle JWTAuth Exceptions

Created on 8 Apr 2015  路  5Comments  路  Source: tymondesigns/jwt-auth

JWTAuth : Tested in 0.5.0 and 0.5.1
Laravel : Tested in 5.0.20 and 5.0.27

I have a functional implementation of JWTAuth with everything fine, except the JWTAuth Exceptions.

For example, when I try to validate an already used token and it expires, my catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) is not called.

// use Something\Not\Relevant\To\This\Issue
// use ...

use JWTAuth;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
use Tymon\JWTAuth\Exceptions\JWTException;

class MyController extends Controller {

    public function checkToken()
    {
        try 
        {
            if (! $user = JWTAuth::parseToken()->authenticate())
            {
                return [
                    'error' => true,
                    'code'  => 10,
                    'data'  => [
                        'message'   => 'User not found by given token'
                    ]
                ];
            }

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

            return [
                'error' => true,
                'code'  => 11,
                'data'  => [
                    'message'   => 'Token Expired'
                ]
            ];

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

            return [
                'error' => true,
                'code'  => 12,
                'data'  => [
                    'message'   => 'Invalid Token'
                ]
            ];

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

            return [
                'error' => true,
                'code'  => 13,
                'data'  => [
                    'message'   => 'Token absent'
                ]
            ];

        }

        return ['error' => false, 'token' => JWTAuth::getToken()];
    }

}

Instaed, Laravel generates a

local.ERROR: exception 'Tymon\JWTAuth\Exceptions\TokenExpiredException' with message 'Token has expired' in /home/api/api/vendor/tymon/jwt-auth/src/Validators/PayloadValidator.php:74

How can I intercept this exceptions?

Most helpful comment

Make sure your namespace isn't messing with the Exception paths.

Since you have imported the exceptions, you should remove the Tymon\JWTAuth\Exceptions\ portion.

// e.g.
} catch (JWTException $e) {

} catch (TokenExpiredException $e) {
// etc

All 5 comments

Make sure your namespace isn't messing with the Exception paths.

Since you have imported the exceptions, you should remove the Tymon\JWTAuth\Exceptions\ portion.

// e.g.
} catch (JWTException $e) {

} catch (TokenExpiredException $e) {
// etc

That's it. Tested, solved and closed

Having the same problem

My Code

   try {
      $user = $this->auth->authenticate($token);
    }catch(\Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
      return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]);
    } catch (\Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
      return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]);
    }catch(\Tymon\JWTAuth\Exceptions\JWTException $e){
      return $this->respond('tymon.jwt.invalid', 'token_invalid ws', $e->getStatusCode(), [$e]);
    }

I am not importing these in the top of the class.

Implemented auth:api guard middleware.
But if a send a request omitting Authorization token, Tymon\JWTAuth\Exceptions\JWTException is thrown in the response
I was expecting that JSON response will be returned to the calling endpoint

Seems that TokenExpiredException, TokenInvalidException, JWTException have no getStatusCode() function
Above Class extends JWTException, which extends Exception, which does not actually define getStatusCode().
Use getCode() instead

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marciomansur picture marciomansur  路  3Comments

mihailo-misic picture mihailo-misic  路  3Comments

gamelife1314 picture gamelife1314  路  3Comments

lbottoni picture lbottoni  路  3Comments

kofi1995 picture kofi1995  路  3Comments