我对Cloud Firestore的安全规则有了解的问题。我不明白如何使用地图来检查用户的uid,以便他们可以访问其数据。
预先感谢您的回答
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
//TEST ONLY
//match /{document=**} {
// allow read, write;
//}
// match logged in user doc in users collection
match /users/users/{userId} {
allow create : if request.auth.uid != null;
allow read : if request.auth.uid == userId;
match /{anything=**} {
allow read, write : if request.auth.uid == userId;
}
}
}
}
这是我如何使用Firestore的一个示例:
// Collection reference
final CollectionReference _usersCollection = Firestore.instance.collection('users');
Stream<List<double>> get existingRecord {
return _usersCollection.document('users').snapshots()
.map(_existingRecordListFormSnapshot);
}
List<double> _existingRecordListFormSnapshot(DocumentSnapshot snapshot) {
...
double tips = snapshot.data['$uid']['data']['$year']['$month']['$week']['$day']['Tips'];
}
我的问题是我只希望用户有权访问其数据。为此,我制作了一个“用户”文档,其中包含一个“用户”映射,其中包含所有用户数据,并以其唯一的uid命名。而且我无法设置安全规则,因此只有用户才能访问其数据。
该图如下所示:users / users / [{userID},{userID},...]
我不知道我是否清楚,但是我真的找不到如何只允许用户访问用户数据映射中的数据
不要将所有用户放在同一个文档中,而是每个用户应该有一个文档(对于更简单的规则,该文档的名称等于Firestore uid)。
那么您的规则可以简单地是:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// match logged in user doc in users collection
match /users/{userId} {
allow create : if request.auth.uid != null;
allow read : if request.auth.uid == userId;
match /{anything=**} {
allow read, write : if request.auth.uid == userId;
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句