# Account linking with Auth0 actions

**URL:** <https://community.auth0.com/t/account-linking-with-auth0-actions/60676>\
**Category:** Get Help\
**Tags:** actions\
**Created:** [April 7, 2021, 8:24am UTC](https://community.auth0.com/t/account-linking-with-auth0-actions/60676 "2021-04-07T08:24:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexab](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alexab/32/13403_2.png) [@alexab](https://community.auth0.com/u/alexab)\
**Post date:** [April 7, 2021, 8:24am UTC](https://community.auth0.com/t/account-linking-with-auth0-actions/60676/1 "2021-04-07T08:24:15Z")

</div>

I have a user with verified e-mail [abc@domain123.com](mailto:abc@domain123.com) in my application, logged in with passwordless. We are now enabling SSO for domain123, and I would like a seamless experience when [abc@domain123.com](mailto:abc@domain123.com) signs in with SSO the first time (in other words, I don’t want to present the user with _any_ account-linking GUI. It should “just” work). I’m aware that there are some potential security vulnerabilities when doing seamless account linking, but in this specific scenario it’s ok.

I have been in contact with Auth0 support where we discussed doing this in a rule, and I’m about to start implementing that. But then I log-in to auth0 and discover we now have something called actions 📣.

Rules, hooks and actions 😅

So in other words, is account-linking something I should accomplish using an action instead? And in case the answer is yes, what flow should I pick? Would love some general feedback on this one 😌

**update**  
I now have a working action that does the account linking. Works pretty good, so I will recommended using an action. Because of some logging limitations in auth0 custom actions, I have decided to move the actual code to a server-function we host, and then let the action call this endpoint.

If requested, I can add specific code snippets, but the most important part is to use the mgmt API to link the users, and then return the “new” primary user like this:

```
event.user = primaryUserAfterAccountLink;
return {
  user: primaryUserAfterAccountLink
}

```

cheers 🍻

---

<div class="post-metadata">

**Author:** ![stephanie.chamblee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stephanie.chamblee/32/26087_2.png) [@stephanie.chamblee](https://community.auth0.com/u/stephanie.chamblee)\
**Post date:** [April 7, 2021, 7:07pm UTC](https://community.auth0.com/t/account-linking-with-auth0-actions/60676/2 "2021-04-07T19:07:38Z")

</div>

Thank you for sharing your solution with the Community @alexab!

Quoting your solution here for others to see! 🙂 :

> [@alexab](#):
>
> I now have a working action that does the account linking. Works pretty good, so I will recommended using an action. Because of some logging limitations in auth0 custom actions, I have decided to move the actual code to a server-function we host, and then let the action call this endpoint.
> 
> If requested, I can add specific code snippets, but the most important part is to use the mgmt API to link the users, and then return the “new” primary user like this:
> 
> ```auto
> event.user = primaryUserAfterAccountLink;
> return {
> user: primaryUserAfterAccountLink
> }
> 
> ```

---

<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:** [April 22, 2021, 7:07pm UTC](https://community.auth0.com/t/account-linking-with-auth0-actions/60676/3 "2021-04-22T19:07:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![peter.fernandez](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/peter.fernandez/32/45477_2.png) [@peter.fernandez](https://community.auth0.com/u/peter.fernandez)\
**Post date:** [March 16, 2023, 2:01pm UTC](https://community.auth0.com/t/account-linking-with-auth0-actions/60676/4 "2023-03-16T14:01:12Z")

</div>

For anyone who finds this thread, and in the interest of clarity, changing the user context as described here apparently relies on now-deprecated functionality from the `v1` programming model for the `PostLogin` trigger. As it stands today, only the `context.primaryUser` method works to change the primary user (see [Context Object Properties in Rules](https://auth0.com/docs/customize/rules/context-object) for more details), and this only works within a Rule context.
