Firebase规则文档建议构建条件,将经过身份验证的用户令牌(即request.auth)与目标 Firestore 文档进行比较。就像是:
match /posts/{postId} {
allow read, write: if (request.auth.uid != null) &&
(resource.data.tenantId == request.auth.token.tenantId);
}
Run Code Online (Sandbox Code Playgroud)
但是,Firebase 规则tenantId中似乎没有像其他相关身份验证字段那样可用 (例如、、uidemailemail_verified等)
一种选择似乎是使用SDK 作为自定义声明tenantId单独添加。但这会在用户对象上创建重复信息:firebase-admin
{
uid: 'nzjNp3QIfSR6uWy',
emailVerified: true,
displayName: 'pickleR'
...
tenantId: 'wubalubadubdub',
customClaims: { tenantId: 'wubalubadubdub' },
}
Run Code Online (Sandbox Code Playgroud)
另一种选择似乎是tenants在 Firestore 中创建集合。然而,这种方法似乎引入了不必要的复杂性并增加了所需的 Firestore 查询数量。
是否有其他方法可以访问tenantIdFirestore 规则和/或通过多租户使用 Firestore 的替代最佳实践?