Question about custom action (from a newbie)

Hi,

This is my first post here, so hello!

I wanted Roles populated in the JWT for my Application so I wrote a custom Action and used it in a Login Flow. It works fine. A question I have is about the Javascript. I used the code from this example in the docs, however I want to exclude the namespace so the function will look like this:

exports.onExecutePostLogin = async (event, api) => {
  if (event.authorization) {
    api.idToken.setCustomClaim('roles', event.authorization.roles);
    api.accessToken.setCustomClaim('roles', event.authorization.roles);
  }
}

For some reason this doesn’t work without the namespace being appended to roles (as in the docs example). These setters are setting values in a simple map (I assume) so why doesn’t my code without the namespace work?

Thanks

Hi there @asmith2306 welcome to the community!

This doesn’t work because roles is a reserved claim - You’ll either need to use namespace (generally recommended) or a non-reserved string.

Cheers!

Ok that makes sense. Another question to add is why roles is populated without the need for an Action/Flow in an API JWT, but not an Application JWT?

Thanks

1 Like

I assume you are referring to access tokens vs. ID tokens - The roles are automatically included in an access token when utilizing RBAC as the access token is used in authorization against an API whereas the ID token is used for authentication purposes.

Ok thanks. I’ll reading the docs, etc. Thanks for your time.

1 Like

No problem, happy to help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.