# Account linking through Actions

**URL:** <https://community.auth0.com/t/account-linking-through-actions/68940>\
**Category:** Get Help\
**Tags:** actions\
**Created:** [September 1, 2021, 3:33pm UTC](https://community.auth0.com/t/account-linking-through-actions/68940 "2021-09-01T15:33:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_jopear](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/_jopear/32/27831_2.png) [@\_jopear](https://community.auth0.com/u/_jopear)\
**Post date:** [September 1, 2021, 3:33pm UTC](https://community.auth0.com/t/account-linking-through-actions/68940/1 "2021-09-01T15:33:48Z")

</div>

Hi,

lately I’ve been struggling with simple email account linking. I’ve tried the “Account Linking Extension”, but it turned out not to be an option for my use case, because it requires the user to manually confirm the linking though the UI.

Then I tried to use actions (specifically the “Post login” flow) and it indeed links accounts with the same email addresses, but then it returns the original user id.

**For example:**

1. I sign in with GitHub (`github|...`)
2. I sign out
3. I sign in again in with username/password (`auth0|...`)
4. Action correctly links the profile, but it returns the original username/password identity (`auth0|...`). I’d expect to receive the primary user id (`github|...`)

To link accounts I used the `/link` endpoint in the Management API v2.

Is there a way how to return the primary user id after account linking?

---

<div class="post-metadata">

**Author:** ![praveen.addepally](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/praveen.addepally/32/14314_2.png) [@praveen.addepally](https://community.auth0.com/u/praveen.addepally)\
**Post date:** [September 5, 2021, 4:59am UTC](https://community.auth0.com/t/account-linking-through-actions/68940/2 "2021-09-05T04:59:30Z")

</div>

Hello,  
It is important to set the **event.user** object to the primary user after the accounts have been linked in the action. Doing this should return the github user\_id as a sub claim in the id\_token/access token you receive to your application.

Below is a similar post that could be helpful.

> [@Account linking with Auth0 actions](https://community.auth0.com/t/account-linking-with-auth0-actions/60676/2):
>
> Thank you for sharing your solution with the Community @alexab! Quoting your solution here for others to see! slight_smile :

Let us know how it goes!

---

<div class="post-metadata">

**Author:** ![\_jopear](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/_jopear/32/27831_2.png) [@\_jopear](https://community.auth0.com/u/_jopear)\
**Post date:** [September 6, 2021, 7:43am UTC](https://community.auth0.com/t/account-linking-through-actions/68940/3 "2021-09-06T07:43:00Z")

</div>

Hi Praveen, thank you for your reply 🙂

I’ve tried this before, but now I tried it again just in case I was doing something wrong. Unfortunately it seems that neither assigning to the **event.user** or returning it has any effect whatsoever.

I think this was possible before, but now it seems it’s not possible (at least according to your docs):  
[https://auth0.com/docs/actions/programming-model-changes](https://auth0.com/docs/actions/programming-model-changes)

I’ll share the full code of the action just in case I’m doing something wrong:

> <https://gist.github.com/jozefhruska/daed673268cfb91956f2b2a060059cc6>

Thank you.

---

<div class="post-metadata">

**Author:** ![gediminas](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/gediminas/32/25158_2.png) [@gediminas](https://community.auth0.com/u/gediminas)\
**Post date:** [September 6, 2021, 7:43am UTC](https://community.auth0.com/t/account-linking-through-actions/68940/4 "2021-09-06T07:43:16Z")

</div>

Hi there.

This does not seem to be working.

I set **event.user=murgedUser** however, the access token still contains the original users data ☹

I’m not sure if I’'m reading this correctly, but I assume this means, that it is no longer possible to edit the access token without the use of **api.command** or am I missing something?  
[https://auth0.com/docs/actions/programming-model-changes#manipulate-scopes](https://auth0.com/docs/actions/programming-model-changes#manipulate-scopes)

---

<div class="post-metadata">

**Author:** ![minhvtb](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/minhvtb/32/32214_2.png) [@minhvtb](https://community.auth0.com/u/minhvtb)\
**Post date:** [October 8, 2021, 10:04am UTC](https://community.auth0.com/t/account-linking-through-actions/68940/5 "2021-10-08T10:04:45Z")

</div>

Hi Praveen,

I’m having the same problem and have tried set the **event.user**. But this does not seem to be working.  
Is there an official way to link accounts in ‘Post Login’?

Below is my full code of the action  
[Example](https://jsfiddle.net/3nLvoz81/)

Best regards,

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [October 8, 2021, 12:32pm UTC](https://community.auth0.com/t/account-linking-through-actions/68940/6 "2021-10-08T12:32:18Z")

</div>

Hey @praveen.addepally!

Would you be able to follow-up on that?

---

<div class="post-metadata">

**Author:** ![wpeeters](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/wpeeters/32/14071_2.png) [@wpeeters](https://community.auth0.com/u/wpeeters)\
**Post date:** [October 12, 2021, 1:06pm UTC](https://community.auth0.com/t/account-linking-through-actions/68940/7 "2021-10-12T13:06:14Z")

</div>

I’ve the same problem. Has someone found a solution for this problem already?

thx

---

<div class="post-metadata">

**Author:** ![minhvtb](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/minhvtb/32/32214_2.png) [@minhvtb](https://community.auth0.com/u/minhvtb)\
**Post date:** [November 3, 2021, 9:45am UTC](https://community.auth0.com/t/account-linking-through-actions/68940/9 "2021-11-03T09:45:19Z")

</div>

Hi @konrad.sopala, can you follow this? I have been stuck on this problem for a while but no solution yet

---

<div class="post-metadata">

**Author:** ![notVicho](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/notvicho/32/31309_2.png) [@notVicho](https://community.auth0.com/u/notVicho)\
**Post date:** [November 15, 2021, 8:33pm UTC](https://community.auth0.com/t/account-linking-through-actions/68940/10 "2021-11-15T20:33:17Z")

</div>

I have a similar use case that the one explained in this thread and [in this one](https://community.auth0.com/t/account-linking-with-auth0-actions/60676/2).

I’ve been struggling to plan a working solution for the automatic linking of accounts matching their emails. I’ve read all across the docs that this isn’t supported by any consolidated auth0 solutions but I still don’t get why.

Our platforms are for internal use so all users will use our enterprise email addresses and I still can’t think of any worst-case scenario where automatically linking accounts by email can be a risk in any way possible. Sadly we can’t easily integrate this because the account linking extension doesn’t support passwordless connections.

We were very excited when we discovered that action flows might be useful, but when r[eading the docs](https://auth0.com/docs/actions/limitations) it seems that Account Linking isn’t supported!

I guess I’ll try to create a serverless function somewhere that hit the management API to link accounts when necessary, triggered by the post-login trigger, as briefly described [here](https://auth0.com/docs/actions/limitations).

Anybody know if this would work? Any alternatives?

---

<div class="post-metadata">

**Author:** ![leif.ross](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/leif.ross/32/13136_2.png) [@leif.ross](https://community.auth0.com/u/leif.ross)\
**Post date:** [December 31, 2021, 4:24pm UTC](https://community.auth0.com/t/account-linking-through-actions/68940/11 "2021-12-31T16:24:01Z")

</div>

The fact that you cannot log the user in as his/her actual account after linking it makes it impossible for us to migrate from rules to actions, is fixing this on the roadmap, or are you going to revert the deprecated state of rules?

For anyone else currently panicking about rules being deprecated, hold off on trying to migrate until this is resolved. The implementation below works, except the user is still being logged in as the account linked into the actual account. e.g. setting event.user or returning the new user is not working.

We ended up rolling back to using the old rules system for now.

```auto
exports.onExecutePostLogin = async (event, api) => {
  
	const ManagementClient = require("auth0").ManagementClient;
  const DOMAIN = event.secrets.DOMAIN;
	const CLIENT_ID = event.secrets.CLIENT_ID;
	const CLIENT_SECRET = event.secrets.CLIENT_SECRET;

	const management = new ManagementClient({
		domain: DOMAIN,
		clientId: CLIENT_ID,
		clientSecret: CLIENT_SECRET
	});

	let currentAccount = event.user;

  let accounts = await management.getUsersByEmail(currentAccount.email);
  
  if (accounts.length > 2) {
    api.access.deny("[!] Rule: Multiple user profiles already exist - cannot select base profile to link with");
    return;
  } else if (accounts.length === 0) {
    api.access.deny("Unable to login, please contact support if this problem persists");
    return;
  } else if (accounts.length === 2) {
    
    const sorted = accounts.sort((a,b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime());
    const originalUser = sorted[0];
    const newUser = sorted[1];

    // logging back in with original user, confirm
    if (currentAccount.user_id === originalUser.user_id) {
      return;
    }

    // one profile exists, new one is not yet verified
    if (!originalUser.email_verified || !newUser.email_verified) {
      api.access.deny("Please verify your email before logging in.");
      return;
    }

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

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

    event.user = originalUser;
    
    return {
      user: originalUser
    }
  }
};

```

---

<div class="post-metadata">

**Author:** ![alexeyf](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alexeyf/32/34621_2.png) [@alexeyf](https://community.auth0.com/u/alexeyf)\
**Post date:** [February 7, 2022, 3:05pm UTC](https://community.auth0.com/t/account-linking-through-actions/68940/13 "2022-02-07T15:05:16Z")

</div>

Hi @leif.ross,  
May you please share your rules that do account linking?

---

<div class="post-metadata">

**Author:** ![leif.ross](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/leif.ross/32/13136_2.png) [@leif.ross](https://community.auth0.com/u/leif.ross)\
**Post date:** [February 7, 2022, 4:29pm UTC](https://community.auth0.com/t/account-linking-through-actions/68940/14 "2022-02-07T16:29:09Z")

</div>

Sure thing, it looks more or less like this.

```auto
function (user, context, callback) {
  const request = require('request');
  
  const userApiUrl = auth0.baseUrl + '/users';
  const userSearchApiUrl = auth0.baseUrl + '/users-by-email';

  request({
    url: userSearchApiUrl,
    headers: {
      Authorization: 'Bearer ' + auth0.accessToken
    },
    qs: {
      email: user.email
    }
  },
  function(err, response, body) {
    if (err) return callback(err);
    if (response.statusCode !== 200) return callback(new Error(body));

    const data = JSON.parse(body);

    if (data.length === 1) {
      return callback(null, user, context);
    }
    
    if (data.length === 0) {
      return callback(new UnauthorizedError('Unable to login, please contact support if this problem persists'));
    }

    if (data.length > 2) {
      return callback(new Error('[!] Rule: Multiple user profiles already exist - cannot select base profile to link with'));
    }

    // Find original and new user
    const sorted = data.sort((a,b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime());
    const originalUser = sorted[0];
    const newUser = sorted[1];

    // logging back in with original user, confirm
    if (user.user_id === originalUser.user_id) {
      return callback(null, user, context);
    }

    // one profile exists, new one is not yet verified
    if (!originalUser.email_verified || !newUser.email_verified) {
      return callback(new UnauthorizedError('Please verify your email before logging in.'));
    }
 
    const provider = newUser.identities[0].provider;
    const providerUserId = newUser.identities[0].user_id;

    request.post({
      url: userApiUrl + '/' + originalUser.user_id + '/identities',
      headers: {
        Authorization: 'Bearer ' + auth0.accessToken
      },
      json: {
        provider: provider,
        user_id: String(providerUserId)
      }
    }, function(err, response, body) {
      if (response.statusCode >= 400) {
        return callback(new Error('Error linking account: ' + response.statusMessage));
      }
      context.primaryUser = originalUser.user_id;
      callback(null, user, context);
    });
  });
}

```

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [February 8, 2022, 1:10pm UTC](https://community.auth0.com/t/account-linking-through-actions/68940/15 "2022-02-08T13:10:00Z")

</div>

Thanks for sharing that with the rest of community!

---

<div class="post-metadata">

**Author:** ![alexeyf](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alexeyf/32/34621_2.png) [@alexeyf](https://community.auth0.com/u/alexeyf)\
**Post date:** [February 8, 2022, 2:38pm UTC](https://community.auth0.com/t/account-linking-through-actions/68940/16 "2022-02-08T14:38:48Z")

</div>

[konrad.sopala](https://community.auth0.com/u/konrad.sopala)  
May you say what is ETA of support Account Linking in actions is?  
Because now seems it doesn’t work because of [limitations](https://auth0.com/docs/actions/limitations)

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [February 9, 2022, 12:29pm UTC](https://community.auth0.com/t/account-linking-through-actions/68940/18 "2022-02-09T12:29:54Z")

</div>

Hey there!

Let me follow up with the team on that front!

---

<div class="post-metadata">

**Author:** ![taras.slyvka](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/taras.slyvka/32/31474_2.png) [@taras.slyvka](https://community.auth0.com/u/taras.slyvka)\
**Post date:** [February 22, 2022, 12:00pm UTC](https://community.auth0.com/t/account-linking-through-actions/68940/19 "2022-02-22T12:00:19Z")

</div>

@konrad.sopala any updates about this?

---

<div class="post-metadata">

**Author:** ![brunofalletti](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/brunofalletti/32/33467_2.png) [@brunofalletti](https://community.auth0.com/u/brunofalletti)\
**Post date:** [March 9, 2022, 2:53pm UTC](https://community.auth0.com/t/account-linking-through-actions/68940/20 "2022-03-09T14:53:37Z")

</div>

Hi @konrad.sopala  
Is there a recommended solution to this problem?

---

<div class="post-metadata">

**Author:** ![TomBlooz](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@TomBlooz](https://community.auth0.com/u/TomBlooz)\
**Post date:** [March 11, 2022, 4:11am UTC](https://community.auth0.com/t/account-linking-through-actions/68940/21 "2022-03-11T04:11:06Z")

</div>

Same issue encountered in my current test, confirmed with a Auth0 engineer, he replied:

> Similarly, I reviewed your post login flow and I see that you tried to change the event.user to target user. I don’t think this would ever work, since Auth0 has already processed the identity that was used to log in. event.user would only persist as long as you are in the Actions webtask (container running the script).

Which means, in this scenario updating event.user won’t work.

The suggestion was to “redirect the user to another page and log them out to let them log in again”:

> I double checked with my teammates and I’m not sure there is a way to avoid showing the profile of the user that just logged in. The management API call in Rules happens but Auth0 has already processed the user credentials entered. So you would have to log the user back in somehow in order to force the linked accounts to work.

> Typically, our customers redirect a user out of Rules/Actions and present them with a page asking them to link accounts. So that gives the system time to process the link and when the user logs in again, their primary account is used. But I know that you guys are trying to give your users a streamlined experience so you would route the user to another login, at least for the first time their account is linked.

---

<div class="post-metadata">

**Author:** ![leif.ross](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/leif.ross/32/13136_2.png) [@leif.ross](https://community.auth0.com/u/leif.ross)\
**Post date:** [March 11, 2022, 2:15pm UTC](https://community.auth0.com/t/account-linking-through-actions/68940/22 "2022-03-11T14:15:23Z")

</div>

I do not understand how this is a viable solution, it’s literally asking thousands of customers to redo their entire authorization flow. The old rules system should not be deprecated before this is fixed, if not we need an actual warning in due time so we can start migrating to a competitor.

---

<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:** [March 26, 2022, 2:16pm UTC](https://community.auth0.com/t/account-linking-through-actions/68940/23 "2022-03-26T14:16:12Z")

</div>

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

[Next page](https://community.auth0.com/t/account-linking-through-actions/68940.md?page=2)
