vue.js Firestore规则拒绝所有对嵌套集合的聊天应用程序的请求,我做错了什么?

h43kikqp  于 2023-04-21  发布在  Vue.js
关注(0)|答案(1)|浏览(108)

我目前正在构建一个聊天应用程序,我遇到了Firebase规则的问题。我想限制用户查看他们不参与的对话。但是,当我尝试实施规则时,似乎即使在检查对话是否存在时也会拒绝访问。我如何正确配置Firebase规则以允许用户仅查看他们参与的对话,而不是数据库里的所有对话

这是规则集,我试图只为对话实现。

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    
    // Allow users to read and write their own participant documents
    match /conversation/{conversationId}/participants/{userId} {
      allow read, write: if request.auth.uid == userId;
    }

    // Allow users to read and write messages in a conversation they are a participant of
    match /conversation/{conversationId}/messages/{messageId} {
      allow read, write: if exists(/databases/$(database)/documents/conversation/$(conversationId)/participants/$(request.auth.uid));
    }

    // Allow users to read and write their own conversation documents
    match /conversation/{conversationId} {
      allow read, write: if exists(/databases/$(database)/documents/conversation/$(conversationId)/participants/$(request.auth.uid));
    }
  }
}

访问已经在“最高级别”被拒绝,这是这里的这一部分:

// Allow users to read and write their own conversation documents
    match /conversation/{conversationId} {
      allow read, write: if exists(/databases/$(database)/documents/conversation/$(conversationId)/participants/$(request.auth.uid));
    }

我假设,这与方式有关,这些嵌套的对话正在工作,但我不知道我必须改变什么.我试着在谷歌firebase文档中阅读有关此,但我找不到任何关于这一点.如果你有任何想法,如何解决这个问题,请让我知道.谢谢!
我已经试过让它在多个“规则集”下工作。它总是在exists()-函数处失败。
我也试过这个,但似乎也不行:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

    function isUserInParticipants(conversationId) {
      return get(/databases/$(database)/documents/conversations/$(conversationId)/participants/$(request.auth.uid)).data != null;
    }

    match /conversations/{conversationId} {
      allow read, write: if request.auth != null && isUserInParticipants(conversationId);

      match /messages/{messageId} {
        allow read, write: if request.auth != null && isUserInParticipants(conversationId);
      }
    }
  }
}

这是我在Vue.js中使用的查询:

init() {
      const storeAuth = useStoreAuth();
      conversationCollectionRef = collection(db, 'conversation')
      conversationCollectionQuery = query(conversationCollectionRef) 
      this.getConversations()
    },
    async getConversations() {
      this.coversationsLoaded = false
      getConversationSnapshot = onSnapshot(conversationCollectionQuery, (snapshot) => {
        console.log('getConversations')
        console.log(snapshot)
        this.conversations = []
        snapshot.forEach((doc) => {
          this.getMessagesOfConversation(doc.id)
          const conversation = {
            id: doc.id,
            ...doc.data(),
          }
          this.conversations.push(conversation)
        })
      })
      this.coversationsLoaded = true
    },
omqzjyyz

omqzjyyz1#

您还没有共享正在运行的触发此故障的查询,但重要的是要记住Firestore rules are not filters
如果您的查询是这样的:

firestore.collection("conversation").get()

如果你期望Firestore只返回规则计算结果为true的对话,那么这是行不通的。Firestore会看到 * 一些 * 对话文档 * 不 * 计算结果为true,* 整个查询将失败 *。
您需要确保您运行的导致规则评估的查询只查看规则评估为true的对话,例如:

firestore.collection("conversation").where(<some condition>).get()

您可以在官方Firestore规则文档中阅读更多内容。

相关问题