首頁  >  問答  >  主體

Firestore 規則拒絕對具有巢狀集合的聊天應用程式的每個請求。我究竟做錯了什麼?

我目前正在建立一個聊天應用,但遇到了 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));
    }

我認為,這與這些嵌套對話的工作方式有關,但我不知道我必須改變什麼。我嘗試在 google 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
    },

P粉627136450P粉627136450407 天前506

全部回覆(1)我來回復

  • P粉459578805

    P粉4595788052023-09-09 14:03:04

    您尚未共用正在執行的觸發此故障的查詢,但請務必記住Firestore 規則不是過濾器

    如果您的查詢是這樣的:

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

    如果您期望 Firestore 只傳回規則計算結果為 true 的對話,那麼這是行不通的。 Firestore 將發現某些對話文件不會評估為true整個查詢將失敗

    您需要確保您執行的導致規則評估的查詢僅查看規則評估為 true 的對話,例如:

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

    您可以在官方 Firestore 規則文件.

    回覆
    0
  • 取消回覆