# Accessing App\_Metadata In Next.js Auth0 App

**URL:** <https://community.auth0.com/t/accessing-app-metadata-in-next-js-auth0-app/136941>\
**Category:** Get Help\
**Tags:** apis, application\
**Created:** [June 24, 2024, 5:17am UTC](https://community.auth0.com/t/accessing-app-metadata-in-next-js-auth0-app/136941 "2024-06-24T05:17:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![secomm](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/secomm/32/48943_2.png) [@secomm](https://community.auth0.com/u/secomm)\
**Post date:** [June 24, 2024, 5:17am UTC](https://community.auth0.com/t/accessing-app-metadata-in-next-js-auth0-app/136941/1 "2024-06-24T05:17:16Z")

</div>

I have a Next.js app where I need to access the user’s app\_metadata and retrieve a certain object from the JSOn (planType to be exact) and I haven’t found how retrieve it within the Next.js application.

Right now, I am trying to render it in a getServerSideProps in a dynamically rendered page on my site.

The code is able to successfully get the user from the auth0 nextjs client import for a different use,

But I am unsure how to access the the app\_metadata through this (it can access things like user.name, user.email, user.username, and other user data but not app\_metadata).

Any idea how to do this if it is possible?

```auto
export const getServerSideProps = async (context: any) => {
  const session = await getSession(context.req, context.res);
  const user = session?.user;
  const { query } = context.query;

  const rawData = await fetchXXXXXXXXX(user, query); // uses user to fetch user.name for different task but this works
  const XXXXXXXX = ..... // not important to this problem
    return rest;
  });
console.log(user) // successful.

  const [APP METADATA VAR] = user?.app_metadata.planType == "paid" //returning an error as this is undefined - this where I am facing problems

  return {
    props: {
      XXXXXXXX,
      [APP METADATA VAR],
    }
  };
};

```

when I log the user data i get this as well - only the basic user data but not the app\_metadata.

```auto
{
  nickname: 'XXXXXXXXXXXXXXXX',
  name: 'XXXXXXXXXXXXXXXXXXXXXX',      
  updated_at: '2024-06-24T04:45:31.227Z',
  email: 'XXXXXXXXXXXXXXXXXXXXXX',
  email_verified: true,
  sub: 'XXXXXXXXXXXXXXX',
  sid: 'XXXXXXXXXXXXXXXXXXXXXXXX'
}

```

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [June 24, 2024, 5:11pm UTC](https://community.auth0.com/t/accessing-app-metadata-in-next-js-auth0-app/136941/3 "2024-06-24T17:11:18Z")

</div>

Hi there @secomm !

> [@secomm](#):
>
> But I am unsure how to access the the app\_metadata through this (it can access things like user.name, user.email, user.username, and other user data but not app\_metadata).
> 
> Any idea how to do this if it is possible?

`app_metadata` and `user_metadata` aren’t added as claims to user tokens by default - You’ll want to add metadata as custom claims in an [Action](https://auth0.com/docs/customize/actions). For example:

```auto
exports.onExecutePostLogin = async (event, api) => {
  // Ensure user_metadata and app_metadata exist
  const userMetadata = event.user.user_metadata || {};
  const appMetadata = event.user.app_metadata || {};

  // Add user_metadata to ID Token
  api.idToken.setCustomClaim('https://example.com/user_metadata', userMetadata);

  // Add app_metadata to ID Token
  api.idToken.setCustomClaim('https://example.com/app_metadata', appMetadata);

  // Add user_metadata to Access Token
  api.accessToken.setCustomClaim('https://example.com/user_metadata', userMetadata);

  // Add app_metadata to Access Token
  api.accessToken.setCustomClaim('https://example.com/app_metadata', appMetadata);
};

```

After configuring this post login action, the metadata should be included in the user data you’re seeing.

---

<div class="post-metadata">

**Author:** ![secomm](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/secomm/32/48943_2.png) [@secomm](https://community.auth0.com/u/secomm)\
**Post date:** [June 28, 2024, 2:04am UTC](https://community.auth0.com/t/accessing-app-metadata-in-next-js-auth0-app/136941/4 "2024-06-28T02:04:13Z")

</div>

Hi,  
Thanks for the help.

If I implement this code after the creation of each user, will I be able to access the app\_metadata regularly by using

```auto
user.app_metadata.planType

```

assuming planType is under app\_metadata?

And how will I get this to work under development - will I have to add this to my dev Auth0 Action and add the token to the localhost:3000 route?

---

<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:** [July 18, 2024, 7:48pm UTC](https://community.auth0.com/t/accessing-app-metadata-in-next-js-auth0-app/136941/5 "2024-07-18T19:48:44Z")

</div>

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