Firebase 自定义声明不会传播 [英] Firebase Custom Claims doesn't propagate

查看:29
本文介绍了Firebase 自定义声明不会传播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 angularfire2 处理 Angular6 应用程序.我在用户创建中将角色设置为自定义声明,但它似乎没有传播.

I'm working in an Angular6 app with angularfire2. I'm setting the roles as custom claims in user creation, but it doesn't seem to propagate.

当我创建用户时,我将用户 ID、业务 ID 和角色发送到云函数:

When I'm creating the user I send the userid, businessid and role to a cloud function:

出价 > businessid

bid > businessid

角色 > 角色

req.body.uid > 用户 ID

req.body.uid > userid

  const customClaims = {
    roles: { [bid]: urole }
  }
  admin.auth().setCustomUserClaims(req.body.uid, customClaims)
    .then(result => {
      res
        .status(200)
        .send()
    })

问题是当对云函数的调用完成时,我想将用户重定向到需要用户设置自定义声明的路由,但它失败了.经过一些调试,我发现如果运行:

The problem is when the call to cloud function finishes and I want to redirect the user to a route which requires the user to have the custom claim set, but it fails. After some debugging, I've found out that if run:

this.angularFireAuth.auth.currentUser.getIdTokenResult(true).then(result => {
      return result.claims.roles
    })

在调用云函数result.claims.roles"之后立即未定义,但如果我刷新页面,result.claims.roles"就会有我之前设置的数据.

immediately after the call to the cloud function "result.claims.roles" is undefined, but if I refresh the page, "result.claims.roles" have the data I set before.

我已经尝试过 reload 方法和 getIdToken(true) 但我遇到了同样的问题.

I've already tried the reload method, and getIdToken(true) but I'm getting the same problem.

有没有办法避免刷新页面并获得自定义声明?

Is there a way to avoid refreshing the page and get the custom claims?

谢谢!

推荐答案

当用户登录时,他们会获得一个有效期约为一个小时的 ID 令牌.如果您设置自定义声明,他们的个人资料会立即更新,但他们的 ID 令牌不会自动更新.因此,您需要刷新他们的 ID 令牌以获取新的自定义声明.

When the user is signed in, they get an ID token that is valid for about an hour. If you set a custom claim, their profile is updated immediately, but their ID token is not auto-updated. So you'll need to refresh their ID token to get the new custom claims.

据我所知,这个 ID 令牌只有在它过期时才会通过调用 getIdTokenResult 来刷新.如果这是原因,请调用 user.reload()然后 获取 ID 令牌应该会为您提供更新的声明.

As far as I know this ID token is only refreshed by calling getIdTokenResult if it has expired. If that's the cause, calling user.reload() and then getting the ID token should give you the updated claims.

这篇关于Firebase 自定义声明不会传播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆