# How to search emails under Identity Provider Attributes? (Account Linking)

**URL:** <https://community.auth0.com/t/how-to-search-emails-under-identity-provider-attributes-account-linking/76775>\
**Category:** Get Help\
**Created:** [January 21, 2022, 4:34am UTC](https://community.auth0.com/t/how-to-search-emails-under-identity-provider-attributes-account-linking/76775 "2022-01-21T04:34:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sam\_hatoum](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sam_hatoum/32/34560_2.png) [@sam\_hatoum](https://community.auth0.com/u/sam_hatoum)\
**Post date:** [January 21, 2022, 4:34am UTC](https://community.auth0.com/t/how-to-search-emails-under-identity-provider-attributes-account-linking/76775/1 "2022-01-21T04:34:57Z")

</div>

I’m trying to link accounts using an action.

I have a user registered using the Github social. The primary email of that user is [abc@example.com](mailto:abc@example.com). However, that user also has other emails under the “Identity Provider Attributes” section in a field called `emails`. In those emails, I have [xyz@example.com](mailto:xyz@example.com)

When I sign in with [xyz@example.com](mailto:xyz@example.com), I want to link these accounts.

How can I search for users by emails inside the “Identity Provider Attributes”.emails field?

Thanks

Sam

---

<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:** [January 21, 2022, 7:45pm UTC](https://community.auth0.com/t/how-to-search-emails-under-identity-provider-attributes-account-linking/76775/3 "2022-01-21T19:45:56Z")

</div>

Hi @sam_hatoum,

Thanks for reaching out to the Auth0 Community!

> [@sam\_hatoum](#):
>
> How can I search for users by emails inside the “Identity Provider Attributes”.emails field?

In your Action script, you will need to iterate through the `event.user.identities` array of objects and search for the `email` attribute in the `profileData` object. Note that the first object in the array references the primary account. All other profiles are appended after the primary profile hence why the index begins from the second item. See below of an example.

```auto
exports.onExecutePostLogin = async (event, api) => {
  var index = 1
  while(event.user.identities[index]){
    console.log("Email:", event.user.identities[index].profileData.email)
    index += 1
  }
};

```

Lastly, have you gotten a chance to take a look at the [Auth0 Account Linking Extension](https://auth0.com/docs/customize/extensions/account-link-extension)? It should accomplish the same behavior you seek with Actions.

> **[User Account Linking](https://auth0.com/docs/manage-users/user-accounts/user-account-linking#how-it-works)**
>
> Understand how user accounts can be linked in Auth0 from various identity providers.

Please let me know how this goes for you.

Thanks!

---

<div class="post-metadata">

**Author:** ![sam\_hatoum](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sam_hatoum/32/34560_2.png) [@sam\_hatoum](https://community.auth0.com/u/sam_hatoum)\
**Post date:** [January 21, 2022, 7:51pm UTC](https://community.auth0.com/t/how-to-search-emails-under-identity-provider-attributes-account-linking/76775/4 "2022-01-21T19:51:03Z")

</div>

Hi @rueben.tiow , thanks for the response.

To iterate I think I would need to iterate through all the users right? I’m wanting search the entire user database for anyone that has the intended secondary emails (social connections like Github and Bitbucket have these)

I did look at the account linking extension but it didn’t check the secondary emails, only primary ones.

Another option I can think of is that whenever a user logs in with their Github account is to use that time as an opportunity to update the user’s metadata field with the emails, then use the v3 search to look inside metadata. That could work.

---

<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:** [January 21, 2022, 8:13pm UTC](https://community.auth0.com/t/how-to-search-emails-under-identity-provider-attributes-account-linking/76775/5 "2022-01-21T20:13:13Z")

</div>

Hi @sam_hatoum,

Thank you for your response.

> [@sam\_hatoum](#):
>
> To iterate I think I would need to iterate through all the users right?

Yes, you will likely have to iterate through every user at least once to determine their linked secondary profile.

That is correct. The Account Linking extension only checks for the primary and not the secondary profile because the secondary profile is removed from the user list.

I do believe you could use your suggested approach of storing the emails in the user’s user\_metadata to be searched in the future. 👏

Please let me know how that works for you.

---

<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:** [February 5, 2022, 8:13pm UTC](https://community.auth0.com/t/how-to-search-emails-under-identity-provider-attributes-account-linking/76775/6 "2022-02-05T20:13:35Z")

</div>

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