Dohvaken
Dohvaken2d ago

actions v2

before actions v2 I had the flatRoles v1 action used to add the role name to my claims. in v2 is this only possible if I run a separate endpoint for the function?
2 Replies
Ask Inkeep
Ask Inkeep2d ago
Hi @Dohvaken, I'm an AI assistant for ZITADEL. While a team member reviews this question, I'll see if I can find relevant sources for your question. Answer Unfortunately, adding custom claims to tokens is not currently possible with Actions v2 (1) . According to the available information, Actions v2 only supports adding information to API requests and responses, not to tokens or claims . For flat roles claims functionality, you would need to continue using Actions v1 (2) (3) . The Actions v1 approach allows you to add custom claims using the complement token flow:
function flatRoles(ctx, api) {
if (ctx.v1.user.grants == undefined || ctx.v1.user.grants.count == 0) {
return;
}

let grants = [];
ctx.v1.user.grants.grants.forEach(claim => {
claim.roles.forEach(role => {
grants.push(claim.projectId+':'+role)
})
})

api.v1.claims.setClaim('my:zitadel:grants', grants)
}
function flatRoles(ctx, api) {
if (ctx.v1.user.grants == undefined || ctx.v1.user.grants.count == 0) {
return;
}

let grants = [];
ctx.v1.user.grants.grants.forEach(claim => {
claim.roles.forEach(role => {
grants.push(claim.projectId+':'+role)
})
})

api.v1.claims.setClaim('my:zitadel:grants', grants)
}
This functionality remains available through Actions v1's complement token triggers for Pre Userinfo creation and Pre access token creation (2) (4) . ​ To learn more, see the sources I considered: [1] Actions V2 - Adding claims to token [2] Claims in ZITADEL [3] Code examples [4] Retrieve User Roles in ZITADEL ​ Feel free to tag @Ask Inkeep with additional questions. Got it. Tagging @Rajat for additional help.
Rajat
Rajat17h ago
hey @Dohvaken thanks for your question, I will look into it.

Did you find this page helpful?