Firebase Custom Claims doesn't propagate

限于喜欢 提交于 2019-11-27 08:36:25

问题


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.

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

bid > businessid

urole > role

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
    })

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.

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?

Thank you!


回答1:


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.

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.



来源:https://stackoverflow.com/questions/52729144/firebase-custom-claims-doesnt-propagate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!