简体   繁体   中英

Firebase account linking error

So when you try to login to an app using some other auth method, for example at first the user used Google and now he using FB and those 2 accounts have the same mail, you get that error

auth/email-already-exists

The thing is, that if you have 3+ auth methods, that error message isnt very specific and it could be a little tricky to develop a logic that handles this situation. How do you solve this issue?

email-already-exists is thrown in the firebase-admin sdk. I don't think it is thrown in the client SDK. For the client SDK, the following errors would be thrown when linking an existing account to another one, or signing in with a new account where the email exists in another one:

  • auth/account-exists-with-different-credential
  • auth/credential-already-in-use
  • auth/email-already-in-use

In all of the above, the error may contain the additional fields:

  • error.email: The email for the associated credential you tried to sign in with
  • error.credential: The credential itself (typically this is returned for OAuth credentials)

You can lookup the existing account to find out what providers exist for it:

firebase.auth().fetchProvidersForEmail(error.email)
  .then(function(providers) {
    // Providers would be an array of the form:
    // ['password', 'google.com']
  });

For auth/account-exists-with-different-credential, you can then sign in with that provider and if needed link the error.credential to the signed in user.

firebase.auth().currentUser.link(error.credential);

If the error occurred when linking (auth/credential-already-in-use), you can directly sign in with that credential

firebase.auth().signInWithCredential(error.credential):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM