所以,我有一个用例,一个用户可能需要多个自定义索赔,但添加在应用程序的不同点,即一个用户可以同时是一个商店管理员和店主,或者他们可能只是一个商店管理员或只是一个用户。
我从文档中了解到,当你分配一个新的自定义索赔时,它将覆盖原有的索赔,因此我需要读取用户当前拥有的索赔。 这就是我被卡住的地方......当我收到索赔后,我如何将它们重新写回给用户?
exports.onGameManagement = functions.database.ref("Core/CoreGames/{game}/teams/committee").onCreate((snapshot,context)=>{
const position = snapshot.key;
const uid = snapshot.val();
const game = context.params.game;
if(position=="gLead"||position=="depGameLead"){
//NEEDS ADMIN RIGHTS
//ADD TOKEN WRITE RIGHTS. - check for current claims
admin.auth().getUser(uid).then((userRecord)=>{
//got the users claims
console.log(userRecord.customClaims);
//how do i add this array of claims to the setCustomUserClaims() method?
admin.auth().setCustomUserClaims(uid, {gameAdmin: true, userRecord.customClaims}).then(() => {
// The new custom claims will propagate to the user's ID token the
// next time a new one is issued.
});
})
}else{
}
})
我怀疑这是个很简单的解决方法,但我似乎在任何地方都找不到如何处理在不同时间添加多个索赔的例子......可以说是堆叠。预先感谢您的帮助。
你已经很接近了:你可以使用传播操作符(...
)来将现有的索赔和新的索赔添加到一个单一的对象中。
admin.auth().getUser(uid).then((userRecord)=>{
admin.auth().setCustomUserClaims(uid, {gameAdmin: true, ...userRecord.customClaims});
})
或者,您也可以简单地从用户记录中提取索赔对象,并在将其传回到 setCustomUserClaims
:
admin.auth().getUser(uid).then((userRecord)=>{
let claims = userRecord.customClaims;
claims.gameAdmin = true;
admin.auth().setCustomUserClaims(uid, claims);
})
要删除一项索赔,应将以下内容替换为: claims.gameAdmin = true;
与 delete claims.gameAdmin;
在前面那段话中。