【问题标题】:How do I set up roles in firebase auth?如何在 Firebase 身份验证中设置角色?
【发布时间】:2016-08-21 01:20:12
【问题描述】:

我目前正在使用电子邮件和密码登录。但我希望能够为每个用户分配一个角色:管理员或用户。我已经读到这是通过使用自定义身份验证方法完成的,但我发现文档不清楚使用电子邮件/密码身份验证来实现。

我该如何设置?

我目前正在为 ember 使用 firebase emberfire

更新:

文档参考:https://www.firebase.com/docs/web/guide/login/custom.html

【问题讨论】:

  • 请添加指向您引用的文档的链接?不确定是 Ember、Ember-Fire 还是 Firebase.. 谢谢!
  • @TameBadger 添加了它
  • 如何实现它取决于您。自定义安全提供程序是一种方法,但这里有一个使用任何提供程序的示例:gist.github.com/sararob/331760829a9dcb4be3e7。另请参阅这个 2.5 年前仍然非常相关的答案:stackoverflow.com/questions/19520615/…
  • 其中许多可能也是一个很好的起点:google.com/…
  • @FrankvanPuffelen 但是我将在哪里设置该数据结构?所有这些答案都感觉不完整。

标签: ember.js firebase emberfire


【解决方案1】:

Firebase 刚刚通过对 ID 令牌的自定义用户声明支持对任何用户进行基于角色的访问:https://firebase.google.com/docs/auth/admin/custom-claims

您将定义管理员访问规则:

{
  "rules": {
    "adminContent": {
      ".read": "auth.token.admin === true",
      ".write": "auth.token.admin === true",
    }
  }
}

使用 Admin SDK 设置用户角色:

// Set admin privilege on the user corresponding to uid.
admin.auth().setCustomUserClaims(uid, {admin: true}).then(() => {
  // The new custom claims will propagate to the user's ID token the
  // next time a new one is issued.
});

这将传播到相应用户的 ID 令牌声明。

要从客户端上的令牌中解析它,请检查:https://firebase.google.com/docs/auth/admin/custom-claims#access_custom_claims_on_the_client

【讨论】:

    猜你喜欢
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 2019-10-30
    • 2018-03-30
    • 1970-01-01
    相关资源
    最近更新 更多