规则:
match /transactions/{transaction} {
allow read: if request.auth.uid == resource.data.user_id;
}
数据库:
ts文件:
this.transCollection = afs.collection<Transaction>('transactions',ref => ref.where('cust_id', '==', this.cust_id).orderBy('created','desc'));
this.transactions = this.transCollection.snapshotChanges().pipe(
map(actions => actions.map(a => {
const data = a.payload.doc.data() as Transaction;
const id = a.payload.doc.id;
return { id, data };
}))
);
错误: core.js:1673错误错误:缺少权限或权限不足。
答案 0 :(得分:2)
安全规则不会自行过滤数据。相反,安全规则仅允许被保证仅与允许的文档匹配的查询。如果查询有可能返回不允许的文档,则安全规则会立即拒绝该查询。
您的查询在cust_id
上过滤,而规则在user_id
上允许/禁止。这意味着您的查询正在尝试检索您无权访问的文档。由于查询可能返回错误的user_id
文档,因此规则会拒绝查询。参见https://firebase.google.com/docs/firestore/security/rules-query
我最好的猜测是您也希望您的规则也与cust_id
相匹配:
allow read: if request.auth.uid == resource.data.cust_id;
使用这些规则,它们与查询的条件相匹配,因此将允许读取/侦听器。
答案 1 :(得分:0)
得到解决方案后,条件没有问题,但是在具有基于字段的条件的uid之前,需要允许经过身份验证的用户发出请求,然后读取user_id是否与当前uid匹配:
// Allow a read if request user id is same as resourse user_id
allow read: if request.auth.uid == resource.data.user_id;
规则:
match /transactions/{transaction} {
allow read: if request.auth.uid == resource.data.user_id;
}
ts文件:
//Add user_id in where condition
this.transCollection = afs.collection<Transaction>('transactions',ref => ref.where('user_id', '==', this.user_id).where('cust_id', '==', this.cust_id).orderBy('created','desc'));
this.transactions = this.transCollection.snapshotChanges().pipe(
map(actions => actions.map(a => {
const data = a.payload.doc.data() as Transaction;
const id = a.payload.doc.id;
return { id, data };
}))
);