# Account linking through actions \[Critical issue\]

**URL:** <https://community.auth0.com/t/account-linking-through-actions-critical-issue/76484>\
**Category:** Get Help\
**Tags:** actions\
**Created:** [January 16, 2022, 2:08pm UTC](https://community.auth0.com/t/account-linking-through-actions-critical-issue/76484 "2022-01-16T14:08:32Z")\
**Posts on this page:** 11\
**Page:** 1

<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:** [January 16, 2022, 2:08pm UTC](https://community.auth0.com/t/account-linking-through-actions-critical-issue/76484/1 "2022-01-16T14:08:32Z")

</div>

Posting this as a separate topic as the comment did not get any response.

I marked this one as critical because this will screw over a significant portion of your existing customers if you go live. This is completely necessary functionality for a authentication provider.

If this goes live, our system will break, and so will countless others. Especially those with databases that rely on emails being unique.

The problem is that it is not currently possible to replicate the behaviour of the old account linking rule with the new action system, this is because it is not possible to seamlessly switch the user to the primary account after linking has happened.

The code below should work, but it does not. It links the accounts, but the user starts his/her session with the secondary account, which at this point does not even exist. This leads to all sorts of wacky behaviour.

The extension can not replicate the necessary behaviour either, as there is no way to disable the option that lets the user proceed without linking.

```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
    }
  }
};

```

I see three potential solutions:

1. Don’t deprecate the rules system, at the very least push it back until this is resolved, and everyone has had enough time to migrate away after it is possible to do so (Currently there is no way out).
2. Allow the account linking extension to force the users to either link accounts or abandon the signup.
3. Fix the Action system so that it can replicate the behaviour of the rule system.

---

<div class="post-metadata">

**Author:** ![joseph.pasque](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/joseph.pasque/32/33350_2.png) [@joseph.pasque](https://community.auth0.com/u/joseph.pasque)\
**Post date:** [October 14, 2022, 2:21pm UTC](https://community.auth0.com/t/account-linking-through-actions-critical-issue/76484/3 "2022-10-14T14:21:18Z")

</div>

I know this is many months old now, but we are running into the same issue currently, were you able to find any way around this behavior?

---

<div class="post-metadata">

**Author:** ![philip.skinner](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@philip.skinner](https://community.auth0.com/u/philip.skinner)\
**Post date:** [March 15, 2023, 1:56pm UTC](https://community.auth0.com/t/account-linking-through-actions-critical-issue/76484/4 "2023-03-15T13:56:48Z")

</div>

I’m having this same issue now, linking the accounts post login logs you in with the secondary rather than the primary.

+1 on having this fixed

---

<div class="post-metadata">

**Author:** ![gparascandolo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/gparascandolo/32/38788_2.png) [@gparascandolo](https://community.auth0.com/u/gparascandolo)\
**Post date:** [March 15, 2023, 2:56pm UTC](https://community.auth0.com/t/account-linking-through-actions-critical-issue/76484/5 "2023-03-15T14:56:05Z")

</div>

I’ll throw my +1 to this for visibility - Because of account linking limitations in Auth0 Actions, we are forced to use a mixture of Auth0 Rules + Auth0 Post-Login flow Actions.

However, this mixture of Rules + Actions leads to unexpected behavior when users initiate account linking for their new accounts.

Although we set the `user` object and `context.primaryUser` value to the user’s existing primary account in the Auth0 Rule, the subsequent Auth0 Actions in the Post-Login flow still has the user’s secondary account information in the `event.user` object.  
If there are any `api.user.setAppMetadata` or `api.user.setUserMetadata` calls in the Auth0 Actions, the login flow breaks since the secondary account is no longer available.

---

<div class="post-metadata">

**Author:** ![CumpsD](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/cumpsd/32/46005_2.png) [@CumpsD](https://community.auth0.com/u/CumpsD)\
**Post date:** [April 1, 2023, 8:57pm UTC](https://community.auth0.com/t/account-linking-through-actions-critical-issue/76484/7 "2023-04-01T20:57:17Z")

</div>

What’s the status on this? Is there a workaround?

---

<div class="post-metadata">

**Author:** ![fran.pregernik](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/fran.pregernik/32/45620_2.png) [@fran.pregernik](https://community.auth0.com/u/fran.pregernik)\
**Post date:** [April 2, 2023, 6:42am UTC](https://community.auth0.com/t/account-linking-through-actions-critical-issue/76484/8 "2023-04-02T06:42:54Z")

</div>

I am also interested in this topic.

Below is my rule workaround attempt (WIP). Some context… we migrated the users into a Auth0 database and have set up a rule to detect social logins with the same email as in the migrated data.

```auto
async function (user, context, callback) {
    const ManagementClient = require('auth0').ManagementClient;
    const management = new ManagementClient({
        token: auth0.accessToken,
        domain: auth0.domain
    });

    console.log(user);

    if (!user.user_id.startsWith("auth0|")) {
        // link social account with migrated
        try {
            const users = (await management.getUsersByEmail(user.email)) || [];
            const dbUsers = users
                .filter(u => u.user_id !== user.user_id)
                .filter(u => u.user_id.startsWith("auth0|"));

            if (dbUsers.length === 1) {
                const migratedUser = dbUsers[0];
                const linkedUser = await management.linkUsers(migratedUser.user_id, { user_id: user.user_id, provider: "google-oauth2" });
                console.log(linkedUser);

                context.primaryUser = migratedUser.user_id;
            }
        } catch (e) {
            console.error("Unable to link accounts", e);
        }
    }

    console.log(context);

    callback(null, user, context);
}

```

There is some code omitted at the end that fetches the permissions from the DB user and puts them in the context.

---

<div class="post-metadata">

**Author:** ![miguel.figueira](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/miguel.figueira/32/51266_2.png) [@miguel.figueira](https://community.auth0.com/u/miguel.figueira)\
**Post date:** [October 9, 2023, 4:51pm UTC](https://community.auth0.com/t/account-linking-through-actions-critical-issue/76484/9 "2023-10-09T16:51:06Z")

</div>

hey, I had the same issue but was able to solve it with some limitations.

Please check my reply [here](https://community.auth0.com/t/allow-for-account-linking-with-actions/109290/8).

(Basically the difference the original approach is to invert the linking order)

---

<div class="post-metadata">

**Author:** ![aaron-ps](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/aaron-ps/32/53559_2.png) [@aaron-ps](https://community.auth0.com/u/aaron-ps)\
**Post date:** [December 29, 2023, 1:41pm UTC](https://community.auth0.com/t/account-linking-through-actions-critical-issue/76484/10 "2023-12-29T13:41:37Z")

</div>

Our use case is a bit different, but we also rely on the ability to switch the user at login.

We won’t be able to port rules to actions without this critical feature.

---

<div class="post-metadata">

**Author:** ![spoudel](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/spoudel/32/51376_2.png) [@spoudel](https://community.auth0.com/u/spoudel)\
**Post date:** [December 30, 2023, 1:13am UTC](https://community.auth0.com/t/account-linking-through-actions-critical-issue/76484/11 "2023-12-30T01:13:12Z")

</div>

This might be helpful:

> <https://github.com/abbaspour/actions-gallery/blob/main/post-login/account-linking/interactive-account-linking.js>

---

<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:** [January 8, 2024, 11:51am UTC](https://community.auth0.com/t/account-linking-through-actions-critical-issue/76484/12 "2024-01-08T11:51:06Z")

</div>

Hey there!

As this topic is related to Actions and Rules & Hooks are being deprecated soon in favor of Actions, I’m excited to let you know about our next [Ask me Anything session in the Forum on Thursday, January 18 with the Rules, Hooks and Actions team](https://community.auth0.com/t/auth0-community-ask-me-anything-rules-hooks-and-why-actions-matter/124043) on Rules & Hooks and why Actions matter! Submit your questions in the thread above and our esteemed product experts will provide written answers on January 18. Find out more about Rules & Hooks and why Actions matter! Can’t wait to see you there!

[Learn more here!](https://community.auth0.com/t/auth0-community-ask-me-anything-rules-hooks-and-why-actions-matter/124043)

---

<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:** [January 22, 2024, 11:51am UTC](https://community.auth0.com/t/account-linking-through-actions-critical-issue/76484/13 "2024-01-22T11:51:13Z")

</div>

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