How to link accounts from `ACTION`s

Continuing the discussion from Account linking with actions, 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 ACTIONs appear to be a different architecture. It is convenient to link the new account to the existing one the following way:

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

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

Which are needed by link(userId, params, cbopt) → {Promise|undefined}
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

1 Like