Skip to content

Commit fe3544f

Browse files
committed
fix(js): complete offline sign-out
Ensure local auth state reaches signed out when session removal fails with an offline network error. Preserve existing error handling for all other failures.
1 parent 760da4c commit fe3544f

3 files changed

Lines changed: 63 additions & 5 deletions

File tree

‎.changeset/calm-otters-sign.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
Complete local sign-out while offline when cached resources surface a network error.

‎packages/clerk-js/src/core/__tests__/clerk.test.ts‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ClerkOfflineError, EmailLinkErrorCodeStatus } from '@clerk/shared/error';
1+
import { ClerkOfflineError, ClerkRuntimeError, EmailLinkErrorCodeStatus } from '@clerk/shared/error';
22
import { ERROR_CODES } from '@clerk/shared/internal/clerk-js/constants';
33
import type {
44
ActiveSessionResource,
@@ -1290,6 +1290,53 @@ describe('Clerk singleton', () => {
12901290
},
12911291
);
12921292

1293+
it('completes local sign-out when removing sessions fails with an offline network error', async () => {
1294+
mockClientRemoveSessions.mockRejectedValue(
1295+
new ClerkRuntimeError('Network request failed.', { code: 'network_error' }),
1296+
);
1297+
mockClientFetch.mockReturnValue(
1298+
Promise.resolve({
1299+
...clientTouchDefaults,
1300+
signedInSessions: [mockSession1],
1301+
sessions: [mockSession1],
1302+
destroy: mockClientDestroy,
1303+
removeSessions: mockClientRemoveSessions,
1304+
}),
1305+
);
1306+
1307+
const sut = new Clerk(productionPublishableKey);
1308+
sut.navigate = vi.fn();
1309+
await sut.load();
1310+
1311+
await expect(sut.signOut()).resolves.toBeUndefined();
1312+
1313+
expect(sut.session).toBeNull();
1314+
expect(sut.navigate).toHaveBeenCalledWith('/');
1315+
});
1316+
1317+
it('rethrows non-network errors when removing sessions during sign-out', async () => {
1318+
const error = new ClerkRuntimeError('Request failed.', { code: 'unexpected_error' });
1319+
mockClientRemoveSessions.mockRejectedValue(error);
1320+
mockClientFetch.mockReturnValue(
1321+
Promise.resolve({
1322+
...clientTouchDefaults,
1323+
signedInSessions: [mockSession1],
1324+
sessions: [mockSession1],
1325+
destroy: mockClientDestroy,
1326+
removeSessions: mockClientRemoveSessions,
1327+
}),
1328+
);
1329+
1330+
const sut = new Clerk(productionPublishableKey);
1331+
sut.navigate = vi.fn();
1332+
await sut.load();
1333+
1334+
await expect(sut.signOut()).rejects.toBe(error);
1335+
1336+
expect(sut.session).toBeUndefined();
1337+
expect(sut.navigate).not.toHaveBeenCalled();
1338+
});
1339+
12931340
it('only removes the session that corresponds to the passed sessionId if it is not the current', async () => {
12941341
mockClientFetch.mockReturnValue(
12951342
Promise.resolve({

‎packages/clerk-js/src/core/clerk.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -789,10 +789,16 @@ export class Clerk implements ClerkInterface {
789789
if (!opts.sessionId || this.client.signedInSessions.length === 1) {
790790
this.#setTransitiveState();
791791

792-
if (this.#options.experimental?.persistClient ?? true) {
793-
await this.client.removeSessions();
794-
} else {
795-
await this.client.destroy();
792+
try {
793+
if (this.#options.experimental?.persistClient ?? true) {
794+
await this.client.removeSessions();
795+
} else {
796+
await this.client.destroy();
797+
}
798+
} catch (err) {
799+
if (!isClerkRuntimeError(err) || err.code !== 'network_error') {
800+
throw err;
801+
}
796802
}
797803

798804
await executeSignOut();

0 commit comments

Comments
 (0)