# V4 NextJS SDK Does Not Return Custom Fields on User idToken

**URL:** <https://community.auth0.com/t/v4-nextjs-sdk-does-not-return-custom-fields-on-user-idtoken/182538>\
**Category:** Get Help\
**Tags:** sdks-quickstarts, nextjs-auth0\
**Created:** [February 20, 2025, 9:20pm UTC](https://community.auth0.com/t/v4-nextjs-sdk-does-not-return-custom-fields-on-user-idtoken/182538 "2025-02-20T21:20:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![relero90](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/relero90/32/72930_2.png) [@relero90](https://community.auth0.com/u/relero90)\
**Post date:** [February 20, 2025, 9:20pm UTC](https://community.auth0.com/t/v4-nextjs-sdk-does-not-return-custom-fields-on-user-idtoken/182538/1 "2025-02-20T21:20:04Z")

</div>

I am running into an issue described here: [https://community.auth0.com/t/post-login-trigger-only-firing-for-one-application-in-my-tenant-why-not-the-others/182462](https://community.auth0.com/t/post-login-trigger-only-firing-for-one-application-in-my-tenant-why-not-the-others/182462)

Basically, I have a custom action that is triggered post login for my tenant that serves to attach custom metadata to a user’s id token via `api.idToken.setCustomClaim()`. In apps using the old v3 SDK, this metadata is returned with the `user` object from `useUser` without issue. In the beta v4 SDK, this metadata is nowhere to be found.

Is there some action I need to take to ensure my metadata is attached to my idTokens in the V4 SDK or is this a bug?

---

<div class="post-metadata">

**Author:** ![remus.ivan](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/remus.ivan/32/68771_2.png) [@remus.ivan](https://community.auth0.com/u/remus.ivan)\
**Post date:** [February 21, 2025, 8:31pm UTC](https://community.auth0.com/t/v4-nextjs-sdk-does-not-return-custom-fields-on-user-idtoken/182538/3 "2025-02-21T20:31:10Z")

</div>

Hi @relero90,

Welcome back to the Auth0 Community and thank you for your post.

In the V3 version of the NextJS Auth0 SDK any claims added to the ID token were automatically propagated to the `user` object. In the **V4** version the list of default claims that persist in the `user` object does not include the `metadata`.

In order to ensure this metadata is passed to the ID token you can use the `beforeSessionSaved` hook. Please check out the [V4 Migration Guide](https://github.com/auth0/nextjs-auth0/blob/main/V4_MIGRATION_GUIDE.md) included in the NextJS SDK for further documentation.

I hope this helped.  
Thanks again,  
Remus

---

<div class="post-metadata">

**Author:** ![relero90](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/relero90/32/72930_2.png) [@relero90](https://community.auth0.com/u/relero90)\
**Post date:** [March 25, 2025, 9:46pm UTC](https://community.auth0.com/t/v4-nextjs-sdk-does-not-return-custom-fields-on-user-idtoken/182538/4 "2025-03-25T21:46:42Z")

</div>

Adding the following value for `beforeSessionSaved` in my options config option for the Auth0 client solved this problem in the v4 SDK for me:

```auto
import { Auth0Client, SessionData } from "@auth0/nextjs-auth0/server";

const createAuth0Client = async (brand: string): Promise<Auth0Client> => {
    const optionsConfig: Auth0ClientOptions = {
        authorizationParameters: {
            audience: 'unity',
        },
        domain:
            process.env[`${brand.toUpperCase()}_AUTH0_DOMAIN`] ||
            process.env.AUTH0_DOMAIN as string,
        clientId:
            process.env[`${brand.toUpperCase()}_AUTH0_CLIENT_ID`] ||
            process.env.AUTH0_CLIENT_ID as string,
        clientSecret:
            process.env[`${brand.toUpperCase()}_AUTH0_CLIENT_SECRET`] ||
            process.env.AUTH0_CLIENT_SECRET as string,
        appBaseUrl:
            process.env[`${brand.toUpperCase()}_APP_BASE_URL`] ||
            process.env.APP_BASE_URL as string,
        secret:
            process.env[`${brand.toUpperCase()}_AUTH0_SECRET`] ||
            process.env.AUTH0_SECRET as string,
        beforeSessionSaved: async (session: SessionData, idToken: string | null): Promise<SessionData> => ({ ...session }),
    };
    return new Auth0Client(optionsConfig)
}

export default createAuth0Client;
```

---

<div class="post-metadata">

**Author:** ![remus.ivan](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/remus.ivan/32/68771_2.png) [@remus.ivan](https://community.auth0.com/u/remus.ivan)\
**Post date:** [March 25, 2025, 9:50pm UTC](https://community.auth0.com/t/v4-nextjs-sdk-does-not-return-custom-fields-on-user-idtoken/182538/5 "2025-03-25T21:50:45Z")

</div>

Thanks @relero90 for sharing this with the Community!

---

<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 8, 2025, 9:50pm UTC](https://community.auth0.com/t/v4-nextjs-sdk-does-not-return-custom-fields-on-user-idtoken/182538/6 "2025-04-08T21:50:58Z")

</div>

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