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

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

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