Get user role with the user informations after authentication via auth/local (Strapi v4)

System Information
  • Strapi Version: 4.1.10
  • Operating System: OSX
  • Database: MySQL
  • Node Version: v14.16.0
  • NPM Version: v8.10.0
  • Yarn Version: -

In Strapi v3 it was possible to use the role attribute of the authenticated user from the response-json directly after the login to use it for some authorization things in frontend. At that time the role attribute was populated OOTB.

Since Strapi v4 the role attribute is not populated anymore. Now i’m searching for a clean way to bring back the role attribute within the “auth/local” response body again.

I already extended the “plugin.controllers.auth.callback” to get the populated role with the user, but the sanitizing of the contentAPI (sanitize.contentAPI.output(user, userSchema, {auth})) is removing the role again. I want to keep the sanitizer. So I need to adjust the schema of the user content-type to prevent the sanitizer from removing the role. But how?

Do you have any ideas or know any other solutions for that?

Thank you

This was a major issue since v4 was introduced but should be resolved now, try updating.

Relevant issue with other workarounds: Population does not work for Users in Users-Permissions · Issue #11957 · strapi/strapi · GitHub

1 Like

Thanks for your help. An update to the current version didn’t help. I used the workaround with population and manual sanitizing like here and it works. This issue is really disappointing…