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

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. 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

When I sign in with 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

Hi @sam_hatoum,

Thanks for reaching out to the Auth0 Community!

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.

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? It should accomplish the same behavior you seek with Actions.

Please let me know how this goes for you.

Thanks!

1 Like

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.

1 Like

Hi @sam_hatoum,

Thank you for your response.

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. :clap:

Please let me know how that works for you.

1 Like

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