I have a login where they start using Facebook. I want to add the option to login using Twitter. But for that I must also be able to handle errors with accounts that have the same mail that has already been registered before. That is, if a user registers with Facebook with the mail [email protected] and then wants to enter with twitter with the email [email protected]. I must detect that and handle it. The documentation gives this code as an example:
// Step 1.
// User tries to sign in to Twitter.
auth.signInWithPopup(new firebase.auth.TwitterAuthProvider()).catch(function(error) {
// An error happened.
if (error.code === 'auth/account-exists-with-different-credential') {
// Step 2.
// User's email already exists.
// The pending Twitter credential.
var pendingCred = error.credential;
// The provider account's email address.
var email = error.email;
// Get registered providers for this email.
auth.fetchProvidersForEmail(email).then(function(providers) {
// Step 3.
// If the user has several providers,
// the first provider in the list will be the "recommended" provider to use.
if (providers[0] === 'password') {
// Asks the user his password.
// In real scenario, you should handle this asynchronously.
var password = promptUserForPassword(); // TODO: implement promptUserForPassword.
auth.signInWithEmailAndPassword(email, password).then(function(user) {
// Step 4a.
return user.link(pendingCred);
}).then(function() {
// Twitter account successfully linked to the existing Firebase user.
goToApp();
});
return;
}
// All the other cases are external providers.
// Construct provider object for that provider.
// TODO: implement getProviderForProviderId.
var provider = getProviderForProviderId(providers[0]);
// At this point, you should let the user know that he already has an account
// but with a different provider, and let him validate the fact he wants to
// sign in with this provider.
// Sign in to provider. Note: browsers usually block popup triggered asynchronously,
// so in real scenario you should ask the user to click on a "continue" button
// that will trigger the signInWithPopup.
auth.signInWithPopup(provider).then(function(result) {
// Remember that the user may have signed in with an account that has a different email
// address than the first one. This can happen as Firebase doesn't control the provider's
// sign in flow and the user is free to login using whichever account he owns.
// Step 4b.
// Link to Twitter credential.
// As we have access to the pending credential, we can directly call the link method.
result.user.link(pendingCred).then(function() {
// Twitter account successfully linked to the existing Firebase user.
goToApp();
});
});
});
}
});
My problem is in this part:
var provider = getProviderForProviderId(providers[0]);
auth.signInWithPopup(provider).then(function(result) {
The browser tells me:
getProviderForProviderId "is not defined
I do not know what to do with the 2 previous lines.