Firebase link existing user to anonymous account?

33 Views Asked by At

I have Ionic 7, Angular, Standalone app. I am currently trying to implement linking anonymous account to registered/loggedin account.

First scenario is a user who doesn't have any registered account below. This works fine and I can see anonymous account merged with newly generated account.

const anonymousUser: User = this.auth.currentUser!
const credential = EmailAuthProvider.credential(email,password)
return linkWithCredential(anonymousUser, credential)

Problem is when this user logout and they try to log back in. When user navigates to the app after logout, we still generate anonymous credential. When we try to use above to link credential with existing user, I get following error:

FirebaseError: Firebase: Error (auth/email-already-in-use)

How can I handle the login situation where user is trying to log back in and we want to link this to existing anonymous account? Is this even possible?

1

There are 1 best solutions below

0
Doug Stevenson On BEST ANSWER

It's not a supported use case to link any number of anonymous accounts to full existing account. Once the user has chosen to convert their anonymous account to a normal account, they should only then sign in directly using that account in order to continue using the app with their prior data.

If you are creating anonymous accounts at each unauthenticated visit, then you will have to accept that those new accounts will be abandoned if and when the user chooses to subsequently sign in with their full account using the (email) provider that was previously linked.

Maybe consider automatic clean-up if you don't want to deal with all these anonymous account accumulating.