Skip to content

Commit fe91da6

Browse files
authored
fix: return null user when access token is expired in withAuth (#72)
1 parent cee5925 commit fe91da6

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/auth.spec.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -407,18 +407,13 @@ describe('auth', () => {
407407
const result = await withAuth(createMockRequest('wos-session=expired-session-data'));
408408

409409
// Should warn about expired token
410-
expect(consoleWarnSpy).toHaveBeenCalledWith('Access token expired for user');
410+
expect(consoleWarnSpy).toHaveBeenCalledWith(
411+
'[AuthKit] Access token expired. Ensure authkitLoader is used in a parent/root route to handle automatic token refresh.',
412+
);
411413

412-
// Result should still contain user info
414+
// Result should return null user when token is expired
413415
expect(result).toEqual({
414-
user: mockSession.user,
415-
sessionId: mockClaims.sessionId,
416-
organizationId: mockClaims.organizationId,
417-
role: mockClaims.role,
418-
permissions: mockClaims.permissions,
419-
entitlements: mockClaims.entitlements,
420-
impersonator: undefined,
421-
accessToken: mockSession.accessToken,
416+
user: null,
422417
});
423418

424419
consoleWarnSpy.mockRestore();

src/auth.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ export async function withAuth(args: LoaderFunctionArgs): Promise<UserInfo | NoU
5656
if (Date.now() >= exp * 1000) {
5757
// The access token is expired. This function does not handle token refresh.
5858
// Ensure that token refresh is implemented in the parent/root loader as documented.
59-
console.warn('Access token expired for user');
59+
console.warn(
60+
'[AuthKit] Access token expired. Ensure authkitLoader is used in a parent/root route to handle automatic token refresh.',
61+
);
62+
return {
63+
user: null,
64+
};
6065
}
6166

6267
return {

0 commit comments

Comments
 (0)