Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/auth/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ export function normalizeExpiresAt(expiresAt: unknown): number | undefined {
}

export function isTokenExpired(idToken: string, expiresAt?: number, bufferSeconds = 60): boolean {
const expiry = normalizeExpiresAt(expiresAt) ?? getIdTokenExpiryMs(idToken);
const jwtExpiry = getIdTokenExpiryMs(idToken);
const configExpiry = normalizeExpiresAt(expiresAt);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we should remove the config expiresAt completely, its not needed anymore

we added this initially when we did not used to have the refresh mechanism, as we have the refresh mechanism, this config exp becomes stale, the no point in storing it

const expiry = jwtExpiry ?? configExpiry;
if (expiry === undefined) return true;
return expiry <= Date.now() + bufferSeconds * 1000;
}
32 changes: 32 additions & 0 deletions tests/auth/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,38 @@ describe('getValidAuthSession', () => {
}
});

it('returns ok without refresh when jwt is valid but config expiresAt is stale', async () => {
const token = makeJwt({
userId: 'u1',
email: 'a@b.com',
exp: Math.floor(Date.now() / 1000) + 3600,
});
vi.mocked(globalConfig.readGlobalConfig).mockResolvedValue({
user: {
uid: 'u1',
email: 'a@b.com',
idToken: token,
refreshToken: 'refresh-123',
expiresAt: Date.now() - 3600_000,
},
});

const fetchMock = vi.fn();
const originalFetch = globalThis.fetch;
globalThis.fetch = fetchMock;

const result = await getValidAuthSession();

globalThis.fetch = originalFetch;

expect(result.ok).toBe(true);
if (result.ok) {
expect(result.idToken).toBe(token);
expect(result.refreshed).toBe(false);
}
expect(fetchMock).not.toHaveBeenCalled();
});

it('refreshes token when expired and refresh token exists', async () => {
const oldToken = makeJwt({
userId: 'u1',
Expand Down
9 changes: 8 additions & 1 deletion tests/auth/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,17 @@ describe('isTokenExpired', () => {
expect(isTokenExpired(token, undefined, 0)).toBe(false);
});

it('uses expiresAt when provided', () => {
it('uses expiresAt when jwt has no exp claim', () => {
vi.setSystemTime(new Date('2025-01-01T12:00:00Z'));
const token = makeJwt({ userId: 'u1' });
const futureMs = new Date('2025-06-01').getTime();
expect(isTokenExpired(token, futureMs)).toBe(false);
});

it('prefers jwt exp over stale config expiresAt', () => {
vi.setSystemTime(new Date('2025-01-01T12:00:00Z'));
const token = makeJwt({ userId: 'u1', exp: 1735736400 });
const stalePastMs = new Date('2024-01-01').getTime();
expect(isTokenExpired(token, stalePastMs)).toBe(false);
});
});
Loading