# Metadata not showing up in useUser() and getSession()

**URL:** <https://community.auth0.com/t/metadata-not-showing-up-in-useuser-and-getsession/139530>\
**Category:** Get Help\
**Tags:** user-profile, user-management\
**Created:** [July 23, 2024, 4:40pm UTC](https://community.auth0.com/t/metadata-not-showing-up-in-useuser-and-getsession/139530 "2024-07-23T16:40:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dev63](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dev63/32/49359_2.png) [@dev63](https://community.auth0.com/u/dev63)\
**Post date:** [July 23, 2024, 4:40pm UTC](https://community.auth0.com/t/metadata-not-showing-up-in-useuser-and-getsession/139530/1 "2024-07-23T16:40:19Z")

</div>

Ready to post? 🔍 First, try searching for your answer.  
Hey guys,

Using nextjs and auth0 js sdk’s.

We’ve successfully added app\_metadata to our user using the management sdk here:

```auto
type Auth0UserMetadata = {
  organizationId?: string | null;
  role?: string | null;
};

const management = new ManagementClient({
  domain: env.AUTH0_DOMAIN_AND_REGION,
  clientId: env.AUTH0_CLIENT_ID,
  clientSecret: env.AUTH0_CLIENT_SECRET,
});

async function updateAuth0AppMetadata(userId: string, metadata: Auth0UserMetadata) {
  try {
    // const user = await management.updateAppMetadata({ id: userId }, metadata);
    const testGetUser = await management.users.get({ id: userId });
    console.log("testGetUser", testGetUser);
    const user = await management.users.update({ id: userId }, { app_metadata: metadata });

    

    console.log('User updated:', user);
    return user;
  } catch (error) {
    console.error("Error updating user in Auth0", error);
    throw error;
  }
}

```

For some reason, when we call getSession(), we do not see the app\_metadata in the session.user object. How do we get this information to show up in all calls to getSession() and useUser()?

---

<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:** [July 23, 2024, 5:49pm UTC](https://community.auth0.com/t/metadata-not-showing-up-in-useuser-and-getsession/139530/3 "2024-07-23T17:49:20Z")

</div>

Hi @dev63,

Could you clarify if you are updating the user during an active user session? If so, you might need the user to reauthenticate or use silent authentication to see the updated data.

Additionally, you might need to create a post-login action script to append the app\_metadata to either the access or ID token and then decode it to read that information.

Let me know if you have any questions.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![dev63](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dev63/32/49359_2.png) [@dev63](https://community.auth0.com/u/dev63)\
**Post date:** [July 23, 2024, 8:21pm UTC](https://community.auth0.com/t/metadata-not-showing-up-in-useuser-and-getsession/139530/4 "2024-07-23T20:21:02Z")

</div>

Hi Reuben,

Adding the post-login script worked mostly. Yes, we are trying to update an active session. user gets logged in, and then we update their metadata. Then, in other parts of the app, for example trpc context, getSession() is called to grab information. After handleCallback is called, we get or create a user in our db, and then we set the app\_metadata to store further ids for the user, for example, RBAC settings.

```auto
export const GET = handleAuth({

  callback: async (req: NextRequest, ctx: { params: Record<string, string | string[]> }) => {
    try {
      const res = await handleCallback(req, ctx);
      console.log(">>> res in callback", res);

      const session = await getSession(req, res);
      if (!session) {
        console.error("No session found");
        return NextResponse.redirect(`${process.env.AUTH0_BASE_URL}/error`);
      }
      console.log(">>> session in callback", session.user.sub);

      const updatedSession = await handleGetOrCreateUser(session);
      console.log(">>> updated session after handling our ■■■■", updatedSession.user.sub);

      if (!updatedSession) {
        console.error("Session update failed");
        return NextResponse.redirect(`${process.env.AUTH0_BASE_URL}/error`);
      }

      let returnTo = `${process.env.AUTH0_BASE_URL}/dashboard/home`;

      // Create a new response with the redirect
      const redirectResponse = NextResponse.redirect(returnTo);

      // Copy all cookies from the original response to the new response
      res.headers.getSetCookie().forEach(cookie => {
        redirectResponse.headers.append('Set-Cookie', cookie);
      });

      // Add a flag to indicate that this is a post-login redirect
      redirectResponse.headers.set('X-Auth-Return-Redirect', '1');

      return redirectResponse;
    } catch (error) {
      console.error("Error in callback handler:", error);
      return NextResponse.redirect(`${process.env.AUTH0_BASE_URL}/error`);
    }
  },
});

```

```auto
exports.onExecutePostLogin = async (event, api) => {
  const userAppMetadata = event.user.app_metadata;
  api.accessToken.setCustomClaim("app_metadata", userAppMetadata)
  api.idToken.setCustomClaim("app_metadata", 
};

```

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![dev63](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dev63/32/49359_2.png) [@dev63](https://community.auth0.com/u/dev63)\
**Post date:** [July 23, 2024, 9:37pm UTC](https://community.auth0.com/t/metadata-not-showing-up-in-useuser-and-getsession/139530/5 "2024-07-23T21:37:42Z")

</div>

Can you elaborate on silent authentication?

---

<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:** [August 28, 2024, 7:10pm UTC](https://community.auth0.com/t/metadata-not-showing-up-in-useuser-and-getsession/139530/6 "2024-08-28T19:10:19Z")

</div>

Hi @dev63,

Thanks for the responses.

Sure! Silent authentication is a process that re-authenticates the user without user interaction as long as there is an active session. To do so, you would include the `prompt=none` as an authorization parameter in your login request.

I recommend referring to our [Configure Silent Authentication](https://auth0.com/docs/authenticate/login/configure-silent-authentication) documentation for more information.

Thanks,  
Rueben

---

<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:** [September 11, 2024, 7:10pm UTC](https://community.auth0.com/t/metadata-not-showing-up-in-useuser-and-getsession/139530/7 "2024-09-11T19:10:38Z")

</div>

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