php - Laravel Token Signature could not be verified -
i'm using laravel/lumen api backend of webapp , run hiccup.
in example have route not need user authenticated. want check in routes controller if user visiting has valid token.
so wrote following:
if ($tokenfetch = jwtauth::parsetoken()->authenticate()) { $token = str_replace("bearer ", "", $request->header('authorization')); } else { $token = ''; } i believe above check bearer token valid else return blank variable.
the following entire controller.
public function show($url, request $request) { if ($tokenfetch = jwtauth::parsetoken()->authenticate()) { $token = str_replace("bearer ", "", $request->header('authorization')); } else { $token = 'book'; } return response()->json(['token' => $token]); } the problem
if pass in valid token bearer, returns token but if pass in invalid 1 following error:
tokeninvalidexception in namshiadapter.php line 62:
token signature not verified.
if don't pass token @ all:
jwtexception in jwtauth.php line 195:
the token not parsed request
is there way check if token passed , if has check if valid, if 1 has not been passed return blank return?
you can wrap inside try/catch block
public function show($url, request $request) { try { $tokenfetch = jwtauth::parsetoken()->authenticate()) $token = str_replace("bearer ", "", $request->header('authorization')); }catch(\tymon\jwtauth\exceptions\jwtexception $e){//general jwt exception $token = 'book'; } return response()->json(['token' => $token]); } there few exceptions might want handle separately (jwt-auth/exceptions)
also you're using laravel 5 can global handling jwt exceptions ,not recommended in case should know of option , choose yourself. app/exceptions/handler.php , inside render method add [at top]
if ($e instanceof \tymon\jwtauth\exceptions\jwtexception) { //what happen when jwt exception occurs }
Comments
Post a Comment