问题描述
我在html中有一个复选框输入,用户可以在关闭浏览器后检查他/她是否希望保持登录该应用程序.我想在记住:默认"和记住:"sessionOnly"之间进行切换,具体取决于用户是否选中了该框.
I have a checkbox input in my html which the user can check if he/she wishes to stay logged into the app after the browser has been closed. I would like to change between remember: "default", and remember: "sessionOnly" depending on whether the user checks the box or not.
这是我的功能,用于检查html中的框是否已选中:
This is my function to check whether the box in the html is checked or not:
function checkLoginTerm() {
var result;
if(document.getElementById('#loginkeeping').checked) {
result = "sessionOnly";
} else {
result = "default";
}
return result;
}
然后,我在登录功能中调用该功能,以在用户进行如下身份验证后执行该功能:
Then I call the function in my login function to be executed after the user authenticates like this:
function loginUser(username, password) {
if (checkVariable(username)) {var username = document.getElementById("loginUsername").value;}
if (checkVariable(password)) {var password = document.getElementById("loginPassword").value;}
firebaseRef.authWithPassword({
email : username,
password : password
}, function(error, authData) {
if (error) {
alert("Login Failed! "+ error, error);
} else {
console.log("Authenticated successfully with payload:", authData);
}
}, {
remember: checkLoginTerm()
});
}
如果我这样做,登录过程将永远无法完成.但是,如果我在记住"之后直接放置适当的字符串:(例如,记住:"sessionOnly"),它将按照其提示进行操作.
If I do this, the login process will never complete. But if I put the appropriate string directly after remember: (for example remember: "sessionOnly"), it will do what its told.
推荐答案
正如罗布·迪马科(Rob DiMarco)在我的问题下方的评论中指出的那样,有两个小问题-
As pointed out by Rob DiMarco in the comments below my question, there were two minor issues -
- 该元素的ID为登录保留,而不是#loginkeeping
- 我在checkLoginTerm()函数中的条件语句中犯了逻辑错误
这篇关于Firebase-身份验证时动态更改记住属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!