简体   繁体   English

Firebase OnAuthStateChanged函数不返回任何内容

[英]Firebase OnAuthStateChanged function not returning anything

I use Firebase password-based Authentication in my login.html . 我在login.html使用Firebase基于密码的身份验证。 I have 2 text box and 3 buttons. 我有2个文本框和3个按钮。

I use firebase.OnAuthStateChanged to know if user is logged in or logged out. 我使用firebase.OnAuthStateChanged来了解用户是否已登录或注销。 When the user is logged in, the password text box and login button should be hidden, so I use display : none; 当用户登录时,密码文本框和登录按钮应该被隐藏,所以我使用display : none; for it. 为了它。 And when the user is logged out, the logout button should be hidden. 当用户注销时,应该隐藏注销按钮。


The buttons are working perfectly, but the real problem is in the if elseif statement. 按钮工作正常,但真正的问题是在if elseif语句中。

Below is my JavaScript code. 以下是我的JavaScript代码。

PS : Feel free to use the Firebase config to test. PS:随意使用Firebase配置进行测试。 Or use this login: 或者使用此登录信息:

  • email: sa@sa.sa 电子邮件:sa@sa.sa
  • password : 123456 密码:123456

// Initialize Firebase
  var config = {
    apiKey: "AIzaSyDjYqNKbZaom0jplOusloWlr5mOhW2WbgQ",
    authDomain: "awes-e3043.firebaseapp.com",
    databaseURL: "https://awes-e3043.firebaseio.com",
    storageBucket: "awes-e3043.appspot.com",
    messagingSenderId: "191054424051"
  };
  firebase.initializeApp(config);
// SignIn
$("button#in").click(function(){
var email  = document.getElementById('logemail');
var password  = document.getElementById('logpassword');
firebase.auth().signInWithEmailAndPassword(email.value, password.value).catch(function(error) {
   console.log(error.code);
   console.log(error.message);
}).then(function(){
  location.reload();
});
});
// SignOut
$("button#out").click(function(){
firebase.auth().signOut().then(function() {
   console.log("Logged out!")
   location.reload();
}, function(error) {
   console.log(error.code);
   console.log(error.message);
});
});

  $(document).ready(function(){
function usigned(){
 firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    return true;
  } else {
    return false;
  }
}); }
if (usigned() == true) {
  $("#hlogp").css("display", "none");
  $("button#in").css("display", "none");
  $("button#out").css("display", "inline-block")
}else if(usigned() == false) {
  $("button#out").css("display", "none");
  $("#hlogp").css("display", "block");
  $("button#in").css("display", "block");
}
});

Like most firebase calls, onAuthStateChanged() runs asynchronously, so you need to set it up with a callback: 与大多数firebase调用一样,onAuthStateChanged()以异步方式运行,因此您需要使用回调进行设置:

function usigned(bool) {
  if (bool) {
    $("#hlogp").css("display", "none");
    $("button#in").css("display", "none");
    $("button#out").css("display", "inline-block");
  } else {
    $("button#out").css("display", "none");
    $("#hlogp").css("display", "block");
    $("button#in").css("display", "block");
  }
}

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    usigned(true);
  } else {
    usigned(false);
  }
});

Alternatively, you could simply put the usigned() code inline... 或者,您可以简单地将usigned()代码放入内联...

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    $("#hlogp").css("display", "none");
    $("button#in").css("display", "none");
    $("button#out").css("display", "inline-block")
  } else {
    $("button#out").css("display", "none");
    $("#hlogp").css("display", "block");
    $("button#in").css("display", "block");
  }
});

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

相关问题 Firebase onAuthStateChanged 总是返回 undefined - Firebase onAuthStateChanged always returning undefined Firebase 身份验证 onAuthStateChanged 在函数主体后响应 - Firebase authentication onAuthStateChanged responding after function body 如何在类星体中处理firebase的onAuthStateChanged函数 - How to handle firebase's onAuthStateChanged function in quasar Firebase getDownloadURL()不返回任何内容 - Firebase getDownloadURL() not returning anything Firebase 和 React TypeError: firebase.auth(...).onAuthStateChanged 不是 function - Firebase and React TypeError: firebase.auth(...).onAuthStateChanged is not a function Firebase 9.4.1 和 Javscript 身份验证功能“onAuthStateChanged”运行缓慢 - Firebase 9.4.1 and Javscript Authentication function "onAuthStateChanged" works slowly Firebase / React:onAuthStateChanged()是在注册函数的时间之前还是之后触发的? - Firebase/React: is onAuthStateChanged() triggered before or after the then of a sign up function? Firebase onAuthStateChanged取消订阅递归 - Firebase onAuthStateChanged unsubscribe recursion Firebase 停止监听 onAuthStateChanged - Firebase stop listening onAuthStateChanged 反应本地firebase onAuthStateChanged - react native firebase onAuthStateChanged
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM