NextJS useUser Not Getting App Metadata

I am trying to retrieve my app_metadata through the useUser nextJS hook but it keeps failing.

I followed this answer to setup a rule to return the metadata but still do not get the info when I call useUser.

I am very stuck and have tried setting up rules/claims to do this but have made no progress.

Hi @ryanwhite,

Welcome to the Auth0 Community!

How is it failing? Can you try to decode the token with jwt.io and see if the claim you are expecting is there?

Hello and thank you @dan.woda

So maybe I am misunderstanding how I should be retrieving the info but when I call useUser in nextjs it returns the user info but no metadata.

This is my rule

function(user, context, callback) {
  const namespace = 'https://myapp.example.com/';
  context.idToken[namespace + 'stripe_id'] = user.app_metadata.stripe_id;
	callback(null, user, context);
}

Then this is what useUser returns

email: "EMAIL_HERE"
email_verified: false
name: "EMAIL_HERE"
nickname: "hello"
picture: "GRAVATAR_HERE"
sub: "auth0|AUTH0_ID"
updated_at: "2021-07-21T21:47:30.606Z"

I have set in the auth0 dashboard the app_metadata of
{
“stripe_id”: “test-value”
}

Oh man I feel dumb. I logged out of my app and back in and it started working. How would I be able to force the retrieval of this data without forcing users to log in/out again @dan.woda?

@ryanwhite,

It must be that the initial token is getting issued before the stripe ID is set in the user’s metadata. How are you setting that value in the user’s app_metadata?

I have switched to setting it in my app with the management API and it seems to be working okay. I will keep testing but I think I have got it figure out. Thanks for the help!

Glad you found a solution!

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