Skip to content

fix(clerk-js): Avoid client updates on sign out #5204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions .changeset/thick-beds-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Avoid client updates on sign out.
13 changes: 10 additions & 3 deletions packages/clerk-js/src/core/resources/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import { ClerkAPIResponseError, ClerkRuntimeError, Client } from './internal';
export type BaseFetchOptions = ClerkResourceReloadParams & {
forceUpdateClient?: boolean;
fetchMaxTries?: number;
skipUpdateClient?: boolean;
};

export type BaseMutateParams = {
action?: string;
body?: any;
method?: HTTPMethod;
path?: string;
skipUpdateClient?: boolean;
};

function assertProductionKeysOnDev(statusCode: number, payloadErrors?: ClerkAPIErrorJSON[]) {
Expand Down Expand Up @@ -105,7 +107,7 @@ export abstract class BaseResource {
}

// TODO: Link to Client payload piggybacking design document
if (requestInit.method !== 'GET' || opts.forceUpdateClient) {
if ((requestInit.method !== 'GET' || opts.forceUpdateClient) && !opts.skipUpdateClient) {
this._updateClient<J>(payload);
}

Expand Down Expand Up @@ -180,8 +182,13 @@ export abstract class BaseResource {
}

protected async _baseMutate<J extends ClerkResourceJSON | null>(params: BaseMutateParams): Promise<this> {
const { action, body, method, path } = params;
const json = await BaseResource._fetch<J>({ method, path: path || this.path(action), body });
const { action, body, method, path, skipUpdateClient } = params;
let json;
if (skipUpdateClient) {
json = await BaseResource._fetch<J>({ method, path: path || this.path(action), body }, { skipUpdateClient });
} else {
json = await BaseResource._fetch<J>({ method, path: path || this.path(action), body });
}
return this.fromJSON((json?.response || json) as J);
}

Expand Down
5 changes: 5 additions & 0 deletions packages/clerk-js/src/core/resources/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ export class Client extends BaseResource implements ClientResource {
removeSessions(): Promise<ClientResource> {
return this._baseDelete({
path: this.path() + '/sessions',
/**
* Skipping updating the client matches the behaviour of `client.destroy`, which allows broadcasting a sign-out event,
* and delays emitting until `setActive` is called within `Clerk.signOut()`
*/
skipUpdateClient: true,
}).then(e => {
SessionTokenCache.clear();
return e as unknown as ClientResource;
Expand Down
5 changes: 5 additions & 0 deletions packages/clerk-js/src/core/resources/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ export class Session extends BaseResource implements SessionResource {
SessionTokenCache.clear();
return this._basePost({
action: 'remove',
/**
* Skipping updating the client matches the behaviour of `client.destroy`, which allows broadcasting a sign-out event,
* and delays emitting until `setActive` is called within `Clerk.signOut()`
*/
skipUpdateClient: true,
});
};

Expand Down
Loading