Javascript firebase.auth().onAuthStateChanged不工作

Javascript firebase.auth().onAuthStateChanged不工作,javascript,html,authentication,web,firebase,Javascript,Html,Authentication,Web,Firebase,我正试图建立一个网站,使用电子邮件认证的用户。但是,每次我尝试注册或登录用户时,都会触发firebase.auth().onAuthStateChanged函数,但无法识别用户已登录或注册。这是我当前的代码。我知道它是有效的,因为每次登录或注册后,它都会提醒我“没有用户!”,因为我可以进入Firebase控制台,查看用户是否已注册。如果有人知道如何解决这个问题,我将不胜感激 谢谢 代码: function initApp() { firebase.auth().onAuthStateCha

我正试图建立一个网站,使用电子邮件认证的用户。但是,每次我尝试注册或登录用户时,都会触发firebase.auth().onAuthStateChanged函数,但无法识别用户已登录或注册。这是我当前的代码。我知道它是有效的,因为每次登录或注册后,它都会提醒我“没有用户!”,因为我可以进入Firebase控制台,查看用户是否已注册。如果有人知道如何解决这个问题,我将不胜感激

谢谢

代码:

 function initApp() {
  firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
      alert("Signed in user!")
    } else {
      alert("No user!")
    }
  });
}

window.onload = function() {
  initApp();
};
function toggleSignIn() {
  if (firebase.auth().currentUser) {
   alert("Sign out")
    firebase.auth().signOut();
    // [END signout]
  } else {
    var email = document.getElementById('email').value;
    var password = document.getElementById('pass').value;
    if (email.length < 4) {
      alert('Please enter an email address.');
      return;
    }
    if (password.length < 4) {
      alert('Please enter a password.');
      return;
    }
    // Sign in with email and pass.
    // [START authwithemail]
    firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      // [START_EXCLUDE]
      if (errorCode === 'auth/wrong-password') {
        alert('Wrong password.');
      } else {
        console.error(error);
      }
      // [END_EXCLUDE]
    });
    // [END authwithemail]
  }
}

function handleSignUp() {
  var email = document.getElementById('semail').value;
  var password = document.getElementById('spass').value;

  if (password.length < 6) {
    alert('Password must be 6 characters or more!');
    return;
  }
  // Sign in with email and pass.
  // [START createwithemail]
  firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
    // [START_EXCLUDE]
    if (errorCode == 'auth/weak-password') {
      alert('The password is too weak.');
    } else {
      console.error(error);
    }
    // [END_EXCLUDE]
  });
  // [END createwithemail]
}
登录和注册代码:

 function initApp() {
  firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
      alert("Signed in user!")
    } else {
      alert("No user!")
    }
  });
}

window.onload = function() {
  initApp();
};
function toggleSignIn() {
  if (firebase.auth().currentUser) {
   alert("Sign out")
    firebase.auth().signOut();
    // [END signout]
  } else {
    var email = document.getElementById('email').value;
    var password = document.getElementById('pass').value;
    if (email.length < 4) {
      alert('Please enter an email address.');
      return;
    }
    if (password.length < 4) {
      alert('Please enter a password.');
      return;
    }
    // Sign in with email and pass.
    // [START authwithemail]
    firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      // [START_EXCLUDE]
      if (errorCode === 'auth/wrong-password') {
        alert('Wrong password.');
      } else {
        console.error(error);
      }
      // [END_EXCLUDE]
    });
    // [END authwithemail]
  }
}

function handleSignUp() {
  var email = document.getElementById('semail').value;
  var password = document.getElementById('spass').value;

  if (password.length < 6) {
    alert('Password must be 6 characters or more!');
    return;
  }
  // Sign in with email and pass.
  // [START createwithemail]
  firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
    // [START_EXCLUDE]
    if (errorCode == 'auth/weak-password') {
      alert('The password is too weak.');
    } else {
      console.error(error);
    }
    // [END_EXCLUDE]
  });
  // [END createwithemail]
}
function toggleSignIn(){
if(firebase.auth().currentUser){
警报(“注销”)
firebase.auth().signOut();
//[结束签出]
}否则{
var email=document.getElementById('email')。值;
var password=document.getElementById('pass')。值;
如果(电子邮件长度<4){
警报('请输入电子邮件地址');
返回;
}
如果(密码长度<4){
警报('请输入密码');
返回;
}
//使用电子邮件登录并传递。
//[启动authwithemail]
firebase.auth(){
//在这里处理错误。
var errorCode=error.code;
var errorMessage=error.message;
//[开始时不包括]
如果(错误代码==='auth/错误密码'){
警报(“密码错误”);
}否则{
控制台错误(error);
}
//[完]
});
//[结束电子邮件]
}
}
函数handleSignUp(){
var email=document.getElementById('semail').value;
var password=document.getElementById('spass')。值;
如果(密码长度<6){
警报('密码必须为6个字符或更多!');
返回;
}
//使用电子邮件登录并传递。
//[从电子邮件开始]
firebase.auth().createUserWithEmailAndPassword(电子邮件,密码).catch(函数(错误){
//在这里处理错误。
var errorCode=error.code;
var errorMessage=error.message;
//[开始时不包括]
如果(错误代码==“验证/弱密码”){
警报('密码太弱');
}否则{
控制台错误(error);
}
//[完]
});
//[以电子邮件结束]
}

我的问题似乎是我的域未被授权用于该Firebase项目的OAuth操作。。。我忘了将我的域名添加到授权域名列表中

要解决此问题,请转到Firebase项目>身份验证>登录方法>在授权域下添加域


感谢您的帮助。

您能把更多的代码放在???为了了解如何以及何时进行登录和签名,我们已将其添加到原始问题中。这两个函数都是通过点击按钮触发的。这似乎是一个常见的问题,目前还没有答案…:/我正确地使用了这两种方法,但使用了react和angular。。。我将使用纯javascriptYep检查它们的行为。您需要在firebase控制台中转到您的项目,并在“身份验证”菜单->登录方法中将域添加到授权域中,默认情况下,localhost是授权的,或者如果您正在本地测试,您可以添加您的本地IP。这正是我的问题,也是我的解决方案。谢谢!