firebase.auth().onAuthStateChanged 不工作

Posted

技术标签:

【中文标题】firebase.auth().onAuthStateChanged 不工作【英文标题】:firebase.auth().onAuthStateChanged Not Working 【发布时间】:2016-10-02 20:31:50 【问题描述】:

我正在尝试为用户构建一个使用电子邮件身份验证的网站。但是,每次我尝试注册或登录用户时,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]

【问题讨论】:

你能放更多你的代码吗...???了解登录和登录的方式和时间 我已将其添加到原始问题中。这两个函数都是通过点击按钮触发的。 看来这是一个普遍的问题,目前还没有答案... :/ ***.com/questions/37504466/… 我已经正确使用了这两种方法,但是使用了 react 和 angular...我将检查它们在纯 javascript 中的行为 是的,您需要在 firebase 控制台中转到您的项目,并在 authentication menu->sign-in-method 中将域添加到授权域,默认情况下 localhost 已授权,或者您可以添加您的如果您在本地测试,则为本地 IP 【参考方案1】:

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

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

感谢@Ymmanuel 的帮助。

【讨论】:

这正是我的问题,也是我的解决方案,谢谢!【参考方案2】:

对于那些在 ios 上使用 react-native-firebase 的可怜人来说,没有必要再挣扎了。

    确保已将生成的 Google plist 添加到项目中。 2.确保您已生成 APN 文件并将其添加到您的 Firebase 帐户。 确保您在 Firebase 控制台中设置的 app bundle id 与 xcode 中的匹配。

【讨论】:

以上是关于firebase.auth().onAuthStateChanged 不工作的主要内容,如果未能解决你的问题,请参考以下文章

firebase.auth().currentUser 为空

如何在 firebase-functions 中使用 firebase.auth()?

错误:名称“用户”在库“package:firebase_auth/firebase_auth.dart”和“package:quizmaker/models/user.dart”中定义

使用自己的身份验证服务器的firebase-auth模块和firebase-common-auth的重复

Firebase Auth - 更新用户的电子邮件后,Firebase Auth 会注销用户

firebase:admin.auth().updateUser() 导致 auth/user-token-expired