• Resolved Timo

    (@timothywp)


    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 4 years, 1 month ago by Yui.
    • This topic was modified 4 years, 1 month ago by Yui. Reason: please use CODE button for code formatting
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi i got the same issue hoping some answer from the plugin author.
    The plugin is awesome btw. Thanks to the author.

    djboris88

    (@djboris88)

    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

    (@timothywp)

    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 ??

    Plugin Author Bagus

    (@contactjavas)

    Thanks @djboris88 ?? I need to update the docs

    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.