我使用 firebase 8.10.0 白色 Vue Js。
我只使用谷歌作為身份驗證提供者,我希望用戶只讀他的資料,所以我使用firestore rules:
我的規則:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{id} {
allow read: if request.auth.uid == id;
}
}
}
這就是我獲取資料的方式:
...
let ref = firebase.firestore().collection("users");
// .where("__name__", "==", uid) or
ref.doc(uid).get().then((doc) => {
this.data = [{id: doc.id}];
})
// working well
ref.get().then((doc) => {
doc.forEach((doc) => {
this.data.push({ id: doc.id });
});
})
// err: Uncaught (in promise) FirebaseError: Missing or insufficient permissions.
...
我可以這樣做嗎?我該如何解決?: 我的錯 :')
uj5u.com熱心網友回復:
您正在嘗試查詢整個users集合,但您的安全規則允許用戶讀取以用戶 UID 作為檔案 ID 的檔案。安全規則不會過濾掉檔案,因此如果您僅嘗試獲取當前用戶的檔案,請嘗試get()在DocumentReference上使用:
// add .doc(<userId>)
let ref = firebase.firestore().collection("users").doc(currentUserId);
ref.get().then((doc) => {
this.data = [{id: doc.id}];
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/453577.html
標籤:javascript 火力基地 谷歌云火库 firebase-安全
下一篇:在React中復制元素
