Gabe O'Leary
Gabe O'Leary

Reputation: 2070

Firebase Auth Linking Merging of accounts issues

I have my app set up so that users can use it without logging in. This includes being able to save data. Each device/browser tuple generates a new anonymous user account for storing data.

I'm in the process of adding (facebook) login so that users can make sure their data persists across browsers and devices.

The firebase documentation shows how to handle a case where a user tries to link their account from multiple anonymous accounts and recommends an approach for merging data:

firebase docs recommended approach https://firebase.google.com/docs/auth/web/account-linking

My problem with this approach is that it deletes the original account that was linked which essentially boots the first account out of it's logged in state.

Imagine this flow for a single user:

  1. Logs into device A & saves data
  2. Logs into device B & saves more data.
  3. On device A begins "login" flow using facebook for and ends up linking facebook credentials.
  4. On device B they login/link facebook again (linking fails so we must use an alternate method for recourse).
  5. The process recommended deletes the original account generated on device A & they must log in using facebook again on this device.

I've implemented a different process which doesn't result in device A being booted, and instead logs device B into the account generated on device A. My problem with this flow is that I can't delete the user data from device B once I've successfully logged in to device A because I'm using access control rules on firebase so that user data may only be modified by the logged-in user who it belongs to.

enter image description here

potential (suboptimal) solutions:

I'm confused as to why the example in firebase docs seems to have this glaring issue and wondering what the best practice for this situation is.

Upvotes: 3

Views: 192

Answers (1)

Gabe O'Leary
Gabe O'Leary

Reputation: 2070

I got no responses, but I implemented what I find to be an "elegant enough" solution to the problem.

Instead of deleting the prevUser's account & data after succesful login to the other account, I just delete the account: enter image description here

I implemented a firebase function to handle the deletion of the orphaned user data: enter image description here

Upvotes: 2

Related Questions