Skip to content

Commit 570fa0e

Browse files
authored
fix: return null user when access token is expired in withAuth (#32)
1 parent e49600d commit 570fa0e

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/auth.spec.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -411,19 +411,13 @@ describe('auth', () => {
411411
const result = await withAuth(createMockRequest('wos-session=expired-session-data'));
412412

413413
// Should warn about expired token
414-
expect(consoleWarnSpy).toHaveBeenCalledWith('Access token expired for user');
414+
expect(consoleWarnSpy).toHaveBeenCalledWith(
415+
'[AuthKit] Access token expired. Ensure authkitLoader is used in a parent/root route to handle automatic token refresh.',
416+
);
415417

416-
// Result should still contain user info
418+
// Result should return null user when token is expired
417419
expect(result).toEqual({
418-
user: mockSession.user,
419-
sessionId: mockClaims.sessionId,
420-
organizationId: mockClaims.organizationId,
421-
role: mockClaims.role,
422-
permissions: mockClaims.permissions,
423-
entitlements: mockClaims.entitlements,
424-
featureFlags: mockClaims.featureFlags,
425-
impersonator: undefined,
426-
accessToken: mockSession.accessToken,
420+
user: null,
427421
});
428422

429423
consoleWarnSpy.mockRestore();

src/auth.ts

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

6368
return {

0 commit comments

Comments
 (0)