Here is my issue.
- I want to allow my users to sign in in my app using different providers (Apple & Google)
- If the email is the same, they should link to the SAME user
My issue is that I have enabled the setting on Firebase Console.
Authentication > Settings > User Account Linking > Link accounts that use the same email
I created my user initially with Google. Now, I logged out and I'm trying to log back in using Apple but it fails saying:
"code": "auth/email-already-in-use", "credentials": null, "userInfo": {"authCredential": null, "code": "email-already-in-use", "message": "The email address is already in use by another account."}
This is the code I'm calling after Apple successfully creates the credential.
const createUserWithCredentials = async (
credential: FirebaseAuthTypes.AuthCredential,
) => {
try {
const currentUser = auth().currentUser;
let res: FirebaseAuthTypes.UserCredential;
if (currentUser) {
console.log('Attempt to link with anonymous user..');
res = await currentUser.linkWithCredential(credential);
} else {
console.log('Attempt to sign up a brand new account..');
res = await auth().signInWithCredential(credential);
}
return res;
} catch (error) {
const freshCredentials = error.userInfo.authCredential;
console.log({ code: error.code });
if (
error.code === 'auth/provider-already-linked' ||
error.code == 'auth/credential-already-in-use' ||
error.code == 'auth/email-already-in-use'
) {
console.log('User already exists so linking the accounts');
await auth().signInWithCredential(freshCredentials);
} else {
throw error;
}
}
};
My console output shows this:
LOG Attempt to link with anonymous user..
LOG {"code": "auth/email-already-in-use", ... }
LOG User already exists so linking the accounts
INFO TypeError: Cannot read property 'providerId' of undefined
This tells me that my call to linkWithCredential()
fails. Then as a fallback we call signWithCredential
which should work. The Firebase docs says that the error FIRAuthErrorCodeEmailAlreadyInUse
is only thrown when "one account per email address" is enabled. I'm not sure what this means.