tymondesigns / jwt-auth

πŸ” JSON Web Token Authentication for Laravel & Lumen
https://jwt-auth.com
MIT License
11.29k stars 1.54k forks source link

Laravel 5.4 not working custom render function in Handler.php (Exception not called) #1184

Open NNBMon opened 7 years ago

NNBMon commented 7 years ago

Hello guys. I need help for this. I wrote this codes in Handler.php that is not working :

public function render($request, Exception $exception) { console.log('Log here'); $exceptionClass = get_class($exception); $exceptionMessage = $exception->getMessage(); $exceptionCode = $exception->getCode();

switch ($exceptionClass) { case 'Tymon\JWTAuth\Exceptions\TokenExpiredException': return response()->json(['expired token'], 401); break; case 'Tymon\JWTAuth\Exceptions\TokenInvalidException': return response()->json(['invalid token'], 401); break; case 'Tymon\JWTAuth\Exceptions\JWTException': return response()->json(['general JWT error'], 500); break; default: return response()->json([$exceptionMessage], $exceptionCode); break; } return parent::render($request, $exception); }

But it's not show console.log('Log here') not called. I'm thinking any exception jwt not called render function. Give me this exception {"error":"token_not_provided"} printed. Sorry for my english :)

donnygunawan93 commented 7 years ago

I have this issue also. I try to catch in Handler.php but still return default from vendor. Awww.

AlennGK commented 7 years ago

Try this approach by setting up new middleware.

xdstack commented 6 years ago

Same question. Laravel 5.5

β€”β€”β€”β€”β€”β€”β€”β€”

Update:

I have solved the problem. The reason is I used dingo/api which took over the exception handler.

So I custom a handler at AppServiceProvider@register.

\API::error(function (\Tymon\JWTAuth\Exceptions\TokenExpiredException $exception) {
            return response()->json([
                'status_code'   => 403,
                'message' => $exception->getMessage()
            ], 200);
        });