I’m trying to link account with post login trigger automatically where I check the existing user with email and link users which have an existing identity. The linking works as expected but when I call api.authentication.setPrimaryUser
I get an error saying Organizations is not supported together with primary user modifications
, I guess this is because we have enabled auto-registration in one of our organisations.
Is there a way to resolve this? since it does work on second login but when the action performs linking the users in the first login transaction, it fails the login with the said error. I could not find this error documented anywhere so any help is appreciated.
Reference flow:
- Existing user =
abc@abc.com
via database connection belongs to ABC organisation - New user =
abc@abc.com
via google connection now belongs to both ABC + Org1 (auto-membership organisation) - Action linking works but returns an error of
Organizations is not supported together with primary user modifications
when login flow is completed hence, login fails at the end. - It does work as intended on second login
Reference code for post login action:
const management = new ManagementClient({
domain: "**********",
clientId: "**********",
clientSecret: "*************,
audience: "<domain>/api/v2/"
});
try {
const { data: users } = await management.usersByEmail.getByEmail({email: event.user.email})
if (users.length == 2) {
console.log("Multiple identities detected, attempting account linking.")
console.log("User id of primary =" + users[0].user_id);
console.log("User id of secondary =" + event.user.user_id);
await management.users.link({id: users[0].user_id}, {provider: event.connection.name, user_id: event.user.user_id});
api.authentication.setPrimaryUser(users[0].user_id);
} else if (users.length > 2) {
console.log("More than one identities detected, instead of crashing skip logic")
}
} catch (e) {
console.log(e);
}