I’m using the latest version of auth0-nextjs.
Here’s the Custom Action we use to add the claims required by our backend:
exports.onExecutePostLogin = async (event, api) => {
// Add the authenticated user's email address to the access token
const namespace = 'https://ourapp.com/';
api.accessToken.setCustomClaim(namespace + 'email', event.user.email);
if (event.request.query["org_id"] != null) {
api.accessToken.setCustomClaim(namespace + 'org_id', event.request.query["org_id"]);
}
};
My client setup is the default:
export const auth0 = new Auth0Client({
authorizationParameters: {
scope: process.env.AUTH0_SCOPE,
audience: process.env.AUTH0_AUDIENCE,
},
// Added this based on:
// https://community.auth0.com/t/custom-claims-not-showing-up-in-nextjs-session/190622/3
async beforeSessionSaved(session) {
return session;
},
});
My middleware is also pretty standard, but this is how I send the org_id
query when needed:
if (!session) {
const basePath = process.env.APP_BASE_PATH ?? '';
const loginUrl = new URL(`${basePath}/auth/login`, process.env.APP_BASE_URL);
if (orgId) {
loginUrl.searchParams.set('org_id', orgId);
}
return NextResponse.redirect(loginUrl);
}
This setup works fine initially: the first token correctly includes the org_id
claim.
The problem comes when the token expires.
When I call auth0.getAccessToken()
to get a new token using the refresh token, the new access token no longer includes the custom claim.
From what I understand, this happens because the org_id
query is not included when the SDK requests a new token from /oauth/token
.
My question: How can I pass the
org_id
(or ensure the claim is preserved) when refreshing the token using getAccessToken()
?
Thanks