Auth0 Post-Login Action Not Adding Custom Claim to ID Token in Next.js App

Hello everyone,

I failed to add the base_currency to the idToken using the post-login trigger.
Based on the output that I’m receiving from the getSession() methods from @auth0/nextjs-auth0@beta.14, the idToken is not containing the base_currency attribute.

My application is Single Page Application (SPA) and using Google OAuth 2.0 and custom database for authentication.


Below is the step that I’ve done to add the base_currency to the idToken.

I have added the base_currency to the user_metadata and app_metadata in the User Management > Users.

{
  "base_currency": "SGD"
}

I also created a custom action under the Actions > Library, which the Trigger set to Login / Post Login.
Below is the code that I added to the action.

exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://myapp.example.com';
  const { base_currency } = event.user.user_metadata;

  api.idToken.setCustomClaim(`${namespace}/base_currency`, base_currency);
};

Lastly, I added the custom action to the Trigger, post-login under the Actions > Triggers.

I login to the application and to verify the details if the base_currency is added to the idToken.

const session = await auth0.getSession();

console.log(session);

Below is the output that I’m receiving from the getSession() method.

{
  "user": {
    "given_name": "",
    "family_name": "",
    "nickname": "",
    "name": "",
    "picture": "",
    "email": "",
    "email_verified": ,
    "sub": ""
  },
  "tokenSet": {
    "accessToken": "",
    "scope": "",
    "refreshToken": "",
    "expiresAt": 1739245169
  },
  "internal": {
    "sid": "",
    "createdAt": 1739158769
  }
}

Did you decoded the token to verify the details?

Hi @vernonweehong

Welcome back to the Auth0 Community!

As Vignesh has advised, please also decode the token using jwt.io or one of their libraries.

Otherwise, you can also try to use getAccessToken() or getAccessTokenSilently() function. You can read more about it on our Github documentation. You can also try using getIdTokenClaims()as such:

const claims = await getIdTokenClaims();
console.log(claims);

Otherwise, please double check if you are setting the user_metadata correctly or if your action was saved, deployed and attached to a trigger.

If you have any other questions, feel free to let me know!

Kind Regards,
Nik

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