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