[英]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: 或者使用此登录信息:
// 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.