[英]Firebase security rules: allow 'get' when document has a specific field
我的用戶注冊過程看起來像
在 Stripe webhook/cloud function 中,我們獲取有關 email 和許可證的信息,並將其存儲在 firestore stripe-customers
集合中的一個新文檔中(具有生成的 ID)。
之后完成帳戶時,我們詢問電子郵件/密碼,使用 firebase 進行身份驗證,並在 firestore customers
集合中創建一個文檔(id = uid)
為了檢查用戶是否擁有有效許可證,我想通過匹配的 email 地址向經過身份驗證的用戶授予對 stripe-customer 的“讀取”權限。 所以我想以某種方式檢查customer/uid/email (or auth.token.email) == stripe-customer/id/email
我嘗試了以下安全規則,但顯然,對於“讀取”, request
沒有resource
屬性。
如何測試文檔中屬性的值?
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /customers/{uid} {
allow read, write: if request.auth != null && request.auth.uid == uid;
}
match /stripe-customers/{id} {
allow read: if request.resource.data.email == request.auth.token.email;
}
}
}
以及一些相關問題:
uid
作為文檔 id 與生成 id 和文檔 uid 屬性相比有什么好處嗎? 更新/創建文檔時, request.resource
變量包含文檔的未來 state。 而是使用resource.data
,它是文檔中存儲的所有字段和值的 map。
match /stripe-customers/{id} {
allow read: if resource.data.email == request.auth.token.email;
}
您可以在文檔中閱讀更多相關信息。
使用 uid 作為文檔 id 與生成 id 和文檔 uid 屬性相比有什么好處嗎?
取決於用例,在這里我更喜歡用戶的身份驗證 UID 作為客戶集合中的文檔 ID,以及來自 Stripe 的客戶 ID 作為 stripe-customers 集合中的文檔 ID。 沒有任何優勢,但是從 Firestore .doc('customers' + uid)
獲取用戶文檔比使用where('userId', '==', uid)
查詢更容易。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.