簡體   English   中英

$uid 節點如何處理 firebase-realtime-database 規則?

[英]How does $uid node works on firebase-realtime-database rules?

我正在研究 ionic 上的 firebase-realtime-database,我對如何在 $uid 節點內“讀取”和“寫入”有點困惑。 我目前正在處理拒絕訪問。

節點看起來像這樣

rules {
"info": {     
   "$uid": {
      ".read": true,
      ".write": "$uid === auth.uid"
   }     
 }
}

雖然我的 json 數據庫看起來像這樣


{
  info: {
     01: {
     name:jay
     age:16
     }
  }
}


我有點困惑要采取哪些步驟來訪問 $uid 節點內的數據。 我確實嘗試將我的 json 數據庫節點更新為此。


{
  info: {
    f3de4734-8fb2-42bd-971d-8e693f8aab3b: {  // auth.uid of dummy@gmail.com
            01: {
            name:jay
            age:16
            }
    }
  }
}

和我的“getafdData() 函數”路徑目錄到“'info/'+ getuser”。

僅當“getuser”是文件的所有者或保存數據的節點時,上述更新才有效,但是我希望其他經過身份驗證的用戶也可以讀取它,而 $uid 無法理解。

我已經通過 firebase 使用“signInWithEmailAndPassword()”功能對登錄進行了身份驗證

這是我用來驗證的代碼

 try {
      var r = await this.fAuth.auth.signInWithEmailAndPassword(
        "dummy@gmail.com",
        "123456"
      );
      if (r) {
        console.log("Successfully logged in!");
        await this.getafdData(); \\\ get the data when login

      }

    } catch (err) {
      console.error(err);
    }
async getafdData(){

var firebaseRef = this.afd.database.ref();
let data = await new Promise(function(resolve,reject){
   return firebaseRef.child('info/')
      .on("child_added", function(snapshot) {
         console.log(snapshot.val());
         //resolve(data);


       }, function(err) {
         console.log(err);
         reject(err);
       })
    }); 
return data;
}

你能告訴我我做錯了什么嗎? 或者更確切地說,我應該采取正確的步驟來訪問 $uid 節點?

".read": true規則允許任何用戶從/info/$uid讀取數據, ".read": true是他們知道要讀取其數據的用戶的 UID 然而,在您的代碼中,您將偵聽器附加到/info並且沒有人對該節點具有讀取權限,因此讀取被拒絕。

如果您希望每個人都能一次性讀取所有用戶節點,您應該將".read": true,規則移動到規則的/info級別:

{
  "rules": {
    "info": {     
      ".read": true,
      "$uid": {
        ".write": "$uid === auth.uid"
    }     
  }
}

實際上,既然您說要允許其他經過身份驗證的用戶讀取用戶信息,那可能應該是:

{
  "rules": {
    "info": {     
      ".read": "auth != null",
      "$uid": {
        ".write": "$uid === auth.uid"
    }     
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM