iOS Firebase/Firestore 权限被拒绝

标签 ios swift firebase google-cloud-firestore firebase-security

我知道这个问题经常被问到,但是提出的解决方案并没有为我解决。

我尝试设置一个基本的 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/

相关文章:

javascript - Canvas获取图像的触摸事件

ios - Xcode 7 你如何刷新配置文件?

ios - 如何在表格 View 单元格的背景中设置图像动画

android - 在 android 中禁用 google ML Kit 库的 firebase 日志记录

firebase - 我应该使用 Cloud Functions 制作 RESTful API 还是在应用程序中调用 Firebase 和 Firestore?

java - com.google.firebase.database.DatabaseException : Can't convert object of type java. lang.String 输入 com.example.pro.mychatapp.Friends

iphone - 触摸后,Objective C 注释 View 显示缓慢

ios - 防止切换开关打开并在 Swiftui 中显示警报

swift - 根据来自服务器的图像调整imageview大小,然后调整其他标签

ios - NSFetchedResultsController - 删除行 - 如何在行删除之前访问托管对象?