Support » Plugin: JWT Authentication for WP REST API » Getting User Id

  • Hello,

    I know it’s a repetitive question, but I’m just looking for a clear way (a little more than two words) to determine the identifier of the user logged in via jwt.
    I need to filter posts by author ID, and much more.

    (I’m using ReactJS client side and wp api v2 server side …)

    Can someone help please?

    Thanks a lot !

Viewing 1 replies (of 1 total)
  • I’ve been struggling with this issue myself. In some callback functions in my REST API, I could determine the current user’s ID with get_current_user_id(). However when I started testing endpoints for adding data via the REST API, get_current_user_id() always returned 0.

    I’m not sure if this is due to a server configuration as this is on a shared hosting server. But fact remains that get_current_user_id() works fine with GET requests and returns 0 on POST requests.

    What I used as a workaround, is to return the user ID while retrieving some other data from the REST API and saving this to the state of my App. After this, when I needed to update something, I would just send the userID as a param in my POST request. And then use this whenever I needed the userID in the backend of my application via $request->get_param(‘userID’).

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.