抱歉,我是 Firebase 的新手,我正在嘗試第一次將我的應用程式部署到生產環境,但我正在努力解決安全規則。
我的 next.js 專案中有一個頁面,它從 firestore(嵌套子集合)中提取資料,如下所示:
useEffect(() => {
const getKids = async (user: any) => {
if (user) {
const collectionRef = collectionGroup(db, 'kids')
console.log(collectionRef, 'collectionRef')
const q = await query(collectionRef,
where("uid", "==", user.uid)
)
console.log(q, 'q')
const data = await getDocs(q)
data.forEach(doc => {
setKids(data.docs.map((doc) => ({
...doc.data(), id: doc.id
})))
})
}
}
getKids(user)
}, [user?.uid])
它還在提交時寫入它,但我只是想先將資料讀到頁面上(到目前為止還沒有運氣)......
這是我的 Firestore 的外觀:

我的規則:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{uid}/kids/{document=**} {
allow read, write: if request.auth.uid == uid;
}
}
}
有了這個,我希望當前登錄的用戶能夠查看用戶內部的孩子子集合中的所有檔案,但它不起作用。
我究竟做錯了什么?
uj5u.com熱心網友回復:
您的規則僅保護嵌套在users. 但是您的查詢正在使用一個集合組,該集合組可能在任何地方都有實體。如果您查看有關集合組安全規則的檔案,您會發現您需要使用不同的表單來允許訪問集合組查詢。
match /{path=**}/kids/{post} { ... }
但是,現在您在用于保護集合組的路徑中沒有 UID,因為集合組可以存在于任何地方。
這里的底線是你必須做以下兩件事之一:
- 不要使用集合組查詢,而是使用特定 uid 的用戶下的完整路徑來參考特定子集合。
- 以某種方式放寬您的規則以使集合組查詢起作用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/510127.html
標籤:Google Cloud Collective javascript火力基地谷歌云火库firebase-安全
