Javascript 函数仅在用户已登录且具有特定UID时加载页面?

Javascript 函数仅在用户已登录且具有特定UID时加载页面?,javascript,firebase,firebase-realtime-database,firebase-authentication,firebase-storage,Javascript,Firebase,Firebase Realtime Database,Firebase Authentication,Firebase Storage,现在我有一个Firebase的基本登录页面。然而,我想知道,如果有人有特定的UID并且已经登录,我如何才能加载网页 现在我有: <h3 style="color: white;">Welcome User</h3> <p id="user_para" style="color: white;">Welcome to Firebase web login Example. You're

现在我有一个Firebase的基本登录页面。然而,我想知道,如果有人有特定的UID并且已经登录,我如何才能加载网页

现在我有:

    <h3 style="color: white;">Welcome User</h3>
    <p id="user_para" style="color: white;">Welcome to Firebase web login Example. You're currently logged in.</p>
    <button onclick="logout()">Logout</button>
  </div>
欢迎用户
欢迎来到Firebase web登录示例。您当前已登录

注销

但这有一个问题。它位于客户端,这意味着任何人都可以看到其中的数据。这将是未来的一个问题,因为该网站将用于存储敏感数据。有人知道如果此人已登录且具有特定UID,我如何编写规则/函数从页面中提取数据吗?

我就是这样做的,您应该有一些文件来检查数据库并使用true/false进行响应。。这是我的文件
FileToCheckDataBase

function CheckUid(uid) {
      var xhttp;
         xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function () {
          if (this.readyState == 4 && this.status == 200) {
           //here is the answer from file after checking in db
           if(this.response==1){
           //user uid is ok
           //do something.. usually continue
           }
           else{
             //  kick him out
           }
          }
      };
      xhttp.open("GET", "FileToCheckDataBase?uid="+uid, true);
      xhttp.send();
  
  }

如果您的数据和文件来自实时数据库和云存储,您可以使用Firebase的。您可以使用这些工具实现许多用例,因此我建议您端到端阅读链接文档。但是,如果您想允许对特定的已知UID(例如,
uidofknowuser
)进行读取访问,则应该是:

对于实时数据库:

对于云存储:

对于云存储上的文件,请记住下载URL不受这些规则的保护。因此,任何拥有文件下载URL的人都可以准备好该文件


如果您的HTML和JavaScript托管在Firebase托管上,那么就没有办法保护对它们的访问,因为Firebase托管上的所有资源都可以被任何知道其URL的人读取

如果您在其他地方托管HTML和JavaScript,请查看该产品的文档,看看它是否允许限制特定用户的访问

{
  "rules": {
      ".read": "auth.uid === 'uidOfKnownUser'"
    }
  }
}
service firebase.storage {
  match /{anyFile=**} {
    // Allow the request if the following conditions are true.
    allow read : if request.auth.uid === 'uidOfKnownUser'
  }
}