Uh oh!
There was an error while loading. Please reload this page.
feat(clerk-js): Allow token refresh when Client failed to resolve - #5345
Conversation
🦋 Changeset detectedLatest commit: 14af574 The changes in this PR will be included in the next version bump. This PR includes changesets to release 22 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
…ent-fails # Conflicts: # packages/clerk-js/bundlewatch.config.json
| if (isNetworkError(e)) { | ||
| return; | ||
| } | ||
| clerkCoreErrorTokenRefreshFailed(e.toString()); |
There was a problem hiding this comment.
We need to stop throwing errors on status 5xx here. These errors are not useful to us, and developers cannot catch them and handle them.
In case of an FAPI outage, the poller would continue to throw errors, that would pollute a customer's Sentry logs
| export interface ClientJSON extends ClerkResourceJSON { | ||
| object: 'client'; | ||
| id: string; | ||
| status: any; |
There was a problem hiding this comment.
this is safe to remove
Uh oh!
There was an error while loading. Please reload this page.
| .then(res => this.updateClient(res)) | ||
| .catch(async e => { | ||
| if (isClerkAPIResponseError(e) && e.errors[0].code === 'requires_captcha') { | ||
| throw e; | ||
| } | ||
| const jwtInCookie = this.#authService?.getSessionCookie(); | ||
| const localClient = createClientFromJwt(jwtInCookie); | ||
| this.updateClient(localClient); | ||
| // Always grab a fresh token | ||
| await this.session?.getToken({ skipCache: true }); | ||
| // Allows for Clerk to be marked as loaded with the client and session created from the JWT. | ||
| return null; | ||
| }); |
There was a problem hiding this comment.
I wouldn't focus too much on where this code is located, we might refactor once both this and #5287 are merged.
| external_id: string | null; | ||
| primary_email_address_id: string | null; | ||
| primary_phone_number_id: string | null; | ||
| primary_web3_wallet_id: string | null; |
| image_url: string; | ||
| has_image: boolean; | ||
| username: string; | ||
| username: string | null; |
| first_name: string | null; | ||
| last_name: string | null; |
| } | ||
| export interface PublicUserDataJSON extends ClerkResourceJSON { | ||
| export interface PublicUserDataJSON { |
…ent-fails # Conflicts: # packages/clerk-js/bundlewatch.config.json # packages/types/src/jwt.ts
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| { | ||
| object: 'session', | ||
| id: sid, | ||
| status: 'active', |
There was a problem hiding this comment.
This could be added in a later PR, but we now have a new claim in the JWT: sts - it's only added when a session is in a pending status
We should extract it here and default to active: sts ?? 'active' -> happy to open up another PR later to update the JWT types and apply it here as well!
There was a problem hiding this comment.
Doing this in another PR sounds proper. TBH i don't think it is that important, because it might force redirect you to the appropriate page, but clerk will be down so you will not be able to proceed.
Uh oh!
There was an error while loading. Please reload this page.
…sh-when-client-fails # Conflicts: # packages/clerk-js/bundlewatch.config.json
Description
Until now we depended on the client to be successfully fetched in order to mark clerk as initialized. This is changing in this PR.
When
/clientfails for any reason, a 4xx error, a 5xx error, a network error (aka blocked), we are creating a "local" only representation of the client. If the user is already signed in, clerk-js leverages the existing JWT of the__sessioncookie to create the client based on the existing information (userId, sessionId, orgId etc.).With this PR, (only for standard browsers) if
/clientfails, clerk-js always initiates a local-only client to allow for operations likeClerk.session?.getToken()to still operate as expected. That meansClerk.loadedwill betrueeven when we don't have a proper client object in memory.Checklist
pnpm testruns as expected.pnpm buildruns as expected.Type of change