We’re having real problems trying to resolve this and so hoping for some Firebase assistance / those that have solved the same problem.
The app is React Native (0.43.2) and using Firebase JS API (latest)
We provide Facebook and Google auth. Works fine.
BUT, if a user:
- signs in with Facebook (is ok)
- later, signs in with Google (is also fine)
- later, tries to sign in with Facebook – BOOM! not so fine and Firebase returns this error:
auth/account-exists-with-different-credential
From reading docs and a few posts on SO, we thought the following was correct but clearly not as we’re getting the same auth error back.
...error returned by Firebase auth after trying Facebook login...
const email = error.email;
const pendingCred = error.credential;
firebase.auth().fetchProvidersForEmail(email)
.then(providers => {
//providers returns this array -> ["google.com"]
firebase.auth().signInWithCredential(pendingCred)
.then(result => {
result.user.link(pendingCred)
})
.catch(error => log(error))
The call to signInWithCredential is throwing the same error auth/account-exists-with-different-credential
.
Can anyone help point out what we are doing wrong with this implementation? Greatly appreciated.
8
Answers
Since google is the trusted provider for @gmail.com addresses it gets higher priority than other accounts using a gmail as their email. This is why if you sign in with Facebook then Gmail an error isn’t thrown, but if you try going Gmail to Facebook then it does throw one.
See this question.
If you want to allow multiple accounts with the same email then go to the Firebase console and under Authentication -> Sign-in methods, there should be an option at the bottom to toggle this.
What is happening is that Firebase enforces a same account for all emails. As you already have a Google account for the same email, you need to link that Facebook account to the Google account so the user can access the same data and next time be able to sign in to the same account with either Google or Facebook.
The issue in your snippet is that you are signing and linking with the same credential. Modify as follows.
When you get the error ‘auth/account-exists-with-different-credential’,
the error will contain error.email and error.credential (Facebook OAuth credential). You need to first lookup the error.email to get the existing provider.
I find it strange and inconvenient that Firebase have chosen this behaviour as default, and the solution is non-trivial. Here’s a full and updated solution for Firebase as of the time of writing based on @bojeil’s answer.
I emailed the Firebase support and they explained more to me. In their own words:
I’ve written about how to do this without needing to sign in for a second time here:
https://blog.wedport.co.uk/2020/05/29/react-native-firebase-auth-with-linking/
You need to store the original credential and retrieve to login silently before linking the accounts. Full code in link:
}
Sometimes the firebase documentation is great and other times it leaves you wanting more. In this case, when it comes to handling the error, it gives very detailed instructions on
signInWithPopup
. However the instructions forsignInWithRedirect
in their entirety are…Based on the answers from @bojeil and @Dominic, here is how you can link a facebook account with a google account calling
signInWithRedirect
.It’s 2022.
Well, in my case. I can just go to Firebase Console -> Authentication -> User Account Link (look at the screenshot attached)
then simply choose the Create Multiple accounts on each identity provider.
choosing Link accounts that use the same email will occur this error.
@Bojeil’s answer is the right one, however I wanted to add some color to it.
Why does Firebase Work This Way??
First, why the heck doesn’t Firebase just deal with this and not involve me? IOW, why are we getting the
auth/account-exists-with-different-credential
error in the first place?Google believes (rightly or wrongly) that Facebook (and other) logins may be using non-verified email addresses (see this closed issue among others). The security issue with this flow for non-verified emails is as follows:
Therefore, at step 4, Firebase refuses to do this, and instead returns the
auth/account-exists-with-different-credential
failure to your application. Your application now requires Alice to login with her Google account (which Eve cannot do) in order to link the Facebook account to the same identity as the Google account.What about Authenticating with Facebook First?
The reason this does not happen if the user logs in with Facebook first is that the since Google is the trusted authority for @gmail.com addresses, we know that Alice is simply logging in to her own account.
In this last situation, Firebase automatically links the Google account and removes the previous Facebook login from the account identity. This is to avoid another attack vector: if Eve can create an application account via her Facebook account and Alice’s email address before Alice logs in, Eve has gained access to Alice’s account in advance. In other words, Alice may happily create an account / log in as [email protected], not realizing that Eve’s Facebook account was already attached to that login, and that Eve can therefore log in to the account. Since Firebase simply removes the non-canonical Facebook login, that attack vector is eliminated.
How
Again, @bojeil’s answer is correct assuming you want to keep the Firebase "Link accounts that use the same email" setting. However, note that in browser environments obtaining the canonical credentials via popup will generally not work, because the second popup will not be a direct result of a user action, and the browser will block it.
One solution to this is to perform the canonical login via redirect. Another solution is to surface the credentials issue to the user, and have them click another button explicitly in order to login with the canonical credentials for the account. Since the second login is now a direct result of a user action, the popup will not be blocked.