# Adding roles to users post-registration using actions

**URL:** <https://community.auth0.com/t/adding-roles-to-users-post-registration-using-actions/75671>\
**Category:** Get Help\
**Created:** [December 31, 2021, 4:56pm UTC](https://community.auth0.com/t/adding-roles-to-users-post-registration-using-actions/75671 "2021-12-31T16:56:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Skia](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/skia/32/33713_2.png) [@Skia](https://community.auth0.com/u/Skia)\
**Post date:** [December 31, 2021, 4:56pm UTC](https://community.auth0.com/t/adding-roles-to-users-post-registration-using-actions/75671/1 "2021-12-31T16:56:43Z")

</div>

Hello, I’m pretty new to auth0 and what i’m trying to achieve is add specific roles that i have already created through the auth0 dashboard and assign them to certain users based on their email domain name

I used this thread as a reference and followed it step by step: [How can I use the Management API in Actions?](https://community.auth0.com/t/how-can-i-use-the-management-api-in-actions/64947)

I actually tried to pull it off in the post-registration flow as well as in the post-login flow and neither worked for me

Here’s what my action looks like

```auto
exports.onExecutePostUserRegistration= async (event, api) => {
  if (event.stats.logins_count !== 1) {
    return;
  }

  const namespace = "https://my-app.example.com";

  const ManagementClient = require("auth0").ManagementClient;

  const management = new ManagementClient({
    domain: event.secrets.domain,
    clientId: event.secrets.clientId,
    clientSecret: event.secrets.clientSecret,
    scope: "read:roles create:roles update:roles",
  });

  const params = { id: event.user.user_id };
  const defaultRole = { "roles": [event.secrets.defaultRole] };
  const adminRole = { "roles": [event.secrets.adminRole] };

  try {
    if (event.authorization) {
      if (!event.user.email_verified) {
        return;
      } else if (
        event.user.email &&
        event.user.email.endsWith("@admin.com")
      ) {
        api.idToken.setCustomClaim(
          `${namespace}/roles`,
          await management.users.assignRoles(params, adminRole)
        );
        api.accessToken.setCustomClaim(
          `${namespace}/roles`,
          await management.users.assignRoles(params, adminRole)
        );
      } else {
        api.idToken.setCustomClaim(
          `${namespace}/roles`,
          await management.users.assignRoles(params, defaultRole)
        );
        api.accessToken.setCustomClaim(
          `${namespace}/roles`,
          await management.users.assignRoles(params, defaultRole)
        );
      }
    }
  } catch (e) {
    console.log(e);
  }
};

```

Could someone be kind enough to help me point out what i’m doing wrong please?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [January 4, 2022, 1:29am UTC](https://community.auth0.com/t/adding-roles-to-users-post-registration-using-actions/75671/3 "2022-01-04T01:29:44Z")

</div>

Hi @Skia,

Thanks for reaching out to the Auth0 Community!

After taking a look at your Action, I found that you are trying to both assign the user roles and set the roles as a custom claim simultaneously.

In this scenario, I recommend separately these tasks and using the [`management.roles.assignUsers`](https://auth0.github.io/node-auth0/module-management.RolesManager.html#assignUsers) method instead.

So far, I found issues with the `management.users.assignRoles` and `management.assignRolestoUser` methods working incorrectly. As a workaround, you could use the `management.roles.assignUsers` for a single user, when normally meant for assigning many users.

Here is a revised version of your Action:

```auto
exports.onExecutePostUserRegistration= async (event, api) => {
  if (event.stats.logins_count !== 1) {
    return;
  }

  const namespace = "https://my-app.example.com";

  const ManagementClient = require("auth0").ManagementClient;

  const management = new ManagementClient({
    domain: event.secrets.domain,
    clientId: event.secrets.clientId,
    clientSecret: event.secrets.clientSecret,
    scope: "read:roles create:roles update:roles",
  });

  const defaultRole = { id :'YOUR_DEFAULT_ROLE_ID_HERE'};
  const adminRole = { id :'YOUR_ADMIN_ROLE_ID_HERE'};
  var data = { "users" : [event.user.user_id]};

  try {
    if (event.authorization) {
      if (!event.user.email_verified) {
        return;
      } else if (event.user.email && event.user.email.endsWith("@arcadous.com")) {
          api.idToken.setCustomClaim(`${namespace}/roles`, event.secrets.adminRole);
          api.accessToken.setCustomClaim(`${namespace}/roles`, event.secrets.adminRole);
          await management.roles.assignUsers(adminRole, data);
      } else {
          api.idToken.setCustomClaim(`${namespace}/roles`, event.secrets.defaultRole);
          api.accessToken.setCustomClaim(`${namespace}/roles`, event.secrets.defaultRole);
          await management.roles.assignUsers(defaultRole, data);
      }
    }
  } catch (e) {
    console.log(e);
  }

};

```

Please let me know how this works for you.

Thank you.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [January 19, 2022, 1:30am UTC](https://community.auth0.com/t/adding-roles-to-users-post-registration-using-actions/75671/4 "2022-01-19T01:30:22Z")

</div>

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