Tmeister / wp-api-jwt-auth

A simple plugin to add JSON Web Token (JWT) Authentication to WP REST API
GNU General Public License v2.0
556 stars 160 forks source link

using jwt_auth_token_before_sign creates invalid json #292

Open simonh1000 opened 11 months ago

simonh1000 commented 11 months ago

Issue Name

Prerequisites

Please answer the following questions for yourself before submitting an issue.

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

Expected Behavior

I'm not a PHP specialist, so i'm just trying to extend code I fond on stackoverflow

I have

add_action( 'jwt_auth_token_before_sign', 'jwt_token', 10, 2 );

function jwt_token($token, $user) {
  $data = [
    'user' => [
        'id' => $user->data->ID,
        'name' => $user->data->display_name,
        'roles' => implode(',', $user->roles)
    ]
  ];
  $token['data'] = $data;
  return $token;
}

the trouble is that when this returns after an auth request I get

Array{
    "token": "eyJ0e...NcbMJcQ",
    "user_email": "...@gmail.com",
    "user_nicename": "xyz",
    "user_display_name": "xyz"
}

the word Array is then messing up the parsing in the client

Any ideas how to return a $token that continues to json encode properly?