[v.4] How to allow user to uplod avatar, cover and update his profile?

OK, I think I managed to solve this… I think the issue was that I tried to create a core controller. Now inside my api/user/controllers/user.js I just export an object with the required methods:

module.exports = {
  async uploadAvatar(ctx) {
    let entity
    const userId = ctx.state.user.id
    if (ctx.is('multipart')) {
      const { data, files } = parseMultipartData(ctx);
      entity = await strapi.service('api::user.user').updateUser(userId, {}, {files})
    } else {
      ctx.request.body.user = ctx.state.user.id;
      entity = await strapi.service('api::user.user').updateUser(userId, ctx.request.body);
    }
    return sanitizeEntity(entity, { model: strapi.models.user });
  },

  // ...other methods
}

Now I’m still not able to test it fully, especially that there are some other issues I haven’t solved yet (for example: parseMultipartData and sanitizeEntity, which used to be a part of 'strapi/utils' and it looks like now they’re not, and it’s not documented anywhere, or an issue with socket.io, which worked great for me in v3, but there were too many breaking changes in v4 and now it doesn’t), but one problem at a time, and at least now my server was able to start properly.

3 Likes