JWT signature failed errors are not coming as JSON errors in SLIM
I am using Firebase JWT in one of my SLIM framework apps for RESTFul api. But all the JWT errors occur as slim app error rather than throwing back json errors ,which i can return to user.
Error is as below
Slim Application Error
The application could not run because of the following error: Details Type: Firebase\JWT\SignatureInvalidException Message: Signature verification failed File: D:\Xampp\htdocs\slim\test_app\test\vendor\firebase\php-jwt\src\JWT.php Line: 112
try{$decoded = JWT::decode("eyJseXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9leGFtcGxlLm9yZyIsImF1ZCI6Imh0dHA6XC9cL2V4YW1wbGUuY29tIiwiaWF0IjoiMjAxOC0wOS0wNCAwNjoyOTowNCIsIm5iZiI6NzgsInVpZCI6IjEyMzUifQ.nk3IAMGhSVgtluQgn-0xTZvRprzJJHLLj3ZWT_iiXPc", $key, array('HS256'));
print_r($decoded);
}catch(\Firebase\JWT\ExpiredException $e){
echo 'Caught exception: ', $e->getMessage(), "\n";
}
Can you help how can i display JWT errros in json format ,so that my restful api return that json error to user.
@bhullar89 store the error message in an array , and then just use the json_encode function to get the errors in json format
$result = array(
'status'=>false,
'message'=>null,
'data'=>null
);
$fbPublicKey=$this->getFbPublicKey();
try {
$decodedToken = JWT::decode($oauthToken, $fbPublicKey , ["RS256"]);
$result['status']=true;
$result['data']=(array)$decodedToken;
} catch (\Exception $e) {
$result['message']=$e->getMessage();
}
return json_encode($result);
Closing as this seems answered