Firebase Multiple Auth Providers Issue

Viewed 1050

I've been banging my head and keyboard Googling, reading the Firebase docs and combing StackOverflow for days now for similar situations and help.

Here's the situation:

  1. User logs into iOS app using Twitter button.
  2. User signs out (for whatever reason).
  3. Some time later, the User returns to the app and taps the Facebook button, and Firebase returns the following error:

"Auth error: An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address."

The Firebase docs say to link the user to, in this case, to the Facebook credential. https://firebase.google.com/docs/auth/ios/account-linking

user.link(with: credential) { (user, error) in

But at this point, Auth.auth().currentUser == nil so there is no account to link to. Furthermore, the "One account per email address" setting is set to TRUE.

From Auth: signInAndRetrieveData(with credential: AuthCredential, completion: FirebaseAuth.AuthDataResultCallback? = nil)

FIRAuthErrorCodeAccountExistsWithDifferentCredential - Indicates the email asserted by the credential (e.g. the email in a Facebook access token) is already in use by an existing account, that cannot be authenticated with this sign-in method. Call fetchProvidersForEmail for this user’s email and then prompt them to sign in with any of the sign-in providers returned. This error will only be thrown if the "One account per email address" setting is enabled in the Firebase console, under Auth settings.

So back to this situation (user currently has Twitter credential, user has tapped Facebook button, currentUser == nil and the above error exists), at this point, would it be appropriate to call the Twitter sign-in function programmatically (on behalf of the user) and then link the Facebook credential to this current user?

Has someone come across this situation and solved it for a cool user experience instead of kicking out an alert to tap twitter button?

Thank you for the help

0 Answers
Related