Missing user role in lifecycle event

Hi.

I’m trying, after creating a user, to put that user in a Mailgun mailing list according to the user’s role. However, the lifecycle event does not hold that information, just the role count role: { count: 1 }. It’s the same thing with the login response, where the user role is also hidden so we have to have another fetch in order to get it.

Is there a way to get the user role form the lifecycle? And also, is there a reason that I’m not seeing that makes Strapi hide user roles?

Cheers.

Is there a way to get the user role form the lifecycle?

Not directly. You need to re-fetch the information you need within your hook. I think it’s a good practice to always do this as usually the hook doesn’t have controll over the parameters the hooked action was called with. For instance, even if your hook depends on a scalar field, you can’t stop the action to be called with fields: [<empty array>] which will make the field unavailable in the event object.

And also, is there a reason that I’m not seeing that makes Strapi hide user roles?

I’d bet the rationale is to only expose the information that needs to be exposed and the role is not strictly necessary so by default it’s not visible. OTOH I really don’t see a reason for why the role cannot be fetched from the login endpoint by using e.g. ?populate=role given the appropriate permissions are met (the user’s role has rights to find the role resource). ./

Thank you @goodhoko for your time and effort in clarifying this for me.

btw @WebShapedBiz While you can’t fetch the role within the login request, after logging-in you can use the /api/users/me endpoint with a populate parameter to fetch the role. ,)

Thank you for this reminder @goodhoko, I’m already using that as a part of my auth process. Still believe the role should be there because if you need authentication you are more than likely going to need authorization as well. Furthermore, role was present in the login response in Strapi V3 so somebody made a deliberate effort and removed it.
Anyhow, thank you for the effort.

Thank you very much for your hard work.