Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing custom Tymon\JWT token with laravel

I'm trying to parse a custom JWT token. To generate the token i have the following class:

public function createToken()
{
    $client = ['sub' => 'u0406'];

    $payload = JWTFactory::make($client);

    $token = JWTAuth::encode($payload);

    return $token;
}

I get this token and passing it

How can i parse this and get the variable $client in another method? My parseMethod is declared like this:

public function create(Request $request)
{
        $token = JWTAuth::getToken();
        return $token;
}

And it just returns the token string. Not the array.

like image 851
Ivan Moreira Avatar asked Nov 16 '25 16:11

Ivan Moreira


1 Answers

I suppose you succeeded in creating your token in the createToken() method and you are passing it in a request to the create() method inside which you want to parse that token to get the client object/associative array again.

From what you are wrote, I think you are using the wrong method on the JWTAuth, which is getToken(). To do the parsing right, try this

  1. Instantiate a $jwtauth object from the JWTAuth class first

  2. In your create method access the client/user by using $client = jwtauth->toUser($token);

like image 61
Ramy Farid Avatar answered Nov 19 '25 06:11

Ramy Farid