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

Hello :wave:

I’m using users/me endpoint to fetch all the data of the current user (learned about that in this dicussion).

The problem is that I added data to the User collection type, but that custom field is not being fetched by the GET /users/me end point.

More details of my problem:
Basically I created a many to many relationship between users and restaurants. Not all users have access to all restaurants (they are administrators). So I added a field to act like a role, now the user is allowed to edit all of the restaurants that are linked to it.

I would like to fetch that data with the /users/me end point, but is only returning the default data, and not my new restaurant field.

Am I doing it right? Do I have to make two requests to get all the data about the current user?

Thanks for the help!

(created a discussion on Github before knowing the existance of this forum)

Hi @Antoine

By default we don’t populate any relations inside of the /users/me endpoint with the exception of role if you need to populate additional relation fields you can use the Plugin Extensions and extend the following code:

4 Likes

Works perfect :fire:

More details on the soluction:
Created file extensions/users-permissions/services/User.js (some folders have to be created).

'use strict';

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

Thanks for the answer!

4 Likes

Is it still true that nothing custom gets added to /me? If so is there any reason? /me is a really fast way to load a users own info… otherwise you have to do custom stuff… It seems to make sense that if /me automatically loaded custom things added to the user table, people could use Strapi without having to make custom routes all the time… and yet this looks like a custom route is required even though a magic /me route exists… :smile:

Is this for security reasons or an oversight / relic / bug?

Performance was the main reason, a few users ended up adding some extremely large manyToMany relations to user and it caused a lot of problems. We can’t predict what users will add in relation to the user model so we opted to limit it to what we knew by default and let users customize it if needed.

Hi I have a question about this.

How can we populate with a second level relation?

This works but only one level


'use strict';

module.exports = {

  /**

   * Promise to fetch authenticated user.

   * @return {Promise}

   */

  fetchAuthenticatedUser(id) {

    return strapi.query('user', 'users-permissions').findOne({ id }, ['roles_frontend']); 
// added frontend_roles but not his relations

  },

};

This not


'use strict';

module.exports = {

  /**

   * Promise to fetch authenticated user.

   * @return {Promise}

   */

  fetchAuthenticatedUser(id) {

    return strapi.query('user', 'users-permissions').findOne({ id }, ['roles_frontend.opcions_menus']);

  },

};

You need to include both in that array: ['roles_frontend', 'roles_frontend.opcions_menus']

Thanks. I make your midification, but it doesn’t work.

I have this configuration in the roles_frontend.

Is there a relation in the users content-type to Roles_frontend?

Hi Derrick.

Yes it is. Is a has one relation.

Thanks a lot for your help.

Just want to ask, how can I fetch the current logged in username from strapi in NextJS?

GET request to /users/me sending the user’s JWT in the authorization header

Unfortunately, it is not working, can you show how to do that in Nextjs?

When I click on http://localhost:1337/users/me, it appear error:

{“statusCode”:400,“error”:“Bad Request”,“message”:[{“messages”:[{“id”:“No authorization header was found”}]}],“data”:[{“messages”:[{“id”:“No authorization header was found”}]}]}

you aren’t sending the jwt in an authorization header

const cuser= await fetch(`http://localhost:1337/users/me`,{
      method:`GET`,
      headers: {
        Authorization: `Bearer ${jwt}`
      }
    })

Is my code wrong?

Looks fine from there but based on the error, that header isn’t making it through

Hi,
I’ve been having the same problem for days and can’t seem to solve it.
I have modified the User collection inserting inside it a new field called cart.

When I call auth/local after login the user object that comes back to me is populated correctly with the cart object.

The cart object however is an array of products which is another collection I created, and inside each product has the items size, brands and category which are all three fields with 1 to many relationships to their respective tables size, brands, category.

The problem is that these fields are not displayed in the cart object which is inside user.

If I call the endpoint /product I see inside each product also size, brands and category so in the call auth/local I think there is a problem of limitations.

I tried to follow the steps above and also the documentation but I can not solve the problem, I also went to the plug-in user-permission folder inside the node-modules and I modified the User.js file inside the services folder in this way:
by this

fetchAuthenticatedUser(id) {
return strapi.query(‘user’, ‘users-permissions’).findOne({ id }, [‘role’]);
},

to this

fetchAuthenticatedUser(id) {
return strapi.query(‘user’, ‘users-permissions’).findOne({ id }, [‘role’, ‘cart’, ‘cart.size’, ‘cart[*].size’, ‘products’, ‘products.size’, ‘product’, ‘product.size’])
},

but there is nothing to do with the collection size inside cart there is no trace.

how can i solve?

Can anyone help me, I badly need to fix this