【问题标题】:code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null代码=PERMISSION_DENIED,描述=缺少权限或权限不足。,原因=空
【发布时间】:2020-02-11 14:26:53
【问题描述】:

我无法在有规则的情况下将数据读/写到 Firestore。

我可以访问我的数据并对其进行操作,而无需设置如下所示的规则:

match /{document=**} {
  allow read, write: if true;
}

但我收到以下规则的“权限被拒绝”错误。

match /{document=**} {
  allow read, write: if request.auth.uid != null;
}

这是我的飞镖代码:

if (uid == null) {
  user = await a.currentUser();
  uid = user.uid;
}
DocumentReference docRef = firestore.collection('users').document(uid);
CollectionReference locations = docRef.collection('locations');


await locations.document().setData(<String, double>{
  'latitude': latitude,
  'longitude': longitude,
});

我可以想到这个请求失败的两种情况:

  1. request.auth.uid == null。但我有一个用户在此查询之前使用 firebase auth 登录。
  2. 我的安全规则和查询不匹配。

请帮我调试这个问题。

编辑 1:

  1. 我在我的 firebase 查询期间对用户进行了身份验证,因为我将代码更改为仅在用户通过以下身份验证时才输入:
    User u = await a.onAuthStateChanged.first;

    if (u != null) {....}
  1. 我能够使用 Firebase 模拟器通过身份验证进行读/写。

我是否可以使用 cloud_firestore 包打印从 dart 发送到 firebase 的实际请求?

编辑 2

  1. Firestore.instance 正在使用规则
  2. Firebaseapp 使用 configure 配置到我的特定应用,选项不起作用。

对为什么会发生这种情况有任何帮助吗?

【问题讨论】:

    标签: flutter firebase-security


    【解决方案1】:

    如果您想允许任何人上传文件,只需将您的规则更改为如下。

    allow read, write;
    

    如果您只想允许经过身份验证的用户允许将文件或文档上传到您的 Firebase,请将您的规则更改为

    allow read, write: if request.auth != null;
    

    【讨论】:

    • 像作者那样使用 request.auth.uid != null 肯定没有错
    【解决方案2】:

    将您的规则更改为allow read,write: if request.auth !=null;

    【讨论】:

    • 我试过这个解决方案。它没有解决问题。我仍然面临同样的错误 - “失败:状态{code=PERMISSION_DENIED,描述=缺少或权限不足。,原因=null}”
    • 检查您的登录信息
    • 当时我有用户登录,因为我的完整错误是:W/Firestore(28828): (21.1.1) [Firestore]: Listen for Query(users/{userid}/locations order name)失败:状态{code=PERMISSION_DENIED,描述=缺少或权限不足。,原因=null}。该错误中的 {userid} 是实际的用户 ID 值。
    • 使用 Firebase 规则模拟器(在控制台中)。在那里,您还可以模拟登录用户。这样您就可以确定它不是您应用中的 Auth 事物。顺便说一句:request.auth.uid != null 绝对没问题。
    • 我运行了 firebase 模拟器。它允许我通过身份验证读/写。为了确保我有一个经过身份验证的用户,我将代码更改为: User u = await a.onAuthStateChanged.first; if (u != null) {....} 我的代码进入了 if 块,这意味着 firebase 用户在我的代码中已经通过了身份验证。
    猜你喜欢
    • 2020-09-25
    • 2020-09-02
    • 2021-12-31
    • 2021-08-24
    • 1970-01-01
    • 2019-01-06
    • 2018-04-25
    • 2020-09-06
    • 2022-01-15
    相关资源
    最近更新 更多