How do I call fetchSignInMethodsForEmail() from Firebase Auth

10.4k views Asked by At

I have a PSK (Polymer Starter Kit) PWA build with polymerfire, I want to call fetchSignInMethodsForEmail() from Firebase Auth. I have tried the following but with error.

firebase.auth().fetchSignInMethodsForEmail(email).then((methods) => {
  // Do something
});

with the error Uncaught TypeError: firebase.auth(...).fetchSignInMethodsForEmail is not a function.

I have tried the following as well with no luck, i.e.

firebase.auth.fetchSignInMethodsForEmail(email);
firebase.$.auth.fetchSignInMethodsForEmail(email); // Assume firebase-auth with id of 'auth'
3

There are 3 answers

0
Andrew See On

fetchProvidersForEmail() was introduced in Firebase JavaScript SDK v4.12 so you need to have at least v4.12 and above.

Being said that it is deprecated in v5.0 in favor of fetchSignInMethodsForEmail().

0
Mudassir Ahmed On

First, you have to declare Auth object.

 FirebaseAuth f = FirebaseAuth.getInstance();

Then call the fetchSignInMethodsForEmail(), method.

as follows:

 // [START auth_differentiate_link]

auth.fetchSignInMethodsForEmail(email)
.addOnCompleteListener(new OnCompleteListener<SignInMethodQueryResult>() {
    @Override
            public void onComplete(@NonNull Task<SignInMethodQueryResult> task) {
               if (task.isSuccessful()) {
                  SignInMethodQueryResult result = task.getResult();
                  List<String> signInMethods = result.getSignInMethods();
           if(signInMethods.contains(EmailAuthProvider.EMAIL_PASSWORD_SIGN_IN_METHOD)) 
                {
                   // User can sign in with email/password
                } else if (signInMethods.contains(EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD)) {
                  // User can sign in with email/link
                }
               } else {
                  Log.e(TAG, "Error getting sign in methods for user", task.getException());
               }
           }
        });
  // [END auth_differentiate_link]
}
0
Jonathan Laliberte On

For people using the new modular version it's done like this:

import {  fetchSignInMethodsForEmail } from 'firebase/auth';

    fetchSignInMethodsForEmail(auth, email).then((result) => {
      console.log(result);
    });