Handle error with existing firebase account

0

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.

    
asked by Fernando Azarías 21.07.2018 в 15:00
source

1 answer

1

The pseudocode in English is saying that you have to implement the getProviderForProviderId function. Then you have to implement it. Look in the documentation, each provider has a PROVIDER_ID which is the one that you have to compare. Something like:

function getProviderForId(id) {
  switch(id) {
    case firebase.auth.TwitterAuthProvider.PROVIDER_ID:
      return new firebase.auth.TwitterAuthProvider();
    case firebase.auth.FacebookAuthProvider.PROVIDER_ID:
      return new firebase.auth.FacebookAuthProvider();
  //case ... Todos los casos por cada Provider que uses en tu proyecto
  }
  
}

However, if you read the documentation , you will notice that the fetchProvidersForEmail method is obsolete. Now you must use fetchSignInMethodsForEmail .

If you change the code to use the new method then you will have to compare, not the PROVIDER_ID , but the ...SIGN_IN_METHOD of each provider. In the case of email authentication, there are two methods .

    
answered by 23.07.2018 / 01:58
source