# How to refresh the token after merging user & assigning role?

**URL:** <https://community.auth0.com/t/how-to-refresh-the-token-after-merging-user-assigning-role/90143>\
**Category:** Get Help\
**Created:** [September 7, 2022, 12:42pm UTC](https://community.auth0.com/t/how-to-refresh-the-token-after-merging-user-assigning-role/90143 "2022-09-07T12:42:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel.j](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/daniel.j/32/39769_2.png) [@daniel.j](https://community.auth0.com/u/daniel.j)\
**Post date:** [September 7, 2022, 12:42pm UTC](https://community.auth0.com/t/how-to-refresh-the-token-after-merging-user-assigning-role/90143/1 "2022-09-07T12:42:42Z")

</div>

I have a post login action like so

```plaintext
exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://superurlcausewhynot.com';

  const { MGMT_SECRET, MGMT_DOMAIN, MGMT_CLIENT_ID } = event.secrets;
  const auth0 = require('auth0');
  var managementClient = new auth0.ManagementClient({
    domain: MGMT_DOMAIN,
    clientId: MGMT_CLIENT_ID,
    clientSecret: MGMT_SECRET,
  });

  const existingUserAccounts = await managementClient.getUsersByEmail(event.user.email)

  let roles = []

  if (event.authorization) {
    roles = event.authorization.roles
  }

  if (existingUserAccounts.length === 2) {
    const sorted = existingUserAccounts.sort((a,b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime());
    const originalUser = sorted[0];
    const newUser = sorted[1];

    const provider = newUser.identities[0].provider;
    const providerUserId = newUser.identities[0].user_id;

    originalUser.identities = await managementClient.linkUsers(originalUser.user_id, {
      provider: provider,
      user_id: providerUserId
    });

    const assignedRoles = await managementClient.getUserRoles({id: originalUser.user_id})
    roles = assignedRoles
    event.user = originalUser
  }

  if (roles.length === 0 && event.user.email.endsWith("@superdomain.com")) {
    await managementClient.assignRolestoUser({ id: event.user.user_id }, { roles: ['rol_uberAdmin']})
    roles = ['rol_uberAdmin']
  }

  if (roles.length === 0 ){
    return api.access.deny(`Access to ${event.client.name} is not allowed for users without a role assigned.`);
  }
  
  return {
    ...event
  }
}

```

It merges the user from google if they were invited through direct email beforehand and assigns a role to the user if they are from a specific domain.

Now my issue is that the token returned from the call is the original token, ie. it doesn’t contain the role / permissions it should after the new role was assigned or the accounts merged (if the one that is being merged in doesn’t have a role).

Is there some way for force token refresh with current user data at this stage ?

* * *

update:  
seems to be assigning roles and permissions properly for passwordless users, but if the same flow is followed with users coming from the social auth the token doesn’t contain the newly assigned role or it’s permissions.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [September 9, 2022, 4:20pm UTC](https://community.auth0.com/t/how-to-refresh-the-token-after-merging-user-assigning-role/90143/5 "2022-09-09T16:20:39Z")

</div>

Hey @daniel.j welcome to the community!

It looks like my colleague was able to provide a solution for this in another thread - Adding for visibility:

> [@Adding role to new user with email domain IF email is verified](https://community.auth0.com/t/adding-role-to-new-user-with-email-domain-if-email-is-verified/87957/8):
>
> Hi @daniel.j, Thanks for your response. Firstly, a Database or Social Connection will work with a Post-Login Action. Moreover, the examples shown in [this link](https://community.auth0.com/t/how-to-add-roles-and-permissions-to-the-id-token-using-actions/84506) and [my previous post](https://community.auth0.com/t/adding-role-to-new-user-with-email-domain-if-email-is-verified/87957/3) are a Post Login Action. I am not seeing where a Post-User Reg is involved. In this sittuation, you will need to toggle off the feature that Adds permissions in the Access Token and create a Post-Login Action to add the user’s permissions and roles to the token. This will avoid doubly adding the permissions and rol…

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [September 24, 2022, 4:20pm UTC](https://community.auth0.com/t/how-to-refresh-the-token-after-merging-user-assigning-role/90143/6 "2022-09-24T16:20:44Z")

</div>

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