# Assign role after verify account with the email link

**URL:** <https://community.auth0.com/t/assign-role-after-verify-account-with-the-email-link/63477>\
**Category:** Get Help\
**Created:** [May 27, 2021, 10:37pm UTC](https://community.auth0.com/t/assign-role-after-verify-account-with-the-email-link/63477 "2021-05-27T22:37:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jorge.vazquez](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jorge.vazquez/32/25731_2.png) [@jorge.vazquez](https://community.auth0.com/u/jorge.vazquez)\
**Post date:** [May 27, 2021, 10:37pm UTC](https://community.auth0.com/t/assign-role-after-verify-account-with-the-email-link/63477/1 "2021-05-27T22:37:21Z")

</div>

Hello everyone, I need to assign a role a user who use the verification link in his email, I did this rule:

```auto
function (user, context, callback) { 
  if(user.email_verified){
    const assignedRoles = (context.authorization || {}).roles;
    if(assignedRoles.length > 0){
      return callback(null, user, context);
    }else{
      const params = { id : user.user_id};
      const data = { "roles" : ["ID_ROLE"]};
      const ManagementClient = require('auth0@2.34.2').ManagementClient;
      const management = new ManagementClient({token: auth0.accessToken,domain: auth0.domain});
      management.users.assignRoles(params, data, function (err, user) {
        if (err) {console.log(err);}
        callback(null, user, context);
      });
    }
  }else{
    return callback(null, user, context);
  } 
}

```

But, the problem is that rule execute when the user login after he verified his account, but to reflect the list of the roles in the token the user needs to refresh the page or logout and login another time, but I need to execute the assign of the role with the email verification action

Thanks a lot for your help

---

<div class="post-metadata">

**Author:** ![stephanie.chamblee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stephanie.chamblee/32/26087_2.png) [@stephanie.chamblee](https://community.auth0.com/u/stephanie.chamblee)\
**Post date:** [May 28, 2021, 2:30pm UTC](https://community.auth0.com/t/assign-role-after-verify-account-with-the-email-link/63477/3 "2021-05-28T14:30:51Z")

</div>

Hi @jorge.vazquez,

Welcome to the Community!

Unfortunately, rules only run after successful authentication, and the email verification link will not authenticate the user. The most similar flow possible when forcing email verification would look like this:

1. User signs up with database connection (email/password)
2. User is redirected back to the app with an `unauthorized` error and shown a message that they must first verify their email address.
  - The rule will send the user back to your app with an error in the URL.
  - Your app will use the error in the URL to display a user-friendly message.
  - Force email verification example: [Verify Emails using Auth0](https://auth0.com/rules/email-verified)

3. The user will verify their email address and they will click a button to go back to the app (you will need to configure an **Application Login URI** to show the button on the email verification page when using the New Universal Login)
4. The user will have to log in again to be assigned the roles.

**Related topic:**

> [@Create session after email verification](https://community.auth0.com/t/create-session-after-email-verification/37879/4):
>
> Email verification does not create a session, so if they had not logged in earlier there will be no active session in the browser. If you expect the user to log in right after verification, they should be redirected to the login page. On the other hand, if they had gone through a regular signup flow, they’d have been logged in automatically after signup - which would leave an active session in the browser. Hope that helps.

---

<div class="post-metadata">

**Author:** ![jorge.vazquez](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jorge.vazquez/32/25731_2.png) [@jorge.vazquez](https://community.auth0.com/u/jorge.vazquez)\
**Post date:** [May 28, 2021, 2:44pm UTC](https://community.auth0.com/t/assign-role-after-verify-account-with-the-email-link/63477/4 "2021-05-28T14:44:37Z")

</div>

Hi @stephanie.chamblee

Thanks a lot for your help. I’m going to do that.

---

<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:** [June 12, 2021, 2:45pm UTC](https://community.auth0.com/t/assign-role-after-verify-account-with-the-email-link/63477/5 "2021-06-12T14:45:20Z")

</div>

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