I have a custom endpoint for User Meta that I've successfully registered to the WP REST API and created a custom endpoint for. However, whenever I make GET and POST requests from my single-page app (JS framework being used is Vue), the return value is 'false.'
I suspect this is happening due to some authorization issues using the JWT Authorization for WP REST API plugin.
Here are my steps taken in the functions.php app:
First I register the user meta field for the REST API:
//register user meta to rest api
add_action( 'rest_api_init', 'adding_user_meta_rest' );
function adding_user_meta_rest (){
register_meta(
'user',
'user_likes',
array(
'single' => false,
'type' => 'object',
'show_in_rest' => true,
)
);
};
This step appears to be working fine since the field is showing up when I use the path "/wp-json/wp/v2/users/".
Next step, I register the custom end point:
add_action('rest_api_init','add_user_likes');
function add_user_likes(){
register_rest_route(
'v1/user_likes','/(?P<id>\d+)',[[
'methods'=> 'GET',
'callback'=>'get_user_likes'
],
[
'methods'=> 'POST',
'callback'=>'post_user_likes'
]]);
};
And here are my callbacks:
function get_user_likes(){
$current_user_id = get_current_user_id();
$status = $response->get_status;
return get_user_meta($current_user_id,'user_likes');
}
function post_user_likes(WP_REST_Request $req){
$body = $req->get_json_params();
$current_user_id = get_current_user_id();
return update_user_meta($current_user_id, 'user_likes',$body);
};
And then on the front end, here is my POST request:
handleLike(){
fetch(`https://site-url.com/wp-json/v1/user_likes/${this.$store.state.userInfo.id}`,{
method:"POST",
body:JSON.stringify({
post_id:this.currentPost.id,
post_title:this.currentPost.title.rendered
}),
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${this.$store.state.accessToken}`
},
}).then(resp=>resp.json())
.then(data=>console.log(data))
My GET request is also returning false.
The issue I believe is that I am not retrieving the current user ID because according to docs for get_user_meta and update_user_meta, if user id is not determined, the function will return false. Why the user id is not being determined, I don't know. As I mentioned, I am using the JWT Authorization for WP REST API. Therefore based on my reading, I do not need to generate a nonce because WP will use the token to determine the current user. Let me know if I am wrong about that. Any guidance would be greatly appreciate.