javascript - 在导航到新页面之前检查用户身份验证 ID 是否存在于 firebase 数据库中
问题描述
用户登录成功后,我想使用用户 UID 来验证用户是否选择了正确的学校,我已经能够完成这个任务,但问题是,我必须点击两次登录按钮才能采取行动影响。
var sbmit = document.getElementById("submit");
sbmit.onclick = function (e) {
var email = document.getElementById("email").value;
var password = document.getElementById("password").value;
var s = document.getElementById("school");
var school = s.options[s.selectedIndex].value;
e.preventDefault();
if (school == null || school == "") {
alert("Please select your school")
return false;
} else if (email == null || email == "") {
alert('email can\'t be empty')
return false;
} else if (password == null || password == "") {
alert("Password ca\'t be empty")
return false;
} else {
toggleSignIn();
//After signing in, use the user auth id to check if the user exist in the selected school
firebase.auth().onAuthStateChanged(user => {
if (user) {
ref = database.ref('/schools/')
userId = firebase.auth().currentUser.uid;
ref.child(school).orderByChild("AuthID").equalTo(userId).once("value", snapshot => {
if (snapshot.exists()) {
document.location.href = "/Result"
} else {
alert("You have selected the wrong school")
}
});
}
});
}
}
解决方案
onAuthStateChanged
在这样的点击处理程序中有一个监听器是非常不寻常的。你更有可能想要类似的东西:
...
} else {
toggleSignIn();
ref = database.ref('/schools/')
userId = firebase.auth().currentUser.uid;
ref.child(school).orderByChild("AuthID").equalTo(userId).once("value", snapshot => {
if (snapshot.exists()) {
document.location.href = "/Result"
} else {
alert("You have selected the wrong school")
}
});
}
顺便说一句:如果您可以通过查询为用户查找学校,是否有任何具体原因为什么您不简单地在表单中为他们预先填充该值?
推荐阅读
- powershell - 使用 PowerShell 问题重置 AD 用户密码
- c# - c# Selenium Chromedriver打开错误的url onload
- javascript - 失败的道具类型:提供给`Route`的`object`类型的`component`,预期的`function`
- php - Laravel 使用 HTTPS
- css - 字体功能设置:lnum 1;不适用于超过 500 的文本字体粗细
- c# - ListView 字符串部分字体颜色
- ecmascript-6 - 为什么我会收到这些错误
- python - 如何运行在 Heroku 中使用错误库的脚本?
- python - 用 BeautifulSoup 编码错误的汉字
- javascript - 单击时更改按钮上的文本