# Isolation guarantees in Actions

**URL:** <https://community.auth0.com/t/isolation-guarantees-in-actions/130200>\
**Category:** Get Help\
**Tags:** account-linking, user-management\
**Created:** [March 21, 2024, 9:00pm UTC](https://community.auth0.com/t/isolation-guarantees-in-actions/130200 "2024-03-21T21:00:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel26](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/daniel26/32/48874_2.png) [@daniel26](https://community.auth0.com/u/daniel26)\
**Post date:** [March 21, 2024, 9:00pm UTC](https://community.auth0.com/t/isolation-guarantees-in-actions/130200/1 "2024-03-21T21:00:31Z")

</div>

Hi. I’ve written a post-login action that prevents users logging in with multiple identities for a given email-i query the management API’s usersByEmail function and deny if the identity being used to log in doesn’t match one of the earliest created identities.

I’m worried that if a user logs in with two different methods simultaneously, that neither check with the management API will reveal the alternate login method and thus the user could be allowed to login with two methods with the same email at the same time.

Is there any way to guarantee some kind of isolation or other mechanism to avoid this situation?

---

<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:** [March 22, 2024, 4:01pm UTC](https://community.auth0.com/t/isolation-guarantees-in-actions/130200/3 "2024-03-22T16:01:05Z")

</div>

Hi @daniel26,

Welcome to the Auth0 Community!

> [@daniel26](#):
>
> Hi. I’ve written a post-login action that prevents users logging in with multiple identities for a given email-i query the management API’s usersByEmail function and deny if the identity being used to log in doesn’t match one of the earliest created identities.

To better understand your scenario, could you please share your Action script?

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![daniel26](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/daniel26/32/48874_2.png) [@daniel26](https://community.auth0.com/u/daniel26)\
**Post date:** [March 23, 2024, 2:38am UTC](https://community.auth0.com/t/isolation-guarantees-in-actions/130200/4 "2024-03-23T02:38:32Z")

</div>

Sure. Below is a condensed version of the action.

Specifically, I just need to know how to guarantee, if possible, that a user has no other logins under the same email. The worry is that two simultaneous calls to `management.usersByEmail.getByEmail` from different accounts sharing an email could fail to reveal each other.

```javascript
function findObjectsWithMinCreatedAt(objects) {
    // Find the minimum value for the "created_at" property
    const minCreatedAt = Math.min(...objects.map(obj => (new Date(obj.created_at)).getTime()))
    const objectsWithMinCreatedAt = objects.filter(obj => (new Date(obj.created_at)).getTime() === minCreatedAt);
    return objectsWithMinCreatedAt;
}

function identitiesOverlap(identities1, identities2) {
  const ids1 = new Set(identities1.map((identity) => JSON.stringify([identity.provider, identity.user_id])))
  return identities2.filter((identity) => ids1.has(JSON.stringify([identity.provider, identity.user_id]))).length !== 0;
}
exports.onExecutePostLogin = async (event, api) => {
  const ManagementClient = require('auth0').ManagementClient;
  const management = new ManagementClient({
      domain: event.secrets.domain,
      clientId: event.secrets.client_id,
      clientSecret: event.secrets.client_secret,
  });

  const res = await (async () => {try {
    return await management.usersByEmail.getByEmail({email: event.user.email, fields: 'created_at,identities', include_fields: true});
    } catch (e) {
      console.error(e)
      api.access.deny('internal_error', 'no idea what happen')
  }})()
  if (!res) {return;}
  
  const identities = findObjectsWithMinCreatedAt(res.data).flatMap((user) => user.identities);
  
  if (!identitiesOverlap(identities, event.user.identities)) {
    const prettyProviders = identities.map(identity => PROVIDER_NAMES[identity.provider] || identity);
    api.access.deny('no_multiple_accounts_for_one_email', "An account by this email exists under a different login method. Please login with any of " + identity.map(identity => identity.provider).join(',') + '.')
  }
};

```

---

<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:** [April 12, 2024, 5:13pm UTC](https://community.auth0.com/t/isolation-guarantees-in-actions/130200/5 "2024-04-12T17:13:03Z")

</div>

Hi @daniel26,

Thanks for sharing your Action script.

> [@daniel26](#):
>
> The worry is that two simultaneous calls to `management.usersByEmail.getByEmail` from different accounts sharing an email could fail to reveal each other.

This should not be a worry since you are using Post Login aciton script. In this situation, there is an assumption that the user was already created (signed up) on your applicaiton, which means that their identities should be linked before they login.

And because these calls are synchrounous, you can be certain that simulateneous calls will associate the user’s identities correctly.

I hope this was clear!

Let me know if you have any questions.

Thanks,  
Rueben

---

<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:** [April 26, 2024, 5:13pm UTC](https://community.auth0.com/t/isolation-guarantees-in-actions/130200/6 "2024-04-26T17:13:13Z")

</div>

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