Writing an Action to combine User Roles and Member Roles

Ready to post? :mag: First, try searching for your answer.
I’m trying to create a post login action to combine user roles (assigned in Console / Users) with member roles (Console / Organizations / Members / Roles) to provide my backend with a consistent set of roles. I don’t want to be sensitive to how the roles were assigned, just to understand that the role was assigned. However my code below still doesn’t seem to receive the user roles. Any suggestions?

exports.onExecutePostLogin = async (event, api) => {
    const namespace = "com.mydomain.";

    const orgRoles = (event.authorization && event.authorization.roles) ? event.authorization.roles : [];
    const userRoles = (event.user && event.user.roles) ? event.user.roles : [];

    const roles = Array.from(new Set([...orgRoles, ...userRoles]));

    if (roles.length > 0) {
        api.accessToken.setCustomClaim(namespace + "roles", roles);
    }

};

Hi @jonathan74

Welcome to the Auth0 Community!

Thank you for posting your question. We have an excellent knowledge solution that covers the topic of assigning the roles and permission to the access token → How to Add Roles and Permissions to the ID Token Using Actions

Regarding your code, the event.user.roles is populated if you are using the Authorization Extension; if not, all user roles will be set at the event.authentication.roles level.

Thanks
Dawid

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.