Authentication Using Facebook at First and Then Google Causes an Error in Firebase for Android

Authentication using Facebook at first and then Google causes an error in Firebase for Android

Please check the thread: https://groups.google.com/forum/#!searchin/firebase-talk/liu/firebase-talk/ms_NVQem_Cw/8g7BFk1IAAAJ
It explains why this happens. This is due to some security issue with Google emails being verified whereas Facebook emails are not.

Firebase Auth with Facebook fails if the account already exists and is associated with a Google Auth

I had the same issue and I was able to overcome it by looking into the customData property of the returned error. Probably this different behavior has to do with the specific version of Firebase. Mine is firebase@9.6.9.

So, try to see if this works:

export async function loginWithFacebook() {
signInWithPopup(auth, new FacebookAuthProvider())
.then((result) => {
const user = result.user;
console.log(user);
})
.catch(function (error) {
if (error.code === 'auth/account-exists-with-different-credential') {
console.log(FacebookAuthProvider.credentialFromError(error));
console.log(error.customData.email);
}
});

}

Handle facebook login with same account used with Google using firebase

After a little of hard work, I've managed to make it work in react native and I'm gonna leave the answer here for peeps who are facing the same issue. Be ware that I used react-native-prompt-android to ask the user for confirming his password when trying to link with Facebook.

The user tries to sign with Facebook and gets this error:

auth/account-exists-with-different-credentials
This is how I handled it:

            .catch((error) => {

// Catching the error
if (
error.code === 'auth/account-exists-with-different-credential'
) {
const _responseInfoCallback = (error, result) => {
if (error) {
alert('Error fetching data: ' + error.toString());
} else {
setEmail(result.email);
}
};

// Getting the email address instead of error.email from Facebook
const profileRequest = new GraphRequest(
'/me?fields=email',
null,
_responseInfoCallback,
);
new GraphRequestManager().addRequest(profileRequest).start();

if (email) {
auth()
.fetchSignInMethodsForEmail(email)
.then(async (methods) => {
// Checking the method
if (methods[0] === 'password') {

// Prompting the user to confirm/input his password for linking
const AsyncAlert = () => {
return new Promise((resolve, reject) => {
prompt(
'Password Confirmation',
'The email address is already linked with password account. Enter your password to process',
[
{
text: 'Cancel',
style: 'cancel',
},
{
text: 'Continue',
onPress: (password) =>
resolve(setPassword(password)),
},
],
{
type: 'secure-text',
cancelable: false,
placeholder: 'Password',
},
);
});
};

// Here the linking goes
await AsyncAlert().then(async () => {
await auth()
.signInWithEmailAndPassword(email, password)
.then(() => {
return auth().currentUser.linkWithCredential(
facebookCredential,
);
})
.catch(() => alert('Something went wrong'));
});
} else if (methods[0] === 'google.com') {
const {idToken} = await GoogleSignin.signIn(email);
const googleCredential = auth.GoogleAuthProvider.credential(
idToken,
);
await auth()
.signInWithCredential(googleCredential)
.then(() => {
return auth().currentUser.linkWithCredential(
facebookCredential,
);
});
}
});
} else {
alert('Something went wrong');
}
}
});


Related Topics



Leave a reply



Submit