3

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'
Andrew See
  • 1,062
  • 10
  • 21
  • I don't immediately see what's wrong here. What version of the Firebase Authentication SDK are you using? – Frank van Puffelen May 15 '18 at 13:36
  • @FrankvanPuffelen I am using polymerfire v2.2.1 with firebase v4.4.0. Do note that when using the function, the user was not signed in as I am trying to link user account when user encounter `auth/account-exists-with-different-credential` error. – Andrew See May 15 '18 at 17:40
  • I think I found the problem, need to use firebase v4.12.0 and above for the `fetchSignInMethodsForEmail()` to work. Thanks for the hint. But I also saw that the method was depreciated in firebase v5.0.0, how should I go about it? – Andrew See May 15 '18 at 19:14
  • Where did you see that it is deprecated? I don't see that in the docs: https://firebase.google.com/docs/reference/js/firebase.auth.Auth#fetchSignInMethodsForEmail – Frank van Puffelen May 15 '18 at 19:28
  • https://firebase.google.com/support/release-notes/js – Andrew See May 16 '18 at 09:47
  • "**Deprecated** `signInWithCredential`, `linkWithCredential`, `reauthenticateWithCredential` and **`fetchProvidersForEmail`** **in favor of** `signInAndRetrieveDataWithCredential`, `linkAndRetrieveDataWithCredential`, `reauthenticateAndRetrieveDataWithCredential` and **`fetchSignInMethodsForEmail`**". Emphasis mine. – Frank van Puffelen May 16 '18 at 13:53
  • Got it, thanks for the pointer, there's a lot of words and I actually missed that. – Andrew See May 16 '18 at 14:42

3 Answers3

3

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);
    });
Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44
2

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().

Andrew See
  • 1,062
  • 10
  • 21
2

First, you have to declare Auth object m

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]
}