title: Firebase auth overriding user providers (email + password, phone) when signing-in with Google
body: <p>As we have seen in another posts related to this situation and github issues, the expected behavior is that Google IDP overrides other non trusted providers related to the same email as an example, another account with the same email + password (non-verified).</p>
  <p><a href="https://stackoverflow.com/questions/60258708/trying-to-understand-firebase-authentication-one-account-per-email-address-and-t">Trying to understand Firebase Authentication one account per email address and trusted providers</a></p>
  <p><a href="https://stackoverflow.com/questions/40766312/firebase-overwrites-signin-with-google-account">Firebase Overwrites Signin with Google Account</a></p>
  <p><a href="https://github.com/firebase/firebase-ios-sdk/issues/5344" rel="nofollow noreferrer">https://github.com/firebase/firebase-ios-sdk/issues/5344</a></p>
  <p><a href="https://groups.google.com/g/firebase-talk/c/ms_NVQem_Cw/m/8g7BFk1IAAAJ" rel="nofollow noreferrer">https://groups.google.com/g/firebase-talk/c/ms_NVQem_Cw/m/8g7BFk1IAAAJ</a></p>
  <p>So, ok, according to google that's the expected behavior.</p>
  <p>Our questions comes when we go to the documentation and there's an example of a user login in with google and getting this error <code>auth/account-exists-with-different-credential</code> just because there's another account created with email+password with the same email. Then, they recommend to catch the error, check the user email related login methods and ask the user to login with the other provider and then link to google.</p>
  <p>Does this make sense ? If they say the expected behavior is that google as a trusted provider will override the others (this is what happens to us) how is possible that the case of the code example would even occur ?</p>
  <p><a href="https://firebase.google.com/docs/auth/web/google-signin#expandable-1" rel="nofollow noreferrer">https://firebase.google.com/docs/auth/web/google-signin#expandable-1</a></p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>// Step 1.
  // User tries to sign in to Google.
  auth.signInWithPopup(new firebase.auth.GoogleAuthProvider()).catch(function(error) {
    // An error happened.
    if (error.code === 'auth/account-exists-with-different-credential') {
      // Step 2.
      // User's email already exists.
      // The pending Google credential.
      var pendingCred = error.credential;
      // The provider account's email address.
      var email = error.email;
      // Get sign-in methods for this email.
      auth.fetchSignInMethodsForEmail(email).then(function(methods) {
        // Step 3.
        // If the user has several sign-in methods,
        // the first method in the list will be the "recommended" method to use.
        if (methods[0] === 'password') {
          // Asks the user their password.
          // In real scenario, you should handle this asynchronously.
          var password = promptUserForPassword(); // TODO: implement promptUserForPassword.
          auth.signInWithEmailAndPassword(email, password).then(function(result) {
            // Step 4a.
            return result.user.linkWithCredential(pendingCred);
          }).then(function() {
            // Google 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(methods[0]);
        // At this point, you should let the user know that they already have an account
        // but with a different provider, and let them validate the fact they want 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 they own.
          // Step 4b.
          // Link to Google credential.
          // As we have access to the pending credential, we can directly call the link method.
          result.user.linkAndRetrieveDataWithCredential(pendingCred).then(function(usercred) {
            // Google account successfully linked to the existing Firebase user.
            goToApp();
          });
        });
      });
    }
  });</code></pre>
  </div>
  </div>
  </p>
  <p>There's another example with the same structure in the flutter docs:</p>
  <p><a href="https://firebase.google.com/docs/auth/flutter/errors#handling_account-exists-with-different-credential_errors" rel="nofollow noreferrer">https://firebase.google.com/docs/auth/flutter/errors#handling_account-exists-with-different-credential_errors</a></p>
  <p>Is this a contradiction in the documentation ? Again, if Firebase will always give priority to the trusted IDP (Google email) in this case, how is it possible to get this error if the other provider will be deleted (at least when having account linking activated - single account per email activated)</p>
  <p>At least this is our case. We create an account with email &amp; password and then try to login with google with the same email and what happens is that the email&amp;password account is overwritten by the new google provider.</p>