- customUserClaims :对象
开发者声称要设置.如果传递null,则会删除现有的自定义声明.传递大于1000个字节的自定义声明有效负载会引发错误.自定义声明将添加到用户的ID令牌,该ID令牌将在每个经过身份验证的请求上传输.对于与配置文件无关的访问相关用户属性,请使用数据库或其他单独的存储系统.
- customUserClaims: Object
The developer claims to set. If null is passed, existing custom claims are deleted. Passing a custom claims payload larger than 1000 bytes will throw an error. Custom claims are added to the user's ID token which is transmitted on every authenticated request. For profile non-access related user attributes, use database or other separate storage systems.
根据该描述尚不清楚,但是语句如果传递了null,则删除现有的自定义声明".提示每次调用setCustomUserClaims
都会完全覆盖自定义声明.
It isn't entirely clear from this description, but the statement, "If null is passed, existing custom claims are deleted," provides a hint that the custom claims are completely overwritten with each call to setCustomUserClaims
.
因此,需要对自定义声明进行如下设置:
Therefore, custom claims need to be set as follows:
claims = {
a: 'value',
b: 'value'
}
admin.auth().setCustomUserClaims(uid, claims)
解决方法:addCustomUserClaims
可以创建一个辅助功能以合并到新的声明中.
Workaround: addCustomUserClaims
A helper function could be created to merge in new claims.
async function addCustomUserClaims(uid, claims) {
const user = await admin.auth().getUser(uid)
let updated_claims = user.customClaims || {}
for (let property in claims) {
if (Object.prototype.hasOwnProperty.call(claims, property)) {
updated_claims[property] = claims[property]
}
}
await admin.auth().setCustomUserClaims(uid, updated_claims)
}
这篇关于Firebase Admin SDK:设置/合并自定义用户声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!