Skip to content

Firebase login—Check if email in use

I am stuck on this issue. When I try and enter the same email and a different password for a signup page I am creating I am not getting the same email was used error but when I use the same email and password then it says email in use. How do I make it so it says email in use even if the password is different?

Thanks

Here is my code:

function signUp(email,password){
  //Check if email adress registerd then password security
  firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error){
    var errorCode = error.code;
    console.log(error.message, errorCode)
    if(errorCode == "auth/weak-password"){
      alert("Password too weak")
    }
    else if(errorCode == "auth/email-already-in-use"){
      alert("Email in use aldready")
    }
    else{
      //If succsess
      firebase.auth().sendSignInLinkToEmail(email, actionCodeSettings).then(() => {
        window.localStorage.setItem('emailForSignIn', email);
      }).catch((error) => {
        var errorCode = error.code;
        var errorMessage = error.message;
      });
    }
  })
}

Edit: I tried using the fetch method, but I still run into the same issue

function signUp(email,password){
  //Check if email adress registerd then password security
  firebase.auth().fetchSignInMethodsForEmail(email)
    .then((signInMethods) => {
      if (signInMethods.length) {
        alert("Aldready signed up with this email")
      } else {
        // User does not exist. Ask user to sign up.
      }
    })
    .catch((error) => {
      // Some error occurred.
    });
}

Answer

To detect if an email is already in use, don’t call createUserWithEmailAndPassword, but instead call fetchSignInMethodsForEmail .