Title: Token null on modified response
Last modified: October 20, 2020

---

# Token null on modified response

 *  Resolved [Timo](https://wordpress.org/support/users/timothywp/)
 * (@timothywp)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/token-null-on-modified-response/)
 * I tried to modify “JWT_AUTH_VALID_CREDENTIAL_RESPONSE” like shown in the docs.
 * But as soon as I do this (using a simple plugin for this) the token is null (
   although the reponse still says success:true).
 * Even with no changes to the reponse function that happens.
 *     ```
       /**
        * Modify the response of valid credential.
        *
        * @param array $response The default valid credential response.
        * @param WP_User $user The authenticated user.
        * .
        * @return array The valid credential response.
        */
       add_filter(
           'jwt_auth_valid_credential_response',
           function ( $response, $user ) {
        $response = array(
           'success'    => true,
           'statusCode' => 200,
           'code'       => 'jwt_auth_valid_credential',
           'message'    => __( 'Credential is valid', 'jwt-auth' ),
           'data'       => array(
               'token'       => $token,
               'id'          => $user->ID,
               'email'       => $user->user_email,
               'nicename'    => $user->user_nicename,
               'firstName'   => $user->first_name,
               'lastName'    => $user->last_name,
               'displayName' => $user->display_name,
           ),
       );
               return $response;
           },
           10,
           2
       );
       ```
   
 * Then I tried to modify the “JWT_AUTH_VALID_TOKEN_RESPONSE”, which works fine.
 * Any idea what’s wrong with the modified “JWT_AUTH_VALID_CREDENTIAL_RESPONSE”?
    -  This topic was modified 5 years, 11 months ago by [Yui](https://wordpress.org/support/users/fierevere/).
    -  This topic was modified 5 years, 11 months ago by [Yui](https://wordpress.org/support/users/fierevere/).
      Reason: please use CODE button for code formatting

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [darick0029](https://wordpress.org/support/users/darick0029/)
 * (@darick0029)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/token-null-on-modified-response/#post-13572755)
 * Hi i got the same issue hoping some answer from the plugin author.
    The plugin
   is awesome btw. Thanks to the author.
 *  [djboris88](https://wordpress.org/support/users/djboris88/)
 * (@djboris88)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/token-null-on-modified-response/#post-13659165)
 * Two things are wrong in your example.
 * You are completely overwriting the already prepared response in the `$response`
   variable.
 * Second, you are calling the `$token` variable which you haven’t defined anywhere,
   so you get the `null` value for the token key.
 * Try this instead:
 *     ```
       /**
        * Modify the response of valid credential.
        *
        * @param array $response The default valid credential response.
        * @param WP_User $user The authenticated user.
        * .
        * @return array The valid credential response.
        */
       add_filter(
           'jwt_auth_valid_credential_response',
           function ( $response, $user ) {
              $response['data']['customResponseData'] = 'Custom data';
   
              return $response;
           },
           10,
           2
       );
       ```
   
 * This way, the already prepared response is still used, as it contains all the
   valid and required data. We are just adding additional data by creating a new
   key/value pair in the `$response` variable. Note that you can name the key whatever
   you want, but if you name it the same as the existing key, you will overwrite
   its value.
 *  Thread Starter [Timo](https://wordpress.org/support/users/timothywp/)
 * (@timothywp)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/token-null-on-modified-response/#post-13659383)
 * Damn, that happens when you just copy something from the docs without thinking
   about it further.
    Now it works perfect, thank you so much [@djboris88](https://wordpress.org/support/users/djboris88/)
   🙂
 *  Plugin Author [Bagus](https://wordpress.org/support/users/contactjavas/)
 * (@contactjavas)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/token-null-on-modified-response/#post-13800885)
 * Thanks [@djboris88](https://wordpress.org/support/users/djboris88/) 😀 I need
   to update the docs
 *  [darick0029](https://wordpress.org/support/users/darick0029/)
 * (@darick0029)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/token-null-on-modified-response/#post-14014795)
 * jwt_auth_valid_credential_response for this hook the $token gives error if you
   use it as a parameter.
 * for this hook jwt_auth_valid_token_response. the filter doesnt work.

Viewing 5 replies - 1 through 5 (of 5 total)

 The topic ‘Token null on modified response’ is closed to new replies.

 * ![](https://ps.w.org/jwt-auth/assets/icon-256x256.png?rev=2298869)
 * [JWT Auth - WordPress JSON Web Token Authentication](https://wordpress.org/plugins/jwt-auth/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/jwt-auth/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/jwt-auth/)
 * [Active Topics](https://wordpress.org/support/plugin/jwt-auth/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/jwt-auth/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/jwt-auth/reviews/)

 * 5 replies
 * 4 participants
 * Last reply from: [darick0029](https://wordpress.org/support/users/darick0029/)
 * Last activity: [5 years, 8 months ago](https://wordpress.org/support/topic/token-null-on-modified-response/#post-14014795)
 * Status: resolved