【发布时间】:2021-09-27 12:50:40
【问题描述】:
我需要为没有身份验证的文档集合添加一个匹配数据库。这样做的正确方法是什么? 这是当前的安全规则:
service cloud.firestore {
match /databases/{database}/documents {
function authenticated() { return request.auth.uid != null }
match /users/{userId} {
allow get: if authenticated() && request.auth.uid == userId;
allow create: if authenticated() && request.auth.uid == userId;
allow update, delete: if authenticated() && request.auth.uid == userId;
}
match /users/{userId}/products/{productId} {
allow get: if authenticated() && request.auth.uid == userId;
allow list: if authenticated() && request.auth.uid == userId;
allow create: if authenticated() && request.auth.uid == userId;
allow update, delete: if authenticated() && request.auth.uid == userId;
}
}
}
我尝试添加此内容,但仍然收到 Firestore 权限不足错误:
function notAuthenticated() {
return request.auth == null;
}
match /share/{id}/documents {
allow get: if notAuthenticated();
allow create: if notAuthenticated();
allow read: if notAuthenticated();
这是向 Firestore 添加“共享”的 JS 代码。:
const db = firebase.firestore()
const docRef = db.collection('share').doc(val)
docRef
.get()
.then(doc => {
if (doc.exists) {
console.log('Document data:', doc.data().id)
} else {
console.log('No such document!')
}
})
.catch(error => {
console.log('Error getting document:', error)
})
enter code here
【问题讨论】:
-
“我仍然收到 Firestore 权限不足错误:”您能显示重现此错误的代码吗?特别是那里:您可以登录
firebase.auth().currentUser以表明它是null,因为这是您的安全规则所要求的。
标签: javascript google-cloud-firestore firebase-security