# Change primary user during post-login action results in Unauthorized

**URL:** <https://community.auth0.com/t/change-primary-user-during-post-login-action-results-in-unauthorized/83162>\
**Category:** Get Help\
**Tags:** account-linking, actions, rules, management-api, login\
**Created:** [May 10, 2022, 9:30am UTC](https://community.auth0.com/t/change-primary-user-during-post-login-action-results-in-unauthorized/83162 "2022-05-10T09:30:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cristiano.piemontese](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/cristiano.piemontese/32/37202_2.png) [@cristiano.piemontese](https://community.auth0.com/u/cristiano.piemontese)\
**Post date:** [May 10, 2022, 9:30am UTC](https://community.auth0.com/t/change-primary-user-during-post-login-action-results-in-unauthorized/83162/1 "2022-05-10T09:30:01Z")

</div>

Hello!

I was wondering if it was possible to “change” the primary user during a post-login action and still authorize the user correctly.

Let’s say I have a user with two identities

1. Username-Password-Authentication
2. google-workspace

Using an action, I can unlink the two accounts and then link the Database one to the Google Workspace one, but I get an `Unauthorized` response. If I then try logging in again everything works correctly.

Is this unsupported or is there some way to do it?

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [May 12, 2022, 4:46pm UTC](https://community.auth0.com/t/change-primary-user-during-post-login-action-results-in-unauthorized/83162/3 "2022-05-12T16:46:36Z")

</div>

Hi @cristiano.piemontese,

Welcome to the Auth0 Community!

> [@cristiano.piemontese](#):
>
> I was wondering if it was possible to “change” the primary user during a post-login action and still authorize the user correctly.

Unfortunately, you cannot change the primary user during the authentication flow. This would not work since the user that initiated the login would have submitted their credentials to log in.

Because of this, the secondary identity will need a way to authenticate in between the first authentication which introduces complexity and security issues.

> [@cristiano.piemontese](#):
>
> Using an action, I can unlink the two accounts and then link the Database one to the Google Workspace one, but I get an `Unauthorized` response. If I then try logging in again everything works correctly.

The `401 Unauthorized` error you observed is to be expected since the current user was not the same as the initial user that requested access.

Now, could you please clarify if the user’s identities were previously linked?

If so, could you please clarify the reason why you need to perform these steps instead of having the user use their desired account to log in? They should be able to log in with any identity given that it is linked.

I am looking forward to your reply.

Thanks.

---

<div class="post-metadata">

**Author:** ![cristiano.piemontese](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/cristiano.piemontese/32/37202_2.png) [@cristiano.piemontese](https://community.auth0.com/u/cristiano.piemontese)\
**Post date:** [May 13, 2022, 6:54am UTC](https://community.auth0.com/t/change-primary-user-during-post-login-action-results-in-unauthorized/83162/4 "2022-05-13T06:54:14Z")

</div>

Hi @rueben.tiow,

Thanks for your answer!

> [@rueben.tiow](#):
>
> Unfortunately, you cannot change the primary user during the authentication flow. This would not work since the user that initiated the login would have submitted their credentials to log in.
> 
> Because of this, the secondary identity will need a way to authenticate in between the first authentication which introduces complexity and security issues.

That makes sense and I figured this was the case.

> [@rueben.tiow](#):
>
> Now, could you please clarify if the user’s identities were previously linked?
> 
> If so, could you please clarify the reason why you need to perform these steps instead of having the user use their desired account to log in? They should be able to log in with any identity given that it is linked.

Yes, user’s identities are linked. I am trying to “migrate” users i.e. switch their main identity and secondary identity. I guess the best way to do this would be with a script instead of relying on a post-login action?

Thanks

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [May 13, 2022, 5:48pm UTC](https://community.auth0.com/t/change-primary-user-during-post-login-action-results-in-unauthorized/83162/5 "2022-05-13T17:48:10Z")

</div>

Hi @cristiano.piemontese,

Thank you for your response.

> [@cristiano.piemontese](#):
>
> Yes, user’s identities are linked. I am trying to “migrate” users i.e. switch their main identity and secondary identity. I guess the best way to do this would be with a script instead of relying on a post-login action?

Yes, I suggest exporting your users using the Management API [Create Export users job endpoint](https://auth0.com/docs/api/management/v2#!/Jobs/post_users_exports) and manipulating the exported JSON file to switch their main identities.

When you import the users back into your application, make sure to set the `upsert` flag to true to update the existing records.

See [Bulk User Exports](https://auth0.com/docs/manage-users/user-migration/bulk-user-exports) to learn more.

Here are additional resources you may find useful:

- [How to Generate a Bulk Users Export CSV File From the Management API?](https://community.auth0.com/t/how-to-generate-a-bulk-users-export-csv-file-from-the-management-api/81197)
- [How to Bulk Update User Profile Details in the Management API?](https://community.auth0.com/t/how-to-bulk-update-user-profile-details-in-the-management-api/81198)

Please let me know if you have any further questions.

Thank you.

---

<div class="post-metadata">

**Author:** ![cristiano.piemontese](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/cristiano.piemontese/32/37202_2.png) [@cristiano.piemontese](https://community.auth0.com/u/cristiano.piemontese)\
**Post date:** [May 23, 2022, 10:22am UTC](https://community.auth0.com/t/change-primary-user-during-post-login-action-results-in-unauthorized/83162/6 "2022-05-23T10:22:42Z")

</div>

Hi @rueben.tiow!

How are you doing?

I tried your proposed solution but I’m having trouble understanding how I could achieve the identity switch 😕

When unlinking an identity from an account, a new account is created whose primary identity is the unlinked one. Then when they are linked back, the two accounts are fused into one by adding the linked account as an identity of the primary.

As I understand by reading the documentation, the bulk import can only be used to upsert a subset of user fields, as defined [here](https://auth0.com/docs/manage-users/user-accounts/user-profiles/user-profile-structure#user-profile-attributes). Two of the fields that **cannot** be upserted are

- `user_id`
- `identities`

If I can’t shuffle identities around how can I perform this linking/unlinking through this interface?

Thank you for your support 🙏

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [May 25, 2022, 10:39pm UTC](https://community.auth0.com/t/change-primary-user-during-post-login-action-results-in-unauthorized/83162/7 "2022-05-25T22:39:26Z")

</div>

Hi @cristiano.piemontese,

Doing well, thank you.

Thank you for your response and for checking this for me.

> [@cristiano.piemontese](#):
>
> As I understand by reading the documentation, the bulk import can only be used to upsert a subset of user fields, as defined [here](https://auth0.com/docs/manage-users/user-accounts/user-profiles/user-profile-structure?_ga=2.66230382.1579634438.1653281938-570828730.1629748139&_gl=1*1valyr8*rollup_ga*NTcwODI4NzMwLjE2Mjk3NDgxMzk.*rollup_ga_F1G3E656YZ*MTY1MzA4MTA1Ny4yMDYuMS4xNjUzMDgxMzQ2LjMz#user-profile-attributes). Two of the fields that **cannot** be upserted are
> 
> - `user_id`
> - `identities`

In this case, you could consider importing just the user’s primary identity with `upsert: false`; Allowing you to set the `user_id` and negate the `identities` property altogether. This will preserve the user’s primary identity, and then from this point, I suggest using [Account Linking](https://auth0.com/docs/manage-users/user-accounts/user-account-linking#suggested-account-linking) for the second identity.

This way, the user can continue to log in to their primary account, as usual, and then offered to link their secondary identity with the primary identity.

This way, you essentially mimicked swapping identities.

Does that help?

Thank you.

---

<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:** [December 21, 2022, 6:13pm UTC](https://community.auth0.com/t/change-primary-user-during-post-login-action-results-in-unauthorized/83162/8 "2022-12-21T18:13:18Z")

</div>


