Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decode JWT using JWT-auth in laravel

Hello my issue is that I need to verify a JWT token coming from android and decode it to fetch the information in the payload but I can't seem to find a decode method in the JWT-Auth 0.5*, is there another way to decode the payload to get the data?

like image 497
plu plu Avatar asked Jan 17 '18 20:01

plu plu


1 Answers

 use Tymon\JWTAuth\Facades\JWTAuth; //use this library

try this

 $token = JWTAuth::getToken();
 $apy = JWTAuth::getPayload($token)->toArray();

also you can get other info like this

   try {
        // attempt to verify the credentials and create a token for the user
        $token = JWTAuth::getToken();
        $apy = JWTAuth::getPayload($token)->toArray();
    } catch (\Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {

        return response()->json(['token_expired'], 500);

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

        return response()->json(['token_invalid'], 500);

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

        return response()->json(['token_absent' => $e->getMessage()], 500);

    }
like image 112
NanThiyagan Avatar answered Oct 09 '22 21:10

NanThiyagan