Auth0 Organization Custom Claim Disappears After Successful Silent Login

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:

  1. 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,
    }
    );
    }
    };
  2. Verify the claim namespace. The claim URL https://cats.dev-thepioneers.com/organizations uses the correct namespace, as Auth0 requires custom claims to use a URL namespace to avoid collisions.
  3. 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.
  4. 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.
  5. 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.

 

Recommended content

No recommended content found...