gpt4 book ai didi

Firebase 自定义声明未在规则中更新

转载 作者:行者123 更新时间:2023-12-05 00:41:54 28 4
gpt4 key购买 nike

我正在努力防止存储滥用。我的目标是限制每天的用户上传操作。所以我部署了一个存储 onFinalize 触发器,一旦文件上传,该函数就会检测到使用 firestore 是否达到限制的计数器。如果是,将毫秒存储到 CustomUserClaims,如下所示:

//the user need to wait after this time to continue upload files (timeToUnlock = currentTime + additionalTime)
setCustomUserClaims(uid, {
timeToUnlock: 1570509112055
})

并在规则中比较

allow create: if request.time.toMillis() >= request.auth.token.timeToUnlock;

但是每次 setCustomUserClaims 我需要注销并再次登录客户端以更新声明或声明不会更新它只是保持以前的值。我打印 customClaims 来控制台参数已更新但似乎规则中的参数未更新。任何人有更好的想法来解决这个问题?因为如果这不起作用,我不知道要阻止这种情况,感谢您的帮助。

console.log((await admin.auth().getUser(uid)).customClaims);

最佳答案

您看到的是预期的行为。您更新声明的后端代码无法强制客户端立即查看这些声明。根据documentation :

After new claims are modified on a user via the Admin SDK, they are propagated to an authenticated user on the client side via the ID token in the following ways:

  • A user signs in or re-authenticates after the custom claims are modified. The ID token issued as a result will contain the latest claims.
  • An existing user session gets its ID token refreshed after an older token expires.
  • An ID token is force refreshed by calling currentUser.getIdToken(true).

您可以做的最好的事情是编写您的客户端,以便服务器上的更改触发客户端上的更改,以强制刷新将使用新声明的 ID token 。但这并不一定会阻止客户端使用旧 token ,直到它最终过期。

如果您想限制上传,在每次存储上传时使用 Cloud Functions 触发器会更加安全可靠,检查文件是否应该被允许,如果违反了您的规则则删除文件。

关于Firebase 自定义声明未在规则中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58280361/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com