How can I retrieve logged-in user's role with REST API?

Hello. I have three different custom user roles in Settings → Users & Permissions Plugin → Roles, and those are: Company Admin, Company User and Guest User. When I log in as Company Admin or Company User, I need an information about the role, so as to show the user the correct content. The response from the /api/auth/local endpoint is looking like this:

{
    "jwt": "...",
    "user": {
        "id": 5,
        "username": "Lorem ipsum",
        "email": "mail@email.com",
        "provider": "local",
        "confirmed": true,
        "blocked": false,
        "createdAt": "2021-12-20T11:44:45.928Z",
        "updatedAt": "2021-12-20T11:44:45.928Z"
    }
}

Neither on this nor on the /users endpoint do I have the information about the user’s role. How can I retrieve it? Thanks in advance

3 Likes

Extend or add new new controller

"use strict";

module.exports = {

  async find(ctx, next) {

    const { id } = ctx.params;

    return await strapi.query("plugin::users-permissions.user").findOne({

      where: { id },

      populate: ["role"],

    });

  },

};

If u get 404 dont forget to add new endpoint inte admin → roles