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.