簡體   English   中英

Firebase 用戶和相關文檔的 Firestore 安全規則

[英]Firebase Firestore Security Rules for Users and Associated Documents

我已經閱讀了其他文章並觀看了 Firebase 文檔視頻,但這對我來說並不完全有意義。 有人可以驗證我是否正確設置了這些 Firebase Firestore 規則嗎? 服務器上,主要有兩個collections(用戶,數據)。 與用戶帳戶相關的信息存儲在“用戶”中,而與用戶創建的特定文檔相關的數據存儲在“數據”中。 這些文檔以用戶創建帳戶時分配的用戶身份驗證 ID 命名。

例如:

/users -> document named with userId (containing user's info)
/data -> document named with userId -> all data documents the user has created

我想允許用戶在“用戶”中讀取、寫入和更新他自己的用戶信息,以及允許用戶在“數據”中讀取、寫入、更新和刪除他的任何文檔。

這是我目前的設置,是否正確?

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

    match /users/{userId} {
        allow read, write, update: if request.auth.uid == userId;
        }
    
    match /data/{userId}/{documents=**} {
        allow read, write, update, delete: if request.auth.uid == userId;
        }
  }
}

要回答這個問題,有一個關於如何為您已經遵循的 Firestore 安全規則編寫條件的指南,通過修改@Frank 在評論中提到的安全規則。

另一種常見模式是確保用戶只能讀取和寫入他們自己的數據:

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
    // Applies to writes nonexistent user or users that doesn't have an account.
      allow create: if request.auth != null;
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM