# Add Roles/Claims to the access token via Login Flow

**URL:** <https://community.auth0.com/t/add-roles-claims-to-the-access-token-via-login-flow/131932>\
**Category:** Get Help\
**Tags:** login-experience, new-universal-login-experience\
**Created:** [April 13, 2024, 12:57pm UTC](https://community.auth0.com/t/add-roles-claims-to-the-access-token-via-login-flow/131932 "2024-04-13T12:57:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![max12234](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/max12234/32/56391_2.png) [@max12234](https://community.auth0.com/u/max12234)\
**Post date:** [April 13, 2024, 12:57pm UTC](https://community.auth0.com/t/add-roles-claims-to-the-access-token-via-login-flow/131932/1 "2024-04-13T12:57:23Z")

</div>

I want to use roles on auth0.  
But the roles are not automatically in the token.

I tried:

> exports.onExecutePostLogin = async (event, api) =\> {  
> exports.onExecutePostLogin = async (event, api) =\> {  
> if (event.user.email && event.user.email.endsWith(“@xxx.com”) && event.client.name === “My SPA”) {  
> api.access.deny(`Access to ${event.client.name} is not allowed.`);  
> }  
> };  
> // Define a namespace (URL you control) to avoid conflicts with standard OIDC claims  
> const namespace = ‘[yyy.eu.auth0.com/claims/](http://yyy.eu.auth0.com/claims/)’;
> 
> // Log the incoming user metadata and app\_metadata  
> console.log(‘User metadata:’, event.user.user\_metadata);  
> console.log(‘App metadata:’, event.user.app\_metadata);
> 
> // Retrieve roles from user’s app\_metadata, or assign a default role if none exist  
> const roles = (event.user.app\_metadata && event.user.app\_metadata.roles)  
> ? event.user.app\_metadata.roles  
> : [‘default\_role’];
> 
> // Log the roles being set to the token  
> console.log(‘Roles being set to the token:’, roles);
> 
> // Set custom claims in both the ID and access tokens  
> api.idToken.setCustomClaim(`${namespace}roles`, roles);  
> api.accessToken.setCustomClaim(`${namespace}roles`, roles);
> 
> // Confirm the claims have been set in the logs  
> console.log(‘Custom claims set in ID Token:’, `${namespace}roles`, roles);  
> console.log(‘Custom claims set in Access Token:’, `${namespace}roles`, roles);  
> api.user.setUserMetadata(“favorite\_color”, “blue”);  
> if (event.authorization) {  
> api.idToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);  
> api.accessToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);  
> }
> 
> const namespace2 = ‘[yourdomain.com/claims](http://yourdomain.com/claims)’;
> 
> const roles2 = event.user.app\_metadata && event.user.app\_metadata.roles ?  
> event.user.app\_metadata.roles : [‘default\_role’];
> 
> api.idToken.setCustomClaim(`${namespace}/roles`, roles2);  
> api.accessToken.setCustomClaim(`${namespace}/roles`, roles);  
> };

But it does not add it to the token too.  
Here are my Action Details =\>

> {  
> “action\_name”: “Add Roles”,  
> “response”: {  
> “logs”: “User metadata: { favorite\_color: ‘blue’, test: ‘test’ }\nApp metadata: {}\nRoles being set to the token: [‘default\_role’]\nCustom claims set in ID Token: /yyy.eu.auth0.com/claims/roles [‘default\_role’]\nCustom claims set in Access Token: https:/”,  
> “stats”: {  
> “total\_request\_duration\_ms”: 375,  
> “total\_runtime\_execution\_duration\_ms”: 372,  
> “runtime\_processing\_duration\_ms”: 4,  
> “action\_duration\_ms”: 312,  
> “runtime\_external\_call\_duration\_ms”: 56,  
> “boot\_duration\_ms”: 60,  
> “network\_duration\_ms”: 3  
> }  
> },  
> “started\_at”: “2024-04-13T12:49:45.453485604Z”,  
> “ended\_at”: “2024-04-13T12:49:45.829570334Z”  
> }

So it clearly logs. I also banned my user from login =\> it was not anymore possible to login

What do I wrong?

On angular side I use:

> provideAuth0({  
> domain: ‘[yyy.eu.auth0.com](http://yyy.eu.auth0.com)’,  
> clientId: ‘333’,  
> authorizationParams: {  
> redirect\_uri: window.location.origin,  
> audience: “api”,  
> scope: ‘openid profile email’,  
> }  
> })

I use =\> this.auth.getAccessTokenSilently(). and =\> this.auth.user$

Questions:

- How can I fix it?
- Is it a good idea to add roles to the token (Keycloak used it =\> wich we used before)
- Is my scope correct?
- Do I need for my spring boot application the same “Auth0 Application” as for my Angular app ? ( I use at the moment 2 apps: “Single Page Application” and “Regular Web Application”)

---

<div class="post-metadata">

**Author:** ![marcelina.barycka](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marcelina.barycka/32/56162_2.png) [@marcelina.barycka](https://community.auth0.com/u/marcelina.barycka)\
**Post date:** [April 15, 2024, 11:38am UTC](https://community.auth0.com/t/add-roles-claims-to-the-access-token-via-login-flow/131932/3 "2024-04-15T11:38:20Z")

</div>

Hey there @max12234 !

Thanks for the context and your script content!  
I believe, in your Action script, you are missing apostrophes for the `setCustomClaim` arguments:

```auto
api.idToken.setCustomClaim(${namespace}/roles, roles2);
api.accessToken.setCustomClaim(${namespace}/roles, roles);

```

Can you please try this way (in every relevant place)? 🙂

```auto
api.idToken.setCustomClaim('${namespace}/roles', roles2);
api.accessToken.setCustomClaim('${namespace}/roles', roles);

```

It works for me this way, so please let us know if that fixes your issue!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [April 29, 2024, 11:38am UTC](https://community.auth0.com/t/add-roles-claims-to-the-access-token-via-login-flow/131932/5 "2024-04-29T11:38:28Z")

</div>

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