# Custom Claims added to accessToken not being returned from /userinfo

**URL:** <https://community.auth0.com/t/custom-claims-added-to-accesstoken-not-being-returned-from-userinfo/102221>\
**Category:** Get Help\
**Tags:** extensibility, actions-extensibility\
**Created:** [March 10, 2023, 2:07am UTC](https://community.auth0.com/t/custom-claims-added-to-accesstoken-not-being-returned-from-userinfo/102221 "2023-03-10T02:07:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexandra1](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alexandra1/32/45307_2.png) [@alexandra1](https://community.auth0.com/u/alexandra1)\
**Post date:** [March 10, 2023, 2:07am UTC](https://community.auth0.com/t/custom-claims-added-to-accesstoken-not-being-returned-from-userinfo/102221/1 "2023-03-10T02:07:45Z")

</div>

Hello! I’m trying to follow the directions here to add custom claims to access tokens:

> **[Create Custom Claims](https://auth0.com/docs/secure/tokens/json-web-tokens/create-custom-claims)**
>
> Learn how to create collision-resistant names for custom claims.

I’ve created an action that looks like this and added it to the login flow, and seems to work fine when I run it in the editor:

```auto
exports.onExecutePostLogin = async (event, api) => {
  api.accessToken.setCustomClaim('https://redacted/app_metadata', event.user.app_metadata);
};

```

However, when I call the /userinfo endpoint with a logged in user’s access token, like so:

```auto
const resp = await fetch('https://redacted.auth0.com/userinfo', {
      method: 'GET',
      headers: {
        Authorization: `Bearer ${session?.accessToken}`,
      },
    });

```

The response contains user info but not the app\_metadata I tried to add via the action above.

Is there a step I’m missing? Or a better approach to accessing the app\_metadata for a user? For context, I’m trying to access it in the getServerSideProps of a nextjs application.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [March 10, 2023, 11:04pm UTC](https://community.auth0.com/t/custom-claims-added-to-accesstoken-not-being-returned-from-userinfo/102221/3 "2023-03-10T23:04:53Z")

</div>

Hi @alexandra1,

Welcome to the Auth0 Community!

I understand that you have added custom claims to an access token but were not able to see them in the token.

After looking closely at the code snippet you shared, I noticed that you are calling the `/userinfo` endpoint with the token you obtained from the login flow. In this situation, the `audience` parameter was never passed in the login request which results in opaque access tokens.

These tokens do not contain custom claims.

To get the custom claims, you must specify an API identifier in the login request as your `audience` query parameter.

For example:

```auto
https://{yourDomain}/authorize?
    response_type=code&
    client_id={yourClientId}&
    redirect_uri={https://yourApp/callback}&
    scope={scope}&
    audience={apiAudience}&
    state={state}

```

After the login flow is complete, the access token returned can be decoded in [jwt.io](http://jwt.io) to see the custom claims.

I hope the explanation was clear! Here are some helpful resources worth checking out:

- [Get Access Tokens](https://auth0.com/docs/secure/tokens/access-tokens/get-access-tokens#control-access-token-audience)
- [Why is my access token not a JWT? (Opaque Token)](https://community.auth0.com/t/why-is-my-access-token-not-a-jwt-opaque-token/31028)

Please reach out if you have questions or need further clarification.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![alexandra1](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alexandra1/32/45307_2.png) [@alexandra1](https://community.auth0.com/u/alexandra1)\
**Post date:** [March 11, 2023, 12:46am UTC](https://community.auth0.com/t/custom-claims-added-to-accesstoken-not-being-returned-from-userinfo/102221/4 "2023-03-11T00:46:15Z")

</div>

I see, thanks for the speedy reply! From the docs it seems like the audience would be for an api we’ve built but that the audience for userinfo always results in an opaque token.

If we don’t have a custom api I’m trying to access and I just want to access the app\_metadata for a user, is it better to add it to the idToken since that’s already in JWT format (i.e. once we have the token we can access it without any additional api calls)?

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [March 11, 2023, 1:10am UTC](https://community.auth0.com/t/custom-claims-added-to-accesstoken-not-being-returned-from-userinfo/102221/5 "2023-03-11T01:10:02Z")

</div>

Hi @alexandra1

Thank you for your response.

> [@alexandra1](#):
>
> If we don’t have a custom api I’m trying to access and I just want to access the app\_metadata for a user, is it better to add it to the idToken since that’s already in JWT format (i.e. once we have the token we can access it without any additional api calls)?

Yes, that is correct. You could workaround this by appending the custom claims to the ID Token rather than the access token.

As a reminder, you must include the `openid` scope in the login request to get an ID Token.

May I help you with anything else?

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![alexandra1](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alexandra1/32/45307_2.png) [@alexandra1](https://community.auth0.com/u/alexandra1)\
**Post date:** [March 11, 2023, 1:15am UTC](https://community.auth0.com/t/custom-claims-added-to-accesstoken-not-being-returned-from-userinfo/102221/6 "2023-03-11T01:15:54Z")

</div>

Nope that should work for me! Thanks so much!

---

<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:** [March 25, 2023, 1:16am UTC](https://community.auth0.com/t/custom-claims-added-to-accesstoken-not-being-returned-from-userinfo/102221/7 "2023-03-25T01:16:44Z")

</div>

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