-
Notifications
You must be signed in to change notification settings - Fork 325
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
feat(clerk-js): Allow token refresh when Client failed to resolve #5345
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
@@ -68,7 +71,6 @@ export interface EnvironmentJSON extends ClerkResourceJSON { | |||
export interface ClientJSON extends ClerkResourceJSON { | |||
object: 'client'; | |||
id: string; | |||
status: any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is safe to remove
.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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per our fapi docs
image_url: string; | ||
has_image: boolean; | ||
username: string; | ||
username: string | null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per our fapi docs
first_name: string | null; | ||
last_name: string | null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per our fapi docs
@@ -275,7 +277,7 @@ export interface UserJSON extends ClerkResourceJSON { | |||
created_at: number; | |||
} | |||
|
|||
export interface PublicUserDataJSON extends ClerkResourceJSON { | |||
export interface PublicUserDataJSON { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per our fapi docs
…ent-fails # Conflicts: # packages/clerk-js/bundlewatch.config.json # packages/types/src/jwt.ts
{ | ||
object: 'session', | ||
id: sid, | ||
status: 'active', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…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
/client
fails 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__session
cookie to create the client based on the existing information (userId, sessionId, orgId etc.).With this PR, (only for standard browsers) if
/client
fails, clerk-js always initiates a local-only client to allow for operations likeClerk.session?.getToken()
to still operate as expected. That meansClerk.loaded
will betrue
even when we don't have a proper client object in memory.Checklist
pnpm test
runs as expected.pnpm build
runs as expected.Type of change