Skip to content

Commit

Permalink
fix(clerk-js): Fix tokenCache.test.ts (#4623)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosdouvlis authored Nov 21, 2024
1 parent 6b13aff commit b9a93fd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-rabbits-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Collect captcha token on load
17 changes: 16 additions & 1 deletion packages/clerk-js/src/core/auth/AuthCookieService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { setDevBrowserJWTInURL } from '@clerk/shared/devBrowser';
import { is4xxError, isClerkAPIResponseError, isNetworkError } from '@clerk/shared/error';
import type { Clerk, EnvironmentResource } from '@clerk/types';
import type { EnvironmentResource } from '@clerk/types';

import { clerkCoreErrorTokenRefreshFailed, clerkMissingDevBrowserJwt } from '../errors';
import { eventBus, events } from '../events';
import type { FapiClient } from '../fapiClient';
import type { Clerk } from '../resources/internal';
import { SessionTokenCache } from '../tokenCache';
import type { ClientUatCookieHandler } from './cookies/clientUat';
import { createClientUatCookie } from './cookies/clientUat';
import type { SessionCookieHandler } from './cookies/session';
Expand Down Expand Up @@ -103,6 +105,19 @@ export class AuthCookieService {
return setDevBrowserJWTInURL(url, devBrowserJwt);
}

/**
* Will refresh the session token immediately once
* if the captcha heartbeat flag is enabled.
* We want to do this to ensure that we collect at least one token
* even for short-lived sessions.
*/
public forceRefreshSessionToken() {
if (this.clerk.__unstable__environment?.displayConfig.captchaHeartbeat) {
SessionTokenCache.clear();
void this.refreshSessionToken();
}
}

private startPollingForToken() {
if (!this.poller) {
this.poller = new SessionCookiePoller();
Expand Down
2 changes: 1 addition & 1 deletion packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1828,8 +1828,8 @@ export class Clerk implements ClerkInterface {
}
}

this.#authService?.forceRefreshSessionToken();
this.#clearClerkQueryParams();

this.#handleImpersonationFab();
return true;
};
Expand Down

0 comments on commit b9a93fd

Please sign in to comment.