Skip to content

Commit 5f3cc46

Browse files
authored
fix(clerk-js,types): Use intent parameter to reload resource (#5553)
1 parent 64e0d55 commit 5f3cc46

File tree

7 files changed

+24
-11
lines changed

7 files changed

+24
-11
lines changed

.changeset/tidy-dolls-join.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
'@clerk/types': patch
4+
---
5+
6+
Optionally handle the `intent` parameter on SSO redirects to reload specific resources.

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

+7-8
Original file line numberDiff line numberDiff line change
@@ -1528,16 +1528,15 @@ export class Clerk implements ClerkInterface {
15281528
// directs the opening page to navigate to the /sso-callback route), we need to reload the signIn and signUp resources
15291529
// to ensure that we have the latest state. This operation can fail when we try reloading a resource that doesn't
15301530
// exist (such as when reloading a signIn resource during a signUp attempt), but this can be safely ignored.
1531-
if (!window.opener) {
1531+
if (!window.opener && params.reloadResource) {
15321532
try {
1533-
await signIn.reload();
1534-
} catch {
1535-
// This can be safely ignored
1536-
}
1537-
try {
1538-
await signUp.reload();
1533+
if (params.reloadResource === 'signIn') {
1534+
await signIn.reload();
1535+
} else if (params.reloadResource === 'signUp') {
1536+
await signUp.reload();
1537+
}
15391538
} catch {
1540-
// This can be safely ignored
1539+
// This catch intentionally left blank.
15411540
}
15421541
}
15431542

packages/clerk-js/src/core/resources/SignIn.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ export class SignIn extends BaseResource implements SignInResource {
264264
if (!popup) {
265265
clerkMissingOptionError('popup');
266266
}
267-
return _authenticateWithPopup(SignIn.clerk, this.authenticateWithRedirectOrPopup, params, url => {
267+
return _authenticateWithPopup(SignIn.clerk, 'signIn', this.authenticateWithRedirectOrPopup, params, url => {
268268
popup.location.href = url.toString();
269269
});
270270
};

packages/clerk-js/src/core/resources/SignUp.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ export class SignUp extends BaseResource implements SignUpResource {
360360
clerkMissingOptionError('popup');
361361
}
362362

363-
return _authenticateWithPopup(SignUp.clerk, this.authenticateWithRedirectOrPopup, params, url => {
363+
return _authenticateWithPopup(SignUp.clerk, 'signUp', this.authenticateWithRedirectOrPopup, params, url => {
364364
popup.location.href = url instanceof URL ? url.toString() : url;
365365
});
366366
};

packages/clerk-js/src/ui/common/SSOCallback.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ export const SSOCallbackCard = (props: HandleOAuthCallbackParams | HandleSamlCal
2424
React.useEffect(() => {
2525
let timeoutId: ReturnType<typeof setTimeout>;
2626
if (__internal_setActiveInProgress !== true) {
27-
handleRedirectCallback({ ...props }, navigate).catch(e => {
27+
const intent = new URLSearchParams(window.location.search).get('intent');
28+
const reloadResource = intent === 'signIn' || intent === 'signUp' ? intent : undefined;
29+
handleRedirectCallback({ ...props, reloadResource }, navigate).catch(e => {
2830
handleError(e, [], card.setError);
2931
timeoutId = setTimeout(() => void navigate('../'), 4000);
3032
});

packages/clerk-js/src/utils/authenticateWithPopup.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { Clerk } from '../core/clerk';
55

66
export async function _authenticateWithPopup(
77
client: Clerk,
8+
reloadResource: 'signIn' | 'signUp',
89
authenticateMethod: (
910
params: AuthenticateWithRedirectParams,
1011
navigateCallback: (url: URL | string) => void,
@@ -27,6 +28,7 @@ export async function _authenticateWithPopup(
2728
const r = new URL(redirectUrl);
2829
r.searchParams.set('sign_in_force_redirect_url', params.redirectUrlComplete);
2930
r.searchParams.set('sign_up_force_redirect_url', params.redirectUrlComplete);
31+
r.searchParams.set('intent', reloadResource);
3032
// All URLs are decorated with the dev browser token in development mode since we're moving between AP and the app.
3133
const redirectUrlWithForceRedirectUrl = client.buildUrlWithAuth(r.toString());
3234

packages/types/src/clerk.ts

+4
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,10 @@ export type HandleOAuthCallbackParams = TransferableOption &
720720
* Full URL or path to navigate to after requesting phone verification.
721721
*/
722722
verifyPhoneNumberUrl?: string | null;
723+
/**
724+
* The underlying resource to optionally reload before processing an OAuth callback.
725+
*/
726+
reloadResource?: 'signIn' | 'signUp';
723727
};
724728

725729
export type HandleSamlCallbackParams = HandleOAuthCallbackParams;

0 commit comments

Comments
 (0)