Skip to content

Export filterDefaultIdTokenClaims and update beforeSessionSaved docs #2119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

frederikprijck
Copy link
Member

📋 Changes

We have the following code here:

if (this.beforeSessionSaved) {
    const updatedSession = await this.beforeSessionSaved(
      session,
      oidcRes.id_token ?? null
    );
    session = {
      ...updatedSession,
      internal: session.internal
    };
  } else {
    session.user = filterClaims(idTokenClaims);
 }

What we can see here is that if the user does not pass a beforeSessionSaved handler, the session.user is set to a filtered list of claims to only contain the default tokens.

However, when a beforeSessionSaved handler is passed, the claims aren't filtered, and the session.user object passed to this.beforeSessionSaved has a non-filtered list of claims.

This means, that the following code would include all claims:

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

However, this isn't clear from the docs. Additionally, if a user wants to only include a single claim, they currently need to ensure they provide all the default claims manually:

export const auth0 = new Auth0Client({
  async beforeSessionSaved(session, idToken) {
    return {
      ...session,
      user: {
        sub: session.user.sub,
        name: session.user.name,
        nickname: session.user.nickname,
        given_name: session.user.given_name,
        family_name: session.user.family_name,
        picture: session.user.picture,
        email: session.user.email,
        email_verified: session.user.email_verified,
        org_id: session.user.org_id,
        foo: session.user.foo, // add the foo claim
      },
    }
  },
})

This is way to complicated and not the kind of experience we want. Therefore this PR exports the filterClaims (and renames it to filterDefaultIdTokenClaims, so it can be used in custom beforeSessionSaved handlers accordingly.

📎 References

🎯 Testing

@frederikprijck frederikprijck requested a review from a team as a code owner May 26, 2025 11:34
@codecov-commenter
Copy link

codecov-commenter commented May 26, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.48%. Comparing base (7bd8398) to head (5dc224c).
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2119      +/-   ##
==========================================
+ Coverage   82.46%   82.48%   +0.02%     
==========================================
  Files          21       21              
  Lines        2024     2027       +3     
  Branches      355      356       +1     
==========================================
+ Hits         1669     1672       +3     
  Misses        348      348              
  Partials        7        7              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

@deepu105 deepu105 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactor fixes

@frederikprijck frederikprijck force-pushed the chore/filter-claims-in-before-session-saved branch from 43b58eb to 5dc224c Compare May 26, 2025 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants