firebase安全规则使用customClaims

huangapple go评论47阅读模式
英文:

firebase security rule using customClaims

问题

目标是只允许已在 Firebase 中进行身份验证且具有自定义声明 "role" 为 "subscriber" 的用户访问存储。

但是出现了以下错误:

错误:simulator.rules 第 [6] 行,第 [9] 列。对象上未定义属性 customClaims。

感谢任何帮助,用户是 Google 身份验证的用户,每个用户都被分配了自定义角色。

英文:

The objective is to restrict storage access only to user who are authenticated in firebase and has a customClaims of role subscriber.

service firebase.storage {
  match /b/{bucket}/o {
    // Allow read access only for authenticated users with the role of "subscriber"
    match /{allPaths=**} {
      allow read: if request.auth != null && (
        request.auth.customClaims.role == "subscriber"
      );
    }
  }
}

but getting the error:

> Error: simulator.rules line [6], column [9]. Property customClaims is undefined on object.

any help is appreciated the users are the google authenticated users and each has a customs role assigned.

答案1

得分: 2

自定义声明可在request.auth.token变量中使用,因此:

request.auth.token.role == "subscriber"

如果今后遇到类似的问题,我建议查看这些关于存储安全规则中自定义声明的问题:存储安全规则中自定义声明的问题

英文:

Custom claims are available in the request.auth.token variable, so:

request.auth.token.role == "subscriber"

If you run into similar problems going forward, I recommend checking these questions about custom claims in Storage security rules/

huangapple
  • 本文由 发表于 2023年6月18日 18:07:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76499993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定