# User Metadata in useUser - Next.js

**URL:** <https://community.auth0.com/t/user-metadata-in-useuser-next-js/74698>\
**Category:** Get Help\
**Tags:** user\_metadata, nextjs\
**Created:** [December 13, 2021, 7:07am UTC](https://community.auth0.com/t/user-metadata-in-useuser-next-js/74698 "2021-12-13T07:07:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandro](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sandro/32/33507_2.png) [@sandro](https://community.auth0.com/u/sandro)\
**Post date:** [December 13, 2021, 7:07am UTC](https://community.auth0.com/t/user-metadata-in-useuser-next-js/74698/1 "2021-12-13T07:07:17Z")

</div>

Please include the following information in your post:

- **Which SDK this is regarding:** @auth0/nextjs-auth0
- **SDK Version:** 1.6.1
- **Platform Version:** node v14.18.1
- **Code Snippets/Error Messages/Supporting Details/Screenshots:**

Hi,

I try to fetch `user_metadata`.

I did two things so far:

**Created Action**

```plaintext
exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://<AUTH0-DOMAIN>.com';
  api.idToken.setCustomClaim(`${namespace}/user_metadata`, event.user.user_metadata);
};

```

**Changed API Route**

I’ve changed the API route in `pages/api/[...auth0].js`

```plaintext
import { handleAuth, handleCallback } from "@auth0/nextjs-auth0";

const afterCallback = async (req, res, session, state) => {
    console.log(session);
    return session;
}

export default handleAuth({
    async callback(req, res) {
        try {
            await handleCallback(req, res, { afterCallback });
        } catch (error) {
            res.status(error.status || 500).end(error.message);
        }
    }
});

```

If I understand it correctly I should see the `user_metadata` now in the `session` object but I don’t see it.

In the end, I’d like to fetch it from the frontend via the `useUser` hook.

Thanks for your help!

---

<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:** [December 13, 2021, 5:00pm UTC](https://community.auth0.com/t/user-metadata-in-useuser-next-js/74698/3 "2021-12-13T17:00:36Z")

</div>

Hi @sandro,

Welcome to the Auth0 Community!

I understand you’ve encountered issues retrieving the user’s _user\_metadata_ in your Next.js app.

First, I noticed that your Post-Login Action script uses a reserved namespace, specifically your Auth0 domain. Unfortunately, this is not allowed as described in our [Create Custom Claims](https://auth0.com/docs/security/tokens/json-web-tokens/create-namespaced-custom-claims) documentation.

Because of this, you’ll need to use any non-Auth0 HTTP or HTTPS URL as your namespace identifier:

```auto
exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://my-app.example.com';
  if (event.authorization) {
    api.idToken.setCustomClaim(`${namespace}/user_metadata`, event.user.user_metadata);
  }
};

```

Once that is addressed, you’ll be able to see the _user\_metadata_.

Lastly, you may find the [Add claims to a token example with Actions](https://auth0.com/docs/actions/triggers/post-login#add-user-roles-to-id-and-access-tokens) helpful.

Please let me know if there’s anything else I can do to help.

Thank you.

---

<div class="post-metadata">

**Author:** ![sandro](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sandro/32/33507_2.png) [@sandro](https://community.auth0.com/u/sandro)\
**Post date:** [December 14, 2021, 6:10am UTC](https://community.auth0.com/t/user-metadata-in-useuser-next-js/74698/4 "2021-12-14T06:10:40Z")

</div>

Thank you so much that works!

---

<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:** [December 14, 2021, 4:58pm UTC](https://community.auth0.com/t/user-metadata-in-useuser-next-js/74698/5 "2021-12-14T16:58:40Z")

</div>

Hi @sandro,

I’m glad that everything is working now!

If you have any further questions, please don’t hesitate to reach out.

Have a great rest of your day!

Thank you.

---

<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:** [December 29, 2021, 4:58pm UTC](https://community.auth0.com/t/user-metadata-in-useuser-next-js/74698/6 "2021-12-29T16:58:48Z")

</div>

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