首页 > 解决方案 > Firebase 登录 - 检查电子邮件是否在使用中

问题描述

我被困在这个问题上。当我尝试为我正在创建的注册页面输入相同的电子邮件和不同的密码时,我没有收到相同的电子邮件被使用错误,但是当我使用相同的电子邮件和密码时,它会显示电子邮件正在使用中。即使密码不同,我该如何让它显示正在使用的电子邮件?

谢谢

这是我的代码:

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;
      });
    }
  })
}

编辑:我尝试使用 fetch 方法,但我仍然遇到同样的问题

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.
    });
}

标签: javascriptfirebasefirebase-authentication

解决方案


要检测电子邮件是否已在使用中,请不要调用createUserWithEmailAndPassword,而是调用fetchSignInMethodsForEmail


推荐阅读