Skip to content

Commit ec89fc1

Browse files
authored
Export filterDefaultIdTokenClaims and update beforeSessionSaved docs (#2119)
2 parents 0bc4774 + 37df4ce commit ec89fc1

File tree

5 files changed

+45
-11
lines changed

5 files changed

+45
-11
lines changed

EXAMPLES.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,19 +621,43 @@ The `beforeSessionSaved` hook is run right before the session is persisted. It p
621621
The hook recieves a `SessionData` object and an ID token. The function must return a Promise that resolves to a `SessionData` object: `(session: SessionData) => Promise<SessionData>`. For example:
622622

623623
```ts
624+
import { Auth0Client, filterDefaultIdTokenClaims } from "@auth0/nextjs-auth0/server"
625+
624626
export const auth0 = new Auth0Client({
625627
async beforeSessionSaved(session, idToken) {
626628
return {
627629
...session,
628630
user: {
629-
...session.user,
630-
foo: "bar",
631+
...filterDefaultIdTokenClaims(session.user),
632+
foo: session.user.foo, // keep the foo claim
631633
},
632634
}
633635
},
634636
})
635637
```
636638

639+
The `session.user` object passed to the `beforeSessionSaved` hook will contain every claim in the ID Token, including custom claims. You can use the `filterDefaultIdTokenClaims` utility to filter out the standard claims and only keep the custom claims you want to persist.
640+
641+
> [!INFO]
642+
> Incase you want to understand which claims are being considered the default Id Token Claims, you can refer to `DEFAULT_ID_TOKEN_CLAIMS`, which can be imported from the SDK from `@auth0/nextjs-auth0/server`:
643+
>
644+
> ```ts
645+
> import { DEFAULT_ID_TOKEN_CLAIMS } from "@auth0/nextjs-auth0/server"
646+
> ```
647+
648+
Alternatively, you can use the entire `session.user` object if you would like to include every claim in the ID Token by just returning the `session` like so:
649+
650+
```ts
651+
import { Auth0Client } from "@auth0/nextjs-auth0/server"
652+
653+
export const auth0 = new Auth0Client({
654+
async beforeSessionSaved(session, idToken) {
655+
return session
656+
},
657+
})
658+
```
659+
Do realize that this has an impact on the size of the cookie being issued, so it's best to limit the claims to only those that are necessary for your application.
660+
637661
### `onCallback`
638662

639663
The `onCallback` hook is run once the user has been redirected back from Auth0 to your application with either an error or the authorization code which will be verified and exchanged.

src/server/auth-client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { toSafeRedirect } from "../utils/url-helpers";
3535
import { addCacheControlHeadersForSession } from "./cookies";
3636
import { AbstractSessionStore } from "./session/abstract-session-store";
3737
import { TransactionState, TransactionStore } from "./transaction-store";
38-
import { filterClaims } from "./user";
38+
import { filterDefaultIdTokenClaims } from "./user";
3939

4040
export type BeforeSessionSavedHook = (
4141
session: SessionData,
@@ -566,7 +566,7 @@ export class AuthClient {
566566
internal: session.internal
567567
};
568568
} else {
569-
session.user = filterClaims(idTokenClaims);
569+
session.user = filterDefaultIdTokenClaims(idTokenClaims);
570570
}
571571

572572
await this.sessionStore.set(req.cookies, res.cookies, session, true);

src/server/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ export { AuthClient } from "./auth-client";
55
export { TransactionStore } from "./transaction-store";
66

77
export { AbstractSessionStore } from "./session/abstract-session-store";
8+
9+
export { filterDefaultIdTokenClaims, DEFAULT_ID_TOKEN_CLAIMS } from "./user";

src/server/user.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { describe, expect, it } from "vitest";
22

3-
import { filterClaims } from "./user";
3+
import { filterDefaultIdTokenClaims } from "./user";
44

5-
describe("filterClaims", async () => {
5+
describe("filterDefaultIdTokenClaims", async () => {
66
it("should return only the allowed claims", () => {
77
const claims = {
88
sub: "user_123",
@@ -20,7 +20,7 @@ describe("filterClaims", async () => {
2020
exp: 1234567890
2121
};
2222

23-
expect(filterClaims(claims)).toEqual({
23+
expect(filterDefaultIdTokenClaims(claims)).toEqual({
2424
sub: "user_123",
2525
name: "John Doe",
2626
nickname: "johndoe",
@@ -34,6 +34,6 @@ describe("filterClaims", async () => {
3434
});
3535

3636
it("should return an empty object if no claims are provided", () => {
37-
expect(filterClaims({})).toEqual({});
37+
expect(filterDefaultIdTokenClaims({})).toEqual({});
3838
});
3939
});

src/server/user.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { User } from "../types";
22

3-
const DEFAULT_ALLOWED_CLAIMS = [
3+
/**
4+
* Default claims for the ID token.
5+
*/
6+
export const DEFAULT_ID_TOKEN_CLAIMS = [
47
"sub",
58
"name",
69
"nickname",
@@ -12,9 +15,14 @@ const DEFAULT_ALLOWED_CLAIMS = [
1215
"org_id"
1316
];
1417

15-
export function filterClaims(claims: { [key: string]: any }) {
18+
/**
19+
* Filters the claims to only include those that are considered default.
20+
* @param claims The claims to filter.
21+
* @returns The filtered claims containing only default ID token claims.
22+
*/
23+
export function filterDefaultIdTokenClaims(claims: { [key: string]: any }) {
1624
return Object.keys(claims).reduce((acc, key) => {
17-
if (DEFAULT_ALLOWED_CLAIMS.includes(key)) {
25+
if (DEFAULT_ID_TOKEN_CLAIMS.includes(key)) {
1826
acc[key] = claims[key];
1927
}
2028
return acc;

0 commit comments

Comments
 (0)