Reputation: 742
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?
Upvotes: 0
Views: 1616
Reputation: 30818
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:
In all of the above, the error may contain the additional fields:
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):
Upvotes: 7