首页 > 解决方案 > 特定 Firebase 规则

问题描述

我将 firestore 用作事件应用程序的 API 端点,并且需要能够为集合写入数据。怎么写规则?

该应用程序没有身份验证(它是完全公开的),所以我尝试了以下规则:

service cloud.firestore {
    match /databases/{database}/documents {
        match /{document=**} {
            allow read: if true;
        }
    }

    // Need to be able to write documents into the silent_disco collection
    match /databases/silent_disco/documents {
    match /{document=**} {
            allow write: if true;
        }
    }

    // Need to be able to write into the passport collection
    match /databases/passport/documents {
    match /{document=**} {
            allow write: if true;
        }
    }
}

当我使用模拟时,我可以按预期读取所有内容,但写入请求被拒绝。

标签: firebasegoogle-cloud-firestorefirebase-security

解决方案


以下应该有效:

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

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

        // Need to be able to write documents into the silent_disco collection
        match /silent_disco/{docId} {
            allow write: if true;
        }

        // Need to be able to write documents into the passport collection
        match /passport/{docId} {
            allow write: if true;
        }

    }
}

您可以观看有关该主题的优秀官方视频:https ://www.youtube.com/watch?v=eW5MdE3ZcAw


推荐阅读