GET /users/me return avatar & relations #7268

This discussion has been migrated from our Github Discussion #7268


BioSs54145d ago

Hello,

I have a few questions about Strapi. I checked the documentation but couldn’t find answers to my questions.

I have an “avatar” field, of type Media, when I call /auth/local, I get my field and when I call /users/me I don’t have it. I read that maybe a field in extensions/users-permissions needed to be changed? Which ?
Same question with my relation called “groups” in User. (User: groups <-> Group: members)

For informations, Swagger Schema @ doc is good :

id* string


username* stringminLength: 3
email* stringminLength: 6
provider string
confirmed booleandefault: false
blocked booleandefault: false
role {…}
bio string
address {…}
mobilePhone string
alerts {…}
groups […]
avatar* {…}
created_by {…}
updated_by {…}

And already try to edit /extensions/users-permissions/config/schema.graphql.js

module.exports = {
  definition: `
    extend type UsersPermissionsMe {
      avatar: UploadFile
      groups: Group
    }
  `
}

with Query

query {
  me {
    username
    email
    avatar {
      url
    }
    groups {
      name
    }
  }
}

i get :

{
  "data": {
    "me": {
      "username": "MikaDo",
      "email": "mikado@blabla.org",
      "avatar": null,
      "groups": null
    }
  }
}

Can someone help me :slight_smile:

Thank you.

Responses to the discussion on Github


lauriejim144d ago

Maintainer

hello! please follow these instruction - #7231
really similar to your point, you will have to return avatar and groups in this function.


BioSs54144d ago

Author

Hmmm…

Its didnt work…

Or i made a mistake ?

/extensions/users-permissions/controllers/User.js

const { sanitizeEntity } = require('strapi-utils');

const sanitizeUser = user =>
  sanitizeEntity(user, {
    model: strapi.query('user', 'users-permissions').model,
  });

module.exports = {
  /**
   * Retrieve authenticated user.
   * @return {Object|Array}
   */
  async me(ctx) {
    const { id } = ctx.state.user.id;
    let data = await strapi.plugins['users-permissions'].services.user.fetch({
      id,
    });

    if (data) {
      data = sanitizeUser(data);
    }

    // Send 200 `ok`
    ctx.body = data;
  },
};

lauriejim142d ago

Maintainer

I just tried on my side with your code and it works well ^^
So I don’t know… did you do something else?


lauriejim142d ago

Maintainer