我正在尝试执行以下写入-
guard let userid = Auth.auth().currentUser?.uid else {fatalError()}
let firstName = "firstName"
let lastName = "lastName"
let userName = "userName"
let uid = userid
let userInfo = [firstName:firstNameTextField.text!,
lastName:lastNameTextField.text!,
userName:userNameTextField.text!,
uid:userid]
Firestore.firestore().collection("users").document(userid).setData(userInfo) { (error) in
if error != nil {
print(error?.localizedDescription)
} else {
self.performSegue(withIdentifier: "goToFeed", sender: self)
}
}
这些是我的安全规则-
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read;
}
match /users/{userid} {
allow read: if request.auth.uid != null;
allow create: if request.resource.data.uid == request.auth.uid;
}
}
}
通过Xcode模拟器执行此写入操作时被拒绝,并显示错误-“缺少权限或权限不足”。
但是,当我尝试在Firestore规则模拟器上构建同一文档时,该文档被允许。
此外,我还尝试将该规则修改为此
match /users/{userid} {
allow create: if request.resource.data.uid == request.auth.uid &&
userid == request.auth.uid
}
在上述情况下,Xcode模拟器和Firestore模拟器都拒绝了它。
我在做什么错了?
一些说明-
在修改规则以使其成为开放数据库时,写入过程会非常完美。
文档结构如下-
let userInfo = [firstName:firstNameTextField.text!,
lastName:lastNameTextField.text!,
userName:userNameTextField.text!,
uid:userid]