Auth0 Organization Custom Claim Disappears After Successful Silent Login
Last Updated:
Overview
When a post-login action sets organization information as a custom claim, the claim disappears after a successful silent login event because Auth0 does not automatically reapply custom claims during silent authentication. Store the organization ID in the user application metadata during the post-login action and retrieve it on every token issuance to resolve the issue. The organization information does not appear in the ID token, and the claim disappears after a successful silent login event, even though the initial login sets it successfully.
Applies To
- Auth0
- Post-Login Actions
- Custom Claims
- Silent Authentication
Cause
Auth0 does not automatically reapply custom claims set only in the post-login action during silent authentication (token refresh). The successful silent login event occurs when Auth0 refreshes tokens using the refresh token flow, which does not re-execute the post-login action with the same context. Administrators must persist custom claims in user metadata or application metadata to survive a token refresh, or design the action to retrieve organization data from a persistent source during every token issuance.
Solution
How do administrators persist custom claims across token refreshes?
Update the post-login action to store the organization ID in the application metadata, verify the claim namespace, and test the silent authentication in isolation by following these steps:
- Update the post-login action to store the organization ID in
app_metadata:exports.onExecutePostLogin = async (event, api) => { if (event.organization && event.organization.id) { // Store organization info in app_metadata for persistence api.user.setAppMetadata('organization_id', event.organization.id); api.user.setAppMetadata('organization_name', event.organization.name); } // Add to token using the stored metadata (works on initial login) const orgId = event.user.app_metadata?.organization_id || event.organization?.id; const orgName = event.user.app_metadata?.organization_name || event.organization?.name; if (orgId) { api.idToken.setCustomClaim( 'https://cats.dev-thepioneers.com/organizations', { pioneers_org_id: orgId, pioneers_org_name: orgName, } ); } }; - Verify the claim namespace. The claim URL
https://cats.dev-thepioneers.com/organizationsuses the correct namespace, as Auth0 requires custom claims to use a URL namespace to avoid collisions. - Decode and inspect both tokens to confirm the initial ID token contains the claim. Use a JSON Web Token (JWT) decoder to verify the claim exists immediately after login.
- Check the React application token caching. If using the Auth0 React SDK, verify that the application calls
getIdTokenClaims()after the initial login completes, rather than retrieving a cached value. The SDK may cache the decoded token, and if the application does not invalidate the cache after a silent refresh, the application may display stale claims. - Test silent authentication in isolation. Call
getTokenSilently()directly in the React application and decode the returned token to confirm whether the new token lacks the claim or whether the token contains the claim, but the application fails to read it.