How to set custom payload on Laravel JWT?

How to set custom payload on Laravel JWT?

How we can set the custom payload on Laravel JWT auth package. The payload means many times we need to store some user related detail in the session when user login. Something like user education, jobs etc.. related data. So we want to store more user related detail on JWT auth token.


Let’s how we can set custom payload on JWT auth token.

I hope you installed the JWT auth package on Laravel. If not please follow this tutorial about JWT auth.

//load user place your code for load user
$user = User::find( $user_id );
$userinfo = UserInfo::where('user_id','=',$user->id)->first();

$payloadable = [
        'id' => $user->id,
        'name' => $user->name,
        'email' => $user->email,
        'company' => $userinfo->company,
        'job_position' => $userinfo->job_position,
        'country' => $userinfo->country,
        'organization' => $userinfo->organization_id

$token = JWTAuth::fromUser($user,$payloadable);

We set the custom payload like “company”, “job_position”, “country”, “Organization” Information from user info table.

How can we get payload detail after user token is verified?

$payload = JWTAuth::parseToken()->getPayload();
// then either of
$comapny = $payload->get('company');
$organization = $payload->get('organization');

If the token is verified you can get the payload information using getPayload method as per above code.

If you face any issue after using code or have any question write the comment below.