首页 > 解决方案 > 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'
}

标签: javascriptfirebasefirebase-realtime-database

解决方案


这是一种声明具有特定角色的特定用户的方法,例如“admin”:

您为希望仅由“管理员”读取的数据设置了如下所示的安全规则:

".read": "auth != null && root.child('admins/' + auth.uid).exists()"

并且您将“admin”用户的 uid 声明为“admins”数据库节点的子节点:

- admins
  -h7yic7LeS123asdfsdgwPrfKZ2: true.     //<- key = uid of a admin user

您的非管理员用户可能仍然可以看到您的网页,但他们将无法获取您通常在此页面中为管理员显示的数据。


推荐阅读