FirebaseExtended / make-it-so-android

Apache License 2.0
214 stars 58 forks source link

fix(auth): add proper use of link account method #17

Closed JuanAlejandro closed 1 year ago

JuanAlejandro commented 1 year ago

Hey @marinacoelho!

First, thanks for putting together your series of articles about getting started with Firebase and Compose. It was the perfect series I found to get started on a side project I'm working on.

I found a potential bug when implementing the firebase authentication. When creating an account I was getting an error stating the account was already linked to the given provider. This was happening because we were creating the account and then trying to link it to an existing (anonymous) account. But after creating the account we lost the anonymous account credentials and Firebase.auth.currentUser was pointing to the new authentication provider (email in this case).

The fix was to not create the account and instead "promote" it to the email provider account.

Something similar happens when the user signs in. Since the account is already created, Firebase cannot link an anonymous account with an existing one, and you cannot "promote" an already promoted account. The best we can do is to copy the data the anonymous account had, over to the logged-in account. I removed the linkWithEmail step because it will fail. Another thing that we can add to it is to delete the anonymous account after signing in to avoid cluttering the user database with anonymous accounts (altho we can activate the auto-cleanup to remove the anonymous account 30-days old.

If you think this is correct, it'll be great if the piece of Part 2 of your series clarifies this.

Upon returning from a successful login or account creation, we proceed to the next step, which is to get the credentials the user used to sign in to the second authentication provider, and link them to the anonymous account

I hope this helps and makes sense.

Thank you again 🙏

marinacoelho commented 1 year ago

Hi @JuanAlejandro Thank you so much for your contribution! We are working on a fix for it in another branch, but it will be released only a few weeks from now. Meanwhile, I'll merge your pull request so we can have a fix on the main branch as well. Thanks again!