【问题标题】:How to access custom claims inside Cloud Firestore rules?如何访问 Cloud Firestore 规则中的自定义声明?
【发布时间】:2021-10-11 18:06:42
【问题描述】:

我有一个用户有以下自定义用户声明,

 customClaims: { role: 'admin' },

如何在 Cloud Firestore 规则中访问此角色属性 (admin)?

我正在使用下面的代码,但它不起作用。需要做什么才能工作?

match /companies/{document=**} {
  allow read: if request.auth != null;
  allow write: if request.auth != null && request.customClaims.role == "admin";
}

【问题讨论】:

    标签: firebase google-cloud-firestore firebase-security


    【解决方案1】:

    所有声明(包括自定义声明)都在 request.auth.token 变量下可用。请注意,声明可能需要一个小时才能传播到安全规则,因为它们嵌入在用户的 ID 令牌中。

    另见:

    【讨论】:

      【解决方案2】:

      自定义声明位于request.auth.token 对象中,如documentation 中所述:

      match /companies/{document=**} {
        allow read: if request.auth != null;
        allow write: if request.auth != null && request.auth.token.role == "admin";
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-19
        • 1970-01-01
        • 1970-01-01
        • 2018-07-23
        • 1970-01-01
        • 2021-03-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多