Javascript Firebase身份验证需要两个';登录';电话

Javascript Firebase身份验证需要两个';登录';电话,javascript,html,firebase,firebase-realtime-database,firebase-authentication,Javascript,Html,Firebase,Firebase Realtime Database,Firebase Authentication,我正在尝试为我的web应用程序硬编码一个身份验证层,这样我就可以看到一个用户是否是“管理员”。如果我没有将“if语句”放在登录函数之前,登录就可以工作。但是,当我添加它时,它需要我按两次登录按钮才能工作 登录功能: function login() { var userEmail = document.getElementById("email_field").value; var userPass = document.getElementById("password_field").

我正在尝试为我的web应用程序硬编码一个身份验证层,这样我就可以看到一个用户是否是“管理员”。如果我没有将“if语句”放在登录函数之前,登录就可以工作。但是,当我添加它时,它需要我按两次登录按钮才能工作

登录功能:

function login() {
  var userEmail = document.getElementById("email_field").value;
  var userPass = document.getElementById("password_field").value;
  var login = [];
  var usersRef = firebase.database().ref("Parking Lots");
  usersRef.orderByChild("admin").on("value", function (snapshot) {
    snapshot.forEach(function (childSnapshot) {
      var childData = childSnapshot.val();
      login.push(childData.admin);
    });
  });
  firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION)
    .then(function () {
      if(login.includes(userEmail)){
       return firebase.auth().signInWithEmailAndPassword(userEmail, userPass);
      }
    })
    .catch(function (error) {
      window.alert("Fail");
      var errorCode = error.code;
      var errorMessage = error.message;
    });
}
function login() {
  var userEmail = document.getElementById("email_field").value;
  var userPass = document.getElementById("password_field").value;
  var login = [];

  var usersRef = firebase.database().ref("Parking Lots");
  return usersRef.orderByChild("admin").once("value").then(function (snapshot) {
    snapshot.forEach(function (childSnapshot) {
      var childData = childSnapshot.val();
      login.push(childData.admin);
    });
    if(login.includes(userEmail)){
      firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION)
      .then(function () {
        return firebase.auth().signInWithEmailAndPassword(userEmail, userPass);
      })
      .catch(function (error) {
        window.alert("Password incorrect");
        var errorCode = error.code;
        var errorMessage = error.message;
      });
    }
    else {
      window.alert("Please enter another email");
    }
  });
}
HTML登录按钮:

function login() {
  var userEmail = document.getElementById("email_field").value;
  var userPass = document.getElementById("password_field").value;
  var login = [];
  var usersRef = firebase.database().ref("Parking Lots");
  usersRef.orderByChild("admin").on("value", function (snapshot) {
    snapshot.forEach(function (childSnapshot) {
      var childData = childSnapshot.val();
      login.push(childData.admin);
    });
  });
  firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION)
    .then(function () {
      if(login.includes(userEmail)){
       return firebase.auth().signInWithEmailAndPassword(userEmail, userPass);
      }
    })
    .catch(function (error) {
      window.alert("Fail");
      var errorCode = error.code;
      var errorMessage = error.message;
    });
}
function login() {
  var userEmail = document.getElementById("email_field").value;
  var userPass = document.getElementById("password_field").value;
  var login = [];

  var usersRef = firebase.database().ref("Parking Lots");
  return usersRef.orderByChild("admin").once("value").then(function (snapshot) {
    snapshot.forEach(function (childSnapshot) {
      var childData = childSnapshot.val();
      login.push(childData.admin);
    });
    if(login.includes(userEmail)){
      firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION)
      .then(function () {
        return firebase.auth().signInWithEmailAndPassword(userEmail, userPass);
      })
      .catch(function (error) {
        window.alert("Password incorrect");
        var errorCode = error.code;
        var errorMessage = error.message;
      });
    }
    else {
      window.alert("Please enter another email");
    }
  });
}
我做了一些研究,对其他人来说,当你设置按钮type=“button”时,它起作用了,但对我来说不起作用

<div id="login_div" class="main-div">
    <h3>Firebase Web login Example</h3>
    <input type="email" placeholder="Email..." id="email_field" />
    <input type="password" placeholder="Password..." id="password_field" />
    <button type="button" onclick="login()">Login to Account</button>
  </div>

on()
是异步的,在数据库发生任何事情之前立即返回。您传递给
on()
的回调的第一个响应完全可能在调用
signInWithEmailAndPassword
后触发。您可能应该改为使用
once()
,并在尝试处理其查询结果之前使用它返回的承诺。另外,您应该使用
once()
,因为我强烈怀疑您不希望每次给定位置的数据发生更改时都不断调用该侦听器。

谢谢!我将代码更新为工作代码,以备将来参考。实际上,您应该将原始代码保留在那里,并使用正确的代码进行更新,以便可能有类似问题的人可以看到修复方法。