Is it possible to know user role on authentication

System Information
  • Strapi Version: 4.0.0
  • Operating System: MacOS
  • Database: PostgreSQL
  • Node Version: 16.13.1
  • NPM Version:
  • Yarn Version:

When logging in as a User using the ‘/auth/local’ call. Is it possible to also return the Role of that given user?

1 Like

When you login with a user you will get a token back. You can then use the token and make a call to /users/me and it will give the role back in that response. It’s an extra call but saves you from modifying the auth local login provider etc.

In the response you can do
response.role.name to get the name of the role it’s an object looking like this

  "role": {
    "id": "string",
    "name": "string",
    "description": "string",
    "type": "string",
    "permissions": [
      "string"
    ],
    "users": [
      "string"
    ],
    "created_by": "string",
    "updated_by": "string"
  },
2 Likes

Tried that, a call to api/users/me with the bearer token returns the user data and not the role of the user. What can I do about that?

3 Likes

Same here I made sure to give the role permissions to find and findOne role too. Doesn’t seem like the /me endpoint returns that data.

1 Like

Since Strapi version 4.2.2 the route api/users/me no longer returns the role field in the user data but accepts populating (see https://github.com/strapi/strapi/issues/13296).
So now to get the user’s role you need to populate the role field, thus the call will be api/users/me?populate=role.
Note that you also need to give the role permissions to find role too.

1 Like

Add the ‘populate’ query like this: /api/users/me?populate=* This will return everything including the role of the user.

2 Likes

thanks man that’s work fine

1 Like

Dont forget to allow role in perms:
image

Otherwise you will not see roles, even with populate.

1 Like