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
560 stars 161 forks source link

using jwt_auth_token_before_sign creates invalid json #292

Open simonh1000 opened 1 year ago

simonh1000 commented 1 year 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?