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

查看:42
本文介绍了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.

创建用户时,我会将userid,businessid和角色发送到云函数:

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

bid> businessid

bid > businessid

urole>角色

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来刷新该ID令牌.如果是原因,请调用 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天全站免登陆