Firestore安全规则错误:FirebaseError:缺少权限或权限不足

时间:2020-07-06 21:42:48

标签: javascript firebase google-cloud-firestore firebase-security

预期行为

目标是重新设计我的安全规则,以便只有经过身份验证的用户才能读取和写入该特定用户的数据。这是基于用户UID的,这是我们识别单个用户的方式(这是通过Firebase身份验证完成的,因此不确定问题出在哪里)。

我的数据库结构

enter image description here

我的安全规则是

service cloud.firestore {
  match /users/{userId} {
   allow read: if request.auth.uid == userId;
  }
}

错误

FirebaseError:缺少权限或权限不足

关于我在这里可以做什么的任何建议将不胜感激。我无法找到其他适用于所问其他问题的东西。

我的查询

fbDB.collection('users').doc(user.uid).collection('practitionerClients').doc(trackingClientNameCheck).collection("sessions").doc(trackingClientNameCheck + currentTime).set({

    name: trackingClientName,
    timeOfSession: currentTime,
    vocValues: vocValueSaved,
    sudsValues: sudsValueSaved, 
    recallValues: recallValueSaved,
    moodValues: moodValueSaved,
    clientNotes: trackingClientNotes,
    triggeringEventProcessed: trackingTriggeringEvent,
    positiveBelief: trackingPositiveBelief,
    negativeBelief: trackingNegativeBelief

}).then(function() {
    console.log("Document successfully written!");

})

.catch(function(error) {
    console.error("Error writing document: ", error);
});;

* 我的错误

Error getting document: FirebaseError: Missing or insufficient permissions.

    at new t (https://www.gstatic.com/firebasejs/6.2.4/firebase-firestore.js:1:47216)
    
    at t.fromRpcStatus (https://www.gstatic.com/firebasejs/6.2.4/firebase-firestore.js:1:236502)
    at t.fromWatchChange (https://www.gstatic.com/firebasejs/6.2.4/firebase-firestore.js:1:245756)
    at t.onMessage (https://www.gstatic.com/firebasejs/6.2.4/firebase-firestore.js:1:189313)
    at https://www.gstatic.com/firebasejs/6.2.4/firebase-firestore.js:1:188114
    at https://www.gstatic.com/firebasejs/6.2.4/firebase-firestore.js:1:188899
    at https://www.gstatic.com/firebasejs/6.2.4/firebase-firestore.js:1:60566

2 个答案:

答案 0 :(得分:2)

您需要在规则中指定该子集合

service cloud.firestore {
  match /databases/{database}/documents {

    match /users/{userId} {
      allow read: if request.auth.uid == userId;

      match /practitionerClients/{clientId} {
        allow read: if request.auth.uid == userId;
      }

    }
  }
}

无论何时添加这样的新子集合,通常都必须指定该子集合或为该路径添加通配符规则。因此,除了上述规则,您可以这样做

service cloud.firestore {
  match /databases/{database}/documents {

    match /users/{userId}/{document=**} {
      allow read: if request.auth.uid == userId;
    }
  }
}

您可以阅读更多here

答案 1 :(得分:0)

您的规则与任何文档都不匹配。您必须保留初始规则集中给定的/databases/{database}/documents的匹配项,如documentation所示。

service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId} {
     allow read: if request.auth.uid == userId;
    }
  }
}