# How to pre-assign roles to a user based on user emails with Authorization Extension?

**URL:** <https://community.auth0.com/t/how-to-pre-assign-roles-to-a-user-based-on-user-emails-with-authorization-extension/26071>\
**Category:** Get Help\
**Tags:** authorization-extens\
**Created:** [June 4, 2019, 2:27am UTC](https://community.auth0.com/t/how-to-pre-assign-roles-to-a-user-based-on-user-emails-with-authorization-extension/26071 "2019-06-04T02:27:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andre.hermanto93](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/andre.hermanto93/32/13514_2.png) [@andre.hermanto93](https://community.auth0.com/u/andre.hermanto93)\
**Post date:** [June 4, 2019, 2:27am UTC](https://community.auth0.com/t/how-to-pre-assign-roles-to-a-user-based-on-user-emails-with-authorization-extension/26071/1 "2019-06-04T02:27:23Z")

</div>

Hi,

I would like to pre-defined roles to some users based on the email list I have, so that whenever that person register an account it would automatically assigned to a role if their email match with one of my email list.

I’m using Authorization Extension to manage roles. Can anyone help me how to do this?

Cheers,  
Andre

---

<div class="post-metadata">

**Author:** ![andre.hermanto93](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/andre.hermanto93/32/13514_2.png) [@andre.hermanto93](https://community.auth0.com/u/andre.hermanto93)\
**Post date:** [June 5, 2019, 6:19am UTC](https://community.auth0.com/t/how-to-pre-assign-roles-to-a-user-based-on-user-emails-with-authorization-extension/26071/2 "2019-06-05T06:19:12Z")

</div>

Can anyone help regarding this topic please?

I tried using rules to call authorization extension API to add user to a role, the problem with this method is that user who just register will not immediately get the access, instead the role will only appear after the 2nd login.

I also tried using hooks, I tried both post-registration and pre-registration.

For post registration, I don’t know why but my script to use the authorization extension API seems not executed properly. I can see from the log the it is indeed calling the API to assign the role to the user, but the user permission is not updated.

For pre registration, I can’t use it because in pre-registration script user\_id does not exist, and this is needed for calling auth extension API for adding user to roles.

---

<div class="post-metadata">

**Author:** ![yan.gao](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/yan.gao/32/49098_2.png) [@yan.gao](https://community.auth0.com/u/yan.gao)\
**Post date:** [July 20, 2023, 7:07pm UTC](https://community.auth0.com/t/how-to-pre-assign-roles-to-a-user-based-on-user-emails-with-authorization-extension/26071/3 "2023-07-20T19:07:03Z")

</div>

I encountered same issue, any solution to that?

---

<div class="post-metadata">

**Author:** ![yan.gao](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/yan.gao/32/49098_2.png) [@yan.gao](https://community.auth0.com/u/yan.gao)\
**Post date:** [July 20, 2023, 7:17pm UTC](https://community.auth0.com/t/how-to-pre-assign-roles-to-a-user-based-on-user-emails-with-authorization-extension/26071/5 "2023-07-20T19:17:36Z")

</div>

But instead of using rules. I was using action in onExecutePostLogin. I get log saids:

 ![image](https://global.discourse-cdn.com/auth0/original/3X/1/9/19135df486f6da9f61ef392185c09c7eb96369b2.png)

Which in first login that happens right after user signup, it failed. (although Assign roles to a user is happen before login fail, my guess will be it uses the old token with no role to attempt the login)Then If I try login again, it will work since the role is assigned. I guess there’s some state missmatch, I want to know if there’s solution to address this so use can login right after they signup

```auto
exports.onExecutePostLogin = async (event, api) => {
  event.user.app_metadata = event.user.app_metadata || {};
  if (event.user.app_metadata.INITIAL_ROLE_ASSIGNED) return; 
  // Do initial signup stuff (role assignment) here
  const ManagementClient = require('auth0').ManagementClient;

  const management = new ManagementClient({
      domain: event.secrets.domain,
      clientId: event.secrets.client_id,
      clientSecret: event.secrets.client_secret,
  });
  const params = { id : event.user.user_id};
  const all_application_access_role = { "roles" : ["rol_3x1T5kuIDdkKQJQu"]};

  try {
    // if user email end with @ ****.com, assign all application access
    if (event.user.email && event.user.email.endsWith("@ ****.com")) {
      const res = await management.assignRolestoUser(params, all_application_access_role)
    } 
  } catch (e) {
    console.log(e)
  } 

  api.user.setAppMetadata('INITIAL_ROLE_ASSIGNED', true);
};

```
