Custom claims in action no longer working after migrating from v3.8 to v4.8

I have some custom claims added in an action that has been working fine for some time. I am trying to migrate to the latest version of nextjs-auth0 and since having done so, I am no longer receiving the custom claims that are in the action. It still works if using the v3 version of nextjs-auth0.

I’ve looked through the documentation to make sure I have everything correct and I did notice that the more recent docs have you namespace the claims, which I have tried but that has not worked. The /profile endpoint and the user object from useUser() are only returning the following properties:

{
mail: "email_address"
email_verified: true
name: "name"
nickname: "nickname"
org_id: "org_"
picture: "picture_url"
sub: ""
}

However, if I use the access token and then call the /userinfo endpoint from auth0 I do get all the additional claims that I have defined. I don’t know why its just not working with the v4 client, am I missing something in the configuration of the client?

Here is an example of one of the claims I am trying to add

exports.onExecutePostLogin = async (event, api) => {
    const namespace = "https://myapp.example.com/";

    const organization = event.organization?.name;

    api.idToken.setCustomClaim(namespace + "org_name", organization);
};

Ok, I just figured this out and putting the fix here for anyone else that had the same issue I did with the nextjs library. You have to configure the client to save the additional claims to the session, otherwise they will be filtered out since they are not part of the default claims.

If you add this it will return all the claims, including your custom ones. You just need to adjust this to only return the ones you need but this should help you get there.

import { Auth0Client } from "@auth0/nextjs-auth0/server";

export const auth0 = new Auth0Client({
  async beforeSessionSaved(session, idToken) {
    return session;
  }
});
1 Like

I’ve been going around and around in circles for hours, trying to fix this same issue. How did you figure it out?!

Found it on the v4 migration guide, in the id token claims section. Searching the issue kept bringing up issues related to the action but everything I tried that was suggested didn’t work. I knew it had to be related to a v4 change so just dug through the guide and found that bit and that worked.