javascript - Firebase 用户设置?
问题描述
我正在为糖尿病用户开发应用程序。我设法创建了注册用户按钮并且它可以工作,但我还希望将新管理员添加到系统中。问题是在管理员登录页面,普通用户也可以登录。我该如何实现这个......我需要帮助吗?
document.getElementById("btnSignUp").addEventListener('click', e=>{
const userEmail = document.getElementById("email_field").value;
const userPass = document.getElementById("password_field").value;
firebase.auth().createUserWithEmailAndPassword(userEmail, userPass).catch(function(error) {
console.log(error.message);
});
})
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
//window.location = 'main.html'
document.getElementById("user_div").style.display = "block";
document.getElementById("login_div").style.display = "none";
var user = firebase.auth().currentUser;
if(user != null){
var email_id = user.email;
document.getElementById("user_para").innerHTML = "Welcome User : " + email_id;
}
} else {
// No user is signed in.
document.getElementById("user_div").style.display = "none";
document.getElementById("login_div").style.display = "block";
}
});
function login(){
var userEmail = document.getElementById("email_field").value;
var userPass = document.getElementById("password_field").value;
firebase.auth().signInWithEmailAndPassword(userEmail, userPass).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
window.alert("Error : " + errorMessage);
// ...
});
}
function logout(){
firebase.auth().signOut();
window.location='index.html'
}
解决方案
这是一种声明具有特定角色的特定用户的方法,例如“admin”:
您为希望仅由“管理员”读取的数据设置了如下所示的安全规则:
".read": "auth != null && root.child('admins/' + auth.uid).exists()"
并且您将“admin”用户的 uid 声明为“admins”数据库节点的子节点:
- admins
-h7yic7LeS123asdfsdgwPrfKZ2: true. //<- key = uid of a admin user
您的非管理员用户可能仍然可以看到您的网页,但他们将无法获取您通常在此页面中为管理员显示的数据。
推荐阅读
- python - 吐司通知程序离开托盘图标循环后
- android - Android Studio:无需我干预即可编辑文件
- python - Python函数返回列表
- html - How to align divs like in chat?
- node.js - 使用增量循环函数
- android - 一些Android制造商杀死了FCM,但GCM曾经从多任务托盘中滑出
- azure - AADSTS90002:未找到租户“xx”。如果租户没有活动订阅,则可能会发生这种情况
- javascript - JQuery ScrollTop 工作,但只是暂时的?
- electron - 如何判断消息是来自主进程中的 send 还是 sendSync?
- javascript - 如何按内部结构对列表进行排序?