Firebase不会检查电子邮件是否为帐户

时间:2016-11-15 15:53:35

标签: javascript firebase firebase-authentication

所以我正在使用Firebase网络电子邮件身份验证,我遇到了这个问题。 Firebase仅检查密码是否与电子邮件中存在的数据库中的密码匹配。如果电子邮件不存在,则它不会给出错误消息并登录此人。我想知道是否有办法解决这个问题,以便Firebase每次检查电子邮件时都会检查,如果没有,则会返回错误消息。

的JavaScript

    function SignUserIn() {
    // Get elements
    const txtEmail = document.getElementById("txtEmail");
    const txtPassword = document.getElementById("txtPassword");

    // Get email and pass
    const email = txtEmail.value;
    const password = txtPassword.value;
    const auth = firebase.auth();

    //Sign In
    firebase.auth().signInWithEmailAndPassword(email, password)
        .catch(function(error) {
            // Handle Errors here.
            var errorCode = error.code;
            var errorMessage = error.message;
            if (errorCode === 'auth/wrong-password') {
                window.alert('Wrong password.');
            }
            else {
                //Realtime listener
                firebase.auth().onAuthStateChanged(frebaseUser => {
                    if (email) {
                        window.alert("You Are Successfully Signed In! Welcome " + email);
                        window.location = 'homepage.html'; //After successful login, user will be redirected to Homepage
                        sessionStorage.setItem("email", email);
                    }
                    else {
                        console.log('Not logged in');
                        window.alert("Incorrect User/Password Please Try Again");
                    }
                });

                sessionStorage.setItem(email);
            }

        }); 
}

HTML BODY

<div class="login-card">
                <div class="mlogo"><img src="LogoFINAL copy.png" alt="Medavance"> </div>

                <h1 class="title">Welcome To Medavance! </h1>
                <br>
     <!------------------------------------- Start of Login  -------------------------------------------->
                <input type="email" id="txtEmail" placeholder="Email" required>
                <input type="password" id="txtPassword" placeholder="Password" required>
                <button id="btnLogin" class="btn btn-action" onclick="SignUserIn()"> Log in </button>
                <div class="btn btn-secondary"><a href="NewAccount.html"> Sign Up</div>
                  </div>

2 个答案:

答案 0 :(得分:0)

查看signInWithEmailAndPassword的文档,它表示如果没有与给定电子邮件相对应的用户,则应该old_repo“抛出”,但是您没有在测试中检查auth/user-not-found {{3}} 1}}然后你执行if (errorCode === 'auth/wrong-password'),这似乎取得了成功。

如果我理解正确的话,signInWithEmailAndPassword只应在出现错误时调用else回调,因此如果被调用则不应该记录它们。

您可以检查特定的errorCode以确定接下来要做什么,但只在跟随 signInWithEmailAndPassword调用时记录它们,而不是在catch函数中。这也意味着你的捕获需要阻止继续执行它。

.catch()还会返回承诺返回非空signInWithEmailAndPassword,其中包含非空firebase.Promise”,您可以使用跟随调用以将用户登录。

免责声明:这些都来自文档,我没有使用firebase.auth()来测试它。

答案 1 :(得分:0)

firebase.auth().signInWithEmailAndPassword(email, password)
.then(function() {
    // Handle success here
    firebase.auth().onAuthStateChanged(frebaseUser => {
        sessionStorage.setItem("email", email);
        window.alert("You Are Successfully Signed In! Welcome " + email);
        window.location = "homepage.html";
    });
}, function(error) {
    // Handle Errors here.
    var errorMessage = error.message;
    window.alert(errorMessage);
    document.getElementById("txtPassword").value = "";
});

}