1

Im trying to pull the current logged in user data into my headless frontend but data always returns empty - i think its to do with setting a nonce?

axios
    .get('https://example.com/wp-json/v1/get-user')
    .then(response => ( console.log(response) ));

and in functions file

$user_id = "";

add_action( 'rest_api_init', 'add_custom_users_api');

function add_custom_users_api(){
    $GLOBALS['user_id'] = get_current_user_id();

    register_rest_route( 'v1', 'get-user', array(
        'methods' => 'GET',
        'callback' => 'get_custom_users_data',
        'nonce'     => wp_create_nonce( 'wp_rest' ),
    ));
}
//Customize the callback to your liking
function get_custom_users_data(){
    return get_user_by( 'id', $GLOBALS['user_id'] );
}

What am i missing? The idea is the user will login on the main website and then on this headless area will be authorised.

1
  • You need to do some test, like, directly return inside your function a dumb data. Check the status code of the response. We can't really help without more information about the api response and when something go wrong. Commented May 22, 2023 at 7:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.