How do i check if angularfire user exist with the authentication identifier

Viewed 1053

I've looked across for many solution but most of it is related to either checking if the auth user is logged in or not, or by using the document reference to read through and check for user email.

What I want to achieve is by running the user input and check within the authentication identifier email addressed whether the email already exist. From what I am aware of, the auth actually send out error like auth/email-already-in-use this is what i have right now but with every email that I try inputting, it will always return me with "user already in use"

 checkAccountEmailExistInFirebase(email) {
    firebase.auth().fetchSignInMethodsForEmail(email).then(user => { 
     console.log("User already in use");
    }).catch(err => { 
      if (err.code === 'auth/user-not-found') {
        this.regEmail = email;
      }
    })
1 Answers

The fetchSignInMethodsForEmail(email) resolves (calls then()) with a list of results. It only rejects (calls catch) if there is an error, such as when you specify an invalid email address.

So you'll have to check if the array of sign-in methods that is passed into then() is empty:

firebase.auth().fetchSignInMethodsForEmail(email)
  .then(function(signInMethods) {
    if (signInMethods.length > 0) {
      ...
    }
  })

See also the example in the Firebase documentation.

Related