我知道这个问题经常被问到,但是提出的解决方案并没有为我解决。
我尝试设置一个基本的 iOS Firestore 应用程序,该应用程序在集合中写入一些文档。 我遵循标准程序,将 GoogleService-Info.plist 添加到我的项目中,并在启动时调用 `FirebaseApp.configure()。
我的 Firestore 规则如下所示:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
现在,一旦我尝试在集合中保存某些内容,Firebase 就会返回一个权限被拒绝的错误。
如果我的规则是这样的:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
写入被接受。显然,该请求似乎不包含任何身份验证信息,但我找不到关于此的任何其他文档。我错过了什么?
感谢您的帮助!
最佳答案
我的猜测是,由于您没有使用 firebase 身份验证服务进行身份验证,因此请求缺少身份验证信息。
来自 Firebase 文档:
Authentication One of the most common security rule patterns is controlling access based on the user's authentication state. For example, your app may want to allow only signed-in users to write data
service cloud.firestore {
match /databases/{database}/documents {
// Allow the user to access documents in the "cities" collection
// only if they are authenticated.
match /cities/{city} {
allow read, write: if request.auth.uid != null;
}
}
}
https://firebase.google.com/docs/firestore/security/rules-conditions
您可以在通过 firebase 身份验证服务进行身份验证后尝试规则,并检查是否通过规则。
关于iOS Firebase/Firestore 权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52422047/