firebase.auth().onAuthStateChanged 不工作 [英] firebase.auth().onAuthStateChanged Not Working

查看:32
本文介绍了firebase.auth().onAuthStateChanged 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to build a website that uses email auth for users. However, everytime I try to sign up or log in a user, the firebase.auth().onAuthStateChanged function fires, but doesn't recognize that a user has logged in or signed up. This is my current code. I know it works because it will alert me, "No user!" after every log in or sign up and because I can go into my Firebase console and see that the user has signed up. If anyone knows how to fix this, I would appreciate it!

谢谢!

代码:

 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]
}

推荐答案

看来我的问题是我的域未被授权进行该 Firebase 项目的 OAuth 操作...我忘记将我的域添加到授权列表中

It appears that my problem was my domain was not authorized for OAuth operations for that Firebase project... I had forgotten to add my domain to the list of authorized ones.

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

To fix this, go to your Firebase project > Authentication > Sign-In Method > Add your domain under Authorized Domains.

感谢 @Ymmanuel 的帮助.

这篇关于firebase.auth().onAuthStateChanged 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆