Get currrent logged user with custom data added to the User Collection type

The link above refers to v3. Here is the one for v4:

The path is now /src/extensions/users-permissions/server/services/user.js and the code has also slightly changed:

'use strict';

module.exports = {
  /**
   * Promise to fetch authenticated user.
   * @return {Promise}
   */
   fetchAuthenticatedUser(id) {
    return strapi
      .query('plugin::users-permissions.user')
      .findOne({ where: { id }, populate: ['role', 'custom_role'] });
  },
};

Hope this helps!

2 Likes

Hey so I tried creating the user.js in /src/extensions/users-permissions/server/services/user.js with my additional fields that I’m trying to get but it doesn’t respond to them. Am I missing something? There was nothing in that folder before.

'use strict';

module.exports = {
  /**
   * Promise to fetch authenticated user.
   * @return {Promise}
   */
   fetchAuthenticatedUser(id) {
    return strapi
      .query('plugin::users-permissions.user')
      .findOne({ where: { id }, populate: ['role', 'subscribedcourses', 'purchasedcourses'] });
  },
};

I’m trying to get the courses from a user when I call /api/users/me which are a one to many relationship.

1 Like

me too…
I tried to copy and paste the code above, but nothing was changed. the api didn’t come with relationship population

I tried with this but still can’t get the related fields of user/me endpoint.

Same as @ttzhtet @tiaswidiatmika @subliminalman32 the solution from sdvservices didn’t work for me.

But this answer that does not look very elegant did the job:

I’m still open to new solutions

Hi,

Have you a solution for TypeScript beta ?

1 Like

I tried all the above solutions but none have worked. Nothing different happens when I call http://localhost:1337/api/users/me, I can only fetch the default fields. Apparently the code added is not called. Is there another solution?

Hi.

After creating association with “User” in Strapi admin UI, you need to change “Authenticated” permission, enabling “find” of associated collection you want to shown in “/api/users/me” endpoint.

For example: I created “Restaurant” collection and associated it to “User”. Then, I needed to go to Settings → (USERS & PERMISSIONS PLUGIN) Roles → Authenticated and then enable “find” method of “Restaurant”.
After that, I needed to change endpoint to “/api/users/me?populate=restaurant”.

I hope this could help anyone had this same issue too

1 Like

thanks it helps me a lot.
but when i want to fetch products per every user images does not show