在 Firebase 安全规则中使用 UID 作为映射键

Flo*_*her 3 firebase firebase-security firebase-authentication google-cloud-firestore

我正在使用 Firestore 构建一个聊天应用程序,每个聊天室都有一个参与者地图,如下所示:

在此输入图像描述

键是用户对应的UID。我想使用此地图来决定哪个用户可以访问聊天。在我的安全规则中,我当前检查登录用户的 UID 是否存在于该映射中以允许读写访问:

allow read, write: if request.auth.uid in resource.data.participants
Run Code Online (Sandbox Code Playgroud)

但这仅检查用户是否在此地图中,而不检查该值是否确实为真。我也想检查一下。

我基本上想要的是这样的:

allow read, write: if resource.data.participants.request.auth.uid == true
Run Code Online (Sandbox Code Playgroud)

但这实际上行不通。如何使用当前用户的 UID 作为地图检查的键?

Dou*_*son 5

使用方括号使用您想要的任何字符串表达式对Map进行索引:

allow read, write: if resource.data.participants[request.auth.uid] == true
Run Code Online (Sandbox Code Playgroud)