# Adding role to new user with email domain IF email is verified

**URL:** <https://community.auth0.com/t/adding-role-to-new-user-with-email-domain-if-email-is-verified/87957>\
**Category:** Get Help\
**Tags:** actions, email-verification, login, roles\
**Created:** [August 1, 2022, 10:52am UTC](https://community.auth0.com/t/adding-role-to-new-user-with-email-domain-if-email-is-verified/87957 "2022-08-01T10:52:06Z")\
**Posts on this page:** 1\
**Showing post:** 3

<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:** [August 1, 2022, 4:59pm UTC](https://community.auth0.com/t/adding-role-to-new-user-with-email-domain-if-email-is-verified/87957/3 "2022-08-01T16:59:41Z")

</div>

Hi @EML,

Welcome to the Auth0 Community!

I understand that you would like to add a role to a new user with a specific email domain provided that their email has been verified.

> [@EML](#):
>
> The issue I see with this is that presumably I cannot check for email verification before adding the user role in the post-registration action - because the user would not have had time to receive and click on the verification link.

In this situation, I recommend using a Post-Login Action script to add a role to the user only if their email has been verified and matches a specific domain.

Because we are using a Post-Login Action script and would like to only assign the user role once, we should include a user\_metdata check for a single execution. Here is an example:

```auto
exports.onExecutePostLogin = async (event, api) => {
  if (!event.user.email_verified || !event.user.email.endsWith("@example.com")){
      api.access.deny(`Access to ${event.client.name} is not allowed.`);
   }
  
  if(!event.user.user_metadata.assignedRole){
    const ManagementClient = require('auth0').ManagementClient;

    const management = new ManagementClient({
        domain: event.secrets.domain,
        clientId: event.secrets.clientId,
        clientSecret: event.secrets.clientSecret,
    });

    const params = { id : event.user.user_id};
    const data = { "roles" : ["ROLE_ID"]};
    
    try {
        const res = await management.assignRolestoUser(params, data)
        api.user.setUserMetadata("assignedRole", true)
    } catch (e) {
        console.log(e)
        // Handle error
    }
  }
};

```

Here are some helpful resources you may find relevant:

- [How can I use the Management API in Actions?](https://community.auth0.com/t/how-can-i-use-the-management-api-in-actions/64947)
- [Actions Triggers: post-login - Event Object](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow/event-object)
- [Actions Triggers: post-login - API Object](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow/api-object)

I hope this helps!

Please let me know how this works for you or if you have any additional questions.

Thank you.

---

_[View the full topic](https://community.auth0.com/t/adding-role-to-new-user-with-email-domain-if-email-is-verified/87957)._
