firebase 规则如何让用户只能读取他的数据
firebase rules how to make user can read only his data
我用的是firebase 8.10.0 白Vue Js.
我只使用 Google 作为身份验证提供者,我希望用户只读他的数据,所以我使用 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.
...
我可以这样做吗?我该如何解决? : 我的错 :')
您正在尝试查询整个 users
集合,但您的安全规则允许用户读取将用户的 UID 作为文档 ID 的文档。安全规则不会过滤掉文档,因此如果您只想获取当前用户的文档,请尝试在 DocumentReference:
上使用 get()
// add .doc(<userId>)
let ref = firebase.firestore().collection("users").doc(currentUserId);
ref.get().then((doc) => {
this.data = [{id: doc.id}];
})
我用的是firebase 8.10.0 白Vue Js.
我只使用 Google 作为身份验证提供者,我希望用户只读他的数据,所以我使用 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.
...
我可以这样做吗?我该如何解决? : 我的错 :')
您正在尝试查询整个 users
集合,但您的安全规则允许用户读取将用户的 UID 作为文档 ID 的文档。安全规则不会过滤掉文档,因此如果您只想获取当前用户的文档,请尝试在 DocumentReference:
get()
// add .doc(<userId>)
let ref = firebase.firestore().collection("users").doc(currentUserId);
ref.get().then((doc) => {
this.data = [{id: doc.id}];
})