/api/users endpoint is not sending relations and media fields

I just found the answer guys, there is a bug in the roles & permissions plugin related to population. There is an issue opened on Github that probably will be merged soon: https://github.com/strapi/strapi/issues/11957

You can use this workaround in the meantime:

  1. Open your Strapi app on a code editor. (Make sure your app is not running during the process)
  2. Create a file called 'strapi-server.js’ route ‘src/extensions/users-permissions/strapi-server.js’ if it doesn’t exist and add the following.
// path: src/extensions/users-permissions/strapi-server.js
module.exports = plugin => {
  const sanitizeOutput = (user) => {
    const {
      password, resetPasswordToken, confirmationToken, ...sanitizedUser
    } = user; // be careful, you need to omit other private attributes yourself
    return sanitizedUser;
  };

  plugin.controllers.user.me = async (ctx) => {
    if (!ctx.state.user) {
      return ctx.unauthorized();
    }
    const user = await strapi.entityService.findOne(
      'plugin::users-permissions.user',
      ctx.state.user.id,
      { populate: ['role'] }
    );

    ctx.body = sanitizeOutput(user);
  };

  plugin.controllers.user.find = async (ctx) => {
    const users = await strapi.entityService.findMany(
      'plugin::users-permissions.user',
      { ...ctx.params, populate: ['role', 'avatar'] }
    );

    ctx.body = users.map(user => sanitizeOutput(user));
  };

  return plugin;
};
  1. Note that you have a controller for every endpoint, findOne, find and so on…
    you need to add the fields you want to populate to the 'populate: ’ values as an array of strings. Example:
{ ...ctx.params, populate: ['role', 'avatar', 'my_fields'] }
  1. You need to run a build
npm run build
  1. Start your project
npm run start
  1. Try fetching from the users or users/me endpoint
http://localhost:1337/api/users
3 Likes