Skip to content

Commit 2000605

Browse files
fix: merge sessionChanges before finalizing session after refresh (#2401)
- Pass sessionChanges to finalizeSession so beforeSessionSaved hook receives refreshed tokens - Add tests to verify beforeSessionSaved hook receives refreshed accessToken - Add tests to verify changes made in beforeSessionSaved hook are honored Co-authored-by: Wolfgang Goedel <[email protected]>
1 parent 0055cc4 commit 2000605

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,5 @@ dist
141141
*.tmp
142142
*PLAN*.md
143143
.yalc/
144-
yalc.lock
144+
yalc.lock
145+
.npmrc

src/server/client.test.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,13 @@ describe("Auth0Client", () => {
244244
// Restore mocking of getTokenSet directly
245245
mockGetTokenSet = vi
246246
.spyOn(AuthClient.prototype as any, "getTokenSet")
247-
.mockResolvedValue([null, mockRefreshedTokenSet]); // Simulate successful refresh
247+
.mockResolvedValue([
248+
null,
249+
{
250+
tokenSet: mockRefreshedTokenSet,
251+
idTokenClaims: {}
252+
}
253+
]); // Simulate successful refresh
248254

249255
// Remove mocks for discoverAuthorizationServerMetadata and getClientAuth
250256
// Remove fetch mock
@@ -286,6 +292,53 @@ describe("Auth0Client", () => {
286292
// Verify save was not called
287293
expect(mockSaveToSession).not.toHaveBeenCalled();
288294
});
295+
296+
it("should provide the refreshed accessToken to beforeSessionSaved hook", async () => {
297+
let accessToken: string | undefined;
298+
299+
client = new Auth0Client({
300+
beforeSessionSaved: async (session) => {
301+
accessToken = session.tokenSet?.accessToken;
302+
return session;
303+
}
304+
});
305+
306+
const mockReq = { headers: new Headers() } as NextRequest;
307+
const mockRes = new NextResponse();
308+
309+
await client.getAccessToken(mockReq, mockRes, { refresh: true });
310+
311+
expect(accessToken).toBe("new_access_token");
312+
});
313+
314+
it("should honor changes made to the tokenSet in beforeSessionSaved hook", async () => {
315+
client = new Auth0Client({
316+
beforeSessionSaved: async (session) => {
317+
return {
318+
...session,
319+
tokenSet: {
320+
...session.tokenSet,
321+
idToken: "modified_id_token"
322+
}
323+
};
324+
}
325+
});
326+
327+
const mockReq = { headers: new Headers() } as NextRequest;
328+
const mockRes = new NextResponse();
329+
330+
await client.getAccessToken(mockReq, mockRes, { refresh: true });
331+
332+
expect(mockSaveToSession).toHaveBeenCalledWith(
333+
expect.objectContaining({
334+
tokenSet: expect.objectContaining({
335+
idToken: "modified_id_token"
336+
})
337+
}),
338+
mockReq,
339+
mockRes
340+
);
341+
});
289342
});
290343

291344
describe("constructor configuration", () => {

src/server/client.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -695,17 +695,10 @@ export class Auth0Client {
695695
// call beforeSessionSaved callback if present
696696
// if not then filter id_token claims with default rules
697697
const finalSession = await this.authClient.finalizeSession(
698-
session,
698+
{ ...session, ...sessionChanges },
699699
tokenSet.idToken
700700
);
701-
await this.saveToSession(
702-
{
703-
...finalSession,
704-
...sessionChanges
705-
},
706-
req,
707-
res
708-
);
701+
await this.saveToSession(finalSession, req, res);
709702
}
710703

711704
return {

0 commit comments

Comments
 (0)