Firebase Auth:使用CreateCustomToken设置自定义索赔

发布于 2025-01-18 14:06:44 字数 1092 浏览 1 评论 0 原文

详细信息

如果您使用函数 createCustomToken 登录用户并设置其自定义声明,则稍后使用函数 setCustomUserClaims 更新自定义声明即使在强制执行后也不会更新声明。 idToken 使用函数 firebase.auth().currentUser.getIdTokenResult(true) 刷新

如何重现?

使用 createCustomToken 函数生成的自定义令牌(包括自定义声明)在 Firebase 上登录用户

  firebase.auth().createCustomToken(uid, {myClaim: "test"}).then((customToken) => console.log(customToken))

使用自定义令牌在前端登录用户

   // copy paste the customToken manually for testing
   firebase.auth().signInWithCustomToken(customToken)

使用 setCustomUserClaims 在后端更新声明code>

   firebase.auth().setCustomUserClaims(uid, {myClaim: "updateTest"})

刷新前端的 idToken 并记录自定义声明

   firebase.auth().currentUser
       .getIdTokenResult(/*force refresh*/ true)
       .then((idTokenResult) => {
          console.log(`custom claims`, idTokenResult.claims)
       })

您应该看到声明仍然是 { myClaim: "test" } 而不是 { myClaim: "updateTest" }

Details

If you use the function createCustomToken to sign in the user and setting his custom claim, updating the custom claims later using the function setCustomUserClaims will not update the claims even after forcing the idToken to refresh using the function firebase.auth().currentUser.getIdTokenResult(true)

How to reproduce?

Sign in the user on firebase using a custom token generated with the function createCustomToken including the custom claims

  firebase.auth().createCustomToken(uid, {myClaim: "test"}).then((customToken) => console.log(customToken))

Sign in the user on the frontend using the custom token

   // copy paste the customToken manually for testing
   firebase.auth().signInWithCustomToken(customToken)

Update the claim on the backend using setCustomUserClaims

   firebase.auth().setCustomUserClaims(uid, {myClaim: "updateTest"})

Refresh the idToken on the frontEnd and log the custom claims

   firebase.auth().currentUser
       .getIdTokenResult(/*force refresh*/ true)
       .then((idTokenResult) => {
          console.log(`custom claims`, idTokenResult.claims)
       })

You should see that the claim is still { myClaim: "test" } instead of { myClaim: "updateTest" }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朦胧时间 2025-01-25 14:06:44

编辑:这实际上是一种预期的行为。使用 createCustomToken 设置的索赔具有更高的优先级。该文档在此处提到

单独设置自定义索赔,而不是使用函数 createcustomToken 设置函数 createCustomToken 设置它们将允许您以后编辑这些声明。

工作代码:

firestore
    .doc(`users/${uid}`)
    .get()
    .then((clientSnapshot) => {
      // give user the claims he has
      const { permissions = {} } = clientSnapshot.data()
     // use setCustomUserClaims to set the claims
      return auth.setCustomUserClaims(uid, { permissions })
    })
    // generate the custom token
    // ⚠️ don't use createCustomToken to set permission as you won't be able to update them
    .then(() => auth.createCustomToken(uid))
    .then((customToken) => {
      // send the custom token to the frontend to sign the user in
      return res.status(200).json({ customToken })
    })

Edit: This is actually an intended behavior. The claims set with createCustomToken have a higher priority. The doc mentions it here https://firebase.google.com/docs/auth/admin/custom-claims#set_and_validate_custom_user_claims_via_the_admin_sdk

Setting the custom claims separately at sign in instead of using the function createCustomToken to set them will allow you to edit these claims later.

Working code:

firestore
    .doc(`users/${uid}`)
    .get()
    .then((clientSnapshot) => {
      // give user the claims he has
      const { permissions = {} } = clientSnapshot.data()
     // use setCustomUserClaims to set the claims
      return auth.setCustomUserClaims(uid, { permissions })
    })
    // generate the custom token
    // ⚠️ don't use createCustomToken to set permission as you won't be able to update them
    .then(() => auth.createCustomToken(uid))
    .then((customToken) => {
      // send the custom token to the frontend to sign the user in
      return res.status(200).json({ customToken })
    })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文