我的 nextjs webapp 中有一個實時資料庫,我將它用于通知。
我的資料庫的架構如下:

我僅以管理員身份從服務器端寫入資料,但通過以下方式從客戶端讀取資料:
const fetchNotifications = () => {
const starCountRef = ref(db, `notifications/${user?.id}`);
const q = query(starCountRef, orderByChild('createdAt'), limitToLast(100));
onValue(q, (snapshot) => {
//Handle data
});
};
在這一刻,我有以下規則:
{
"rules": {
".read": "auth != null",
".write": false,
"notifications":{
"$user_id":{".indexOn":"createdAt"}
}
}
}
如您所見,經過身份驗證的每個人都可以閱讀所有內容。我希望用戶只閱讀他自己的通知。ObjectID 是來自我的 mongoDB 的參考。
例如,如果我ID = 1只想訪問路徑的資料,/notifications/1并且如果我嘗試從中獲取資料,/notifications/2那么我什么也得不到(或錯誤)。
首先,我如何從 firebase 中查看我在本地資料庫中使用的 ID?我要先讓他知道嗎?其次,我怎樣才能把它寫在我的規則中?
uj5u.com熱心網友回復:
如果直接在下面的密鑰notifications是 Firebase 身份驗證 UID(如您的規則所建議的那樣),您可以通過以下方式限制對用戶節點的訪問:
{
"rules": {
//".read": "auth != null", // ?? Remove this line
".write": false,
"notifications":{
"$user_id":{
".read": "auth.uid === $user_id", // ?? add this line
".indexOn":"createdAt"
}
}
}
}
另請參閱 Firebase 檔案,了解有關保護僅內容所有者的訪問權限。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/450879.html
標籤:javascript 火力基地 firebase-实时数据库
