简体   繁体   English

"firebase.auth().onAuthStateChanged 不工作"

[英]firebase.auth().onAuthStateChanged Not Working

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.但是,每次我尝试注册或登录用户时,firebase.auth().onAuthStateChanged 函数都会触发,但无法识别用户已登录或注册。 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.每次登录或注册后,因为我可以进入我的 Firebase 控制台并查看用户已注册。 If anyone knows how to fix this, I would appreciate it!如果有人知道如何解决这个问题,我将不胜感激!

Thanks!谢谢!

Code:代码:

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

window.onload = function() {
  initApp();
};

CODE FOR LOGIN & SIGNUP:登录和注册代码:

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

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项目的OAuth操作授权...我忘记将我的域名添加到授权列表中。

To fix this, go to your Firebase project > Authentication > Sign-In Method > Add your domain under Authorized Domains. 要解决此问题,请转到您的Firebase项目>身份验证>登录方法>在授权域下添加您的域。

Thanks to @Ymmanuel for the help. 感谢@Ymmanuel的帮助。

For those poor souls working with react-native-firebase on iOS, there is no need to struggle any more.对于那些在 iOS 上使用 react-native-firebase 的可怜人来说,没有必要再挣扎了。

  1. Make sure you have added the generated Google plist to your project.确保您已将生成的 Google plist 添加到您的项目中。 2.Make sure you have generated and added your APN file to your firebase account. 2.确保您已生成 APN 文件并将其添加到您的 Firebase 帐户。
  2. Make sure the app bundle id you have set in Firebase consile matches with that in xcode.确保您在 Firebase 控制台中设置的 app bundle id 与 xcode 中的匹配。

对于那些在其本机Capacitor\/Cordova 应用程序<\/strong>中使用web js 库<\/strong>作为 firebase 的用户(以便在创建 pwa 时也减少代码),请参阅此处的解决方案: https<\/a> :\/\/stackoverflow.com\/a\/70902260\/17137614

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 React 中放置 firebase.auth().onAuthStateChanged 的位置 - where to place firebase.auth().onAuthStateChanged in React firebase.auth().onAuthStateChanged 无限循环 - firebase.auth().onAuthStateChanged looping infinitely Firebase 和 React TypeError: firebase.auth(...).onAuthStateChanged 不是 function - Firebase and React TypeError: firebase.auth(...).onAuthStateChanged is not a function firebase.auth()。currentUser和onAuthStateChanged始终为null,尽管已登录 - firebase.auth().currentUser and onAuthStateChanged always null inspite of being signed in 跨两个页面使用 firebase.auth().onAuthStateChanged 时的问题 - Issue when using firebase.auth().onAuthStateChanged accross two pages Javascript - firebase.auth().onAuthStateChanged 在登录/注销时未触发 - Javascript - firebase.auth().onAuthStateChanged not firing upon login/logout firebase.auth().onAuthStateChanged 登录后未执行? - firebase.auth().onAuthStateChanged not executed after sign-in? firebase.auth().onAuthStateChanged 在 vuex 中刷新页面后获取 null - firebase.auth().onAuthStateChanged getting null after refresh page in vuex Firebase.auth示例不起作用 - Firebase.auth Example Not Working 类型错误:调用“firebase.auth().onAuthStateChanged((user)”时无法读取未定义的属性“auth” - TypeError: Cannot read property 'auth' of undefined when calling " firebase.auth().onAuthStateChanged((user)"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM