# How to link accounts from \`ACTION\`

**URL:** <https://community.auth0.com/t/how-to-link-accounts-from-action/68535>\
**Category:** Get Help\
**Created:** [August 25, 2021, 1:41pm UTC](https://community.auth0.com/t/how-to-link-accounts-from-action/68535 "2021-08-25T13:41:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![steven.varga](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/steven.varga/32/31288_2.png) [@steven.varga](https://community.auth0.com/u/steven.varga)\
**Post date:** [August 25, 2021, 1:41pm UTC](https://community.auth0.com/t/how-to-link-accounts-from-action/68535/1 "2021-08-25T13:41:37Z")

</div>

Continuing the discussion from [Account linking with actions](https://community.auth0.com/t/account-linking-with-actions/68508), I am directly asking the question: How to link two accounts seamlessly from ACTIONs?  
While the existing trail on linking accounts lead to RULEs, where I am aware of how to return the modified `user` record `ACTION`s appear to be a different architecture. It is convenient to link the new account to the existing one the following way:

```auto
INCORRECT CODE DO NOT COPY PASTE!!!

var AWS = require("aws-sdk");
var auth0 = require("auth0");

exports.onExecutePostLogin = async (event, api) => {
 ... additional code needed to skip the part if account linked ...
  
 const user = event.user;
 // obtain M2M credetials, with correct permissions!!!
  const management = new auth0.ManagementClient({
      domain: configuration.AUTH0_DOMAIN,
      clientId: configuration.AUTH0_CLIENT_ID,
      clientSecret: configuration.AUTH0_SECRET,
  });
  const users = await management.users.getByEmail(user.email);
  if(users.length > 1){ // current user is in the set...
      users.forEach(function(usr){
        if(usr.user_id != user.user_id){ 
          management.users.link(usr.user_id, 
            {user_id: user.user_id, 
            connection_id: event.connection.id, 
            provider: event.connection.name }, function(err, user){
                     // this never gets called, not sure why....
             });
        } 
      });

   .. do something else ....
   return 
  }

```

As the `current_login` has

```auto
event {
connection.id: ...,
connection.name: ...,
}
user {
user_id: ...
}

```

Which are needed by [`link(userId, params, cbopt) → {Promise|undefined}`](https://auth0.github.io/node-auth0/module-management.UsersManager.html#link)  
However `current_login` gets removed by the process when we link the `current_user` to `existing_user`, it is logical if it doesn’t work one way, flip it around. But that appears laborious, as no handy information available for the `connection_id` and the `provider` (same as `connection.name`).

What is the best way to link two accounts seamlessly in an `ACTION`?  
best wishes: steven

---

<div class="post-metadata">

**Author:** ![lihua.zhang](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lihua.zhang/32/39683_2.png) [@lihua.zhang](https://community.auth0.com/u/lihua.zhang)\
**Post date:** [December 29, 2022, 9:28pm UTC](https://community.auth0.com/t/how-to-link-accounts-from-action/68535/2 "2022-12-29T21:28:06Z")

</div>

Hi @steven.varga ,

Sorry about the delayed reply!

Currently, we can not implement Account linking using actions. However, we support Account Linking in two ways:

- User-initiated account linking: allow users to link their accounts using an admin screen in your app.
- Suggested account linking: identify accounts with the same email address and prompt the user in your app to link them.

Please reference [this](https://community.auth0.com/t/implementing-account-linking-using-post-login-actions/90162) FAQ. Hope this helps!

---

<div class="post-metadata">

**Author:** ![lihua.zhang](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lihua.zhang/32/39683_2.png) [@lihua.zhang](https://community.auth0.com/u/lihua.zhang)\
**Post date:** [December 29, 2022, 9:28pm UTC](https://community.auth0.com/t/how-to-link-accounts-from-action/68535/3 "2022-12-29T21:28:14Z")

</div>


