Skip to content

Commit a67785c

Browse files
committed
feat(ui,clerk-js,shared,localizations): Offer enterprise SSO in the reverification modal
The modal dropped enterprise_sso from the supported first factors, so a user whose only factor is an identity provider got "no available methods". It now shows a button per enterprise connection, runs the IdP round trip in a popup that reports back through the Account Portal popup callback, and falls back to a full-page redirect when the popup is blocked. The prepare call sends the email address of the enterprise account tied to the chosen connection, which FAPI requires. After the popup reports back, the modal reloads the session and only completes once the first factor shows as freshly verified, so a failed identity provider round trip surfaces as an error instead of a success. Apps that cannot open a popup can pass `oauthFlow: 'redirect'` to `useReverification` or to the modal props. 'auto' follows the sign-in heuristic. The default stays 'popup'. Part of ORGS-1939
1 parent 3abc482 commit a67785c

15 files changed

Lines changed: 586 additions & 21 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@clerk/ui': minor
3+
'@clerk/clerk-js': patch
4+
'@clerk/shared': patch
5+
'@clerk/localizations': patch
6+
---
7+
8+
Offer enterprise SSO as a first factor in the reverification modal. Users with an `enterprise_sso` factor now get a button per enterprise connection, verify with their identity provider in a popup, and the action that asked for reverification resumes without a page reload. When the popup is blocked the modal falls back to a full-page redirect, and `oauthFlow: 'redirect'` on `useReverification` or the modal props opts into the redirect from the start. `session.prepareFirstFactorVerification` accepts `actionCompleteRedirectUrl` for the `enterprise_sso` strategy.

‎packages/clerk-js/src/core/resources/Session.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ export class Session extends BaseResource implements SessionResource {
280280
emailAddressId: factor.emailAddressId,
281281
enterpriseConnectionId: factor.enterpriseConnectionId,
282282
redirectUrl: factor.redirectUrl,
283+
actionCompleteRedirectUrl: factor.actionCompleteRedirectUrl,
283284
} as EnterpriseSSOConfig;
284285
break;
285286
default:

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { buildAccountsBaseUrl } from '@clerk/shared/buildAccountsBaseUrl';
2+
import { buildPopupCallbackUrls } from '@clerk/shared/internal/clerk-js/popupCallback';
23
import type { AuthenticateWithPopupParams, AuthenticateWithRedirectParams } from '@clerk/shared/types';
34

45
import type { Clerk } from '../core/clerk';
@@ -19,8 +20,6 @@ export async function _authenticateWithPopup(
1920
return;
2021
}
2122

22-
const accountPortalHost = buildAccountsBaseUrl(client.frontendApi);
23-
2423
const { redirectUrl } = params;
2524

2625
// We set the force_redirect_url query parameter to ensure that the user is redirected to the correct page even
@@ -32,10 +31,11 @@ export async function _authenticateWithPopup(
3231
// All URLs are decorated with the dev browser token in development mode since we're moving between AP and the app.
3332
const redirectUrlWithForceRedirectUrl = client.buildUrlWithAuth(r.toString());
3433

35-
const popupRedirectUrlComplete = client.buildUrlWithAuth(`${accountPortalHost}/popup-callback`);
36-
const popupRedirectUrl = client.buildUrlWithAuth(
37-
`${accountPortalHost}/popup-callback?return_url=${encodeURIComponent(redirectUrlWithForceRedirectUrl)}`,
38-
);
34+
const {
35+
accountsOrigin: accountPortalHost,
36+
redirectUrl: popupRedirectUrl,
37+
redirectUrlComplete: popupRedirectUrlComplete,
38+
} = buildPopupCallbackUrls(client.frontendApi, url => client.buildUrlWithAuth(url), redirectUrlWithForceRedirectUrl);
3939

4040
const messageHandler = async (event: MessageEvent) => {
4141
if (event.origin !== accountPortalHost) {
@@ -101,8 +101,6 @@ export function wrapWithPopupRoutes(
101101
redirectUrl: string;
102102
},
103103
): { redirectCallbackUrl: string; redirectUrl: string } {
104-
const accountPortalHost = buildAccountsBaseUrl(client.frontendApi);
105-
106104
// We set the force_redirect_url query parameter to ensure that the user is redirected to the correct page even
107105
// in situations like a modal transfer flow.
108106
const r = new URL(redirectCallbackUrl);
@@ -111,9 +109,10 @@ export function wrapWithPopupRoutes(
111109
// All URLs are decorated with the dev browser token in development mode since we're moving between AP and the app.
112110
const redirectUrlWithForceRedirectUrl = client.buildUrlWithAuth(r.toString());
113111

114-
const popupRedirectUrlComplete = client.buildUrlWithAuth(`${accountPortalHost}/popup-callback`);
115-
const popupRedirectUrl = client.buildUrlWithAuth(
116-
`${accountPortalHost}/popup-callback?return_url=${encodeURIComponent(redirectUrlWithForceRedirectUrl)}`,
112+
const { redirectUrl: popupRedirectUrl, redirectUrlComplete: popupRedirectUrlComplete } = buildPopupCallbackUrls(
113+
client.frontendApi,
114+
url => client.buildUrlWithAuth(url),
115+
redirectUrlWithForceRedirectUrl,
117116
);
118117

119118
return { redirectCallbackUrl: popupRedirectUrl, redirectUrl: popupRedirectUrlComplete };

‎packages/localizations/src/en-US.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,7 @@ export const enUS: LocalizationResource = {
16411641
actionText: 'Don’t have any of these?',
16421642
blockButton__backupCode: 'Use a backup code',
16431643
blockButton__emailCode: 'Email code to {{identifier}}',
1644+
blockButton__enterpriseSSO: 'Continue with {{name}}',
16441645
blockButton__passkey: 'Use your passkey',
16451646
blockButton__password: 'Continue with your password',
16461647
blockButton__phoneCode: 'Send SMS code to {{identifier}}',
@@ -1664,6 +1665,10 @@ export const enUS: LocalizationResource = {
16641665
subtitle: 'Enter the code sent to your email to continue',
16651666
title: 'Verification required',
16661667
},
1668+
enterpriseSSO: {
1669+
subtitle: 'Continue with your enterprise account to confirm it’s you',
1670+
title: 'Verification required',
1671+
},
16671672
noAvailableMethods: {
16681673
message: 'Cannot proceed with verification. No suitable authentication factor is configured',
16691674
subtitle: 'An error occurred',
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { buildAccountsBaseUrl } from '../../buildAccountsBaseUrl';
2+
import { ClerkRuntimeError } from '../../errors/clerkRuntimeError';
3+
4+
export const POPUP_CLOSED_ERROR_CODE = 'popup_closed';
5+
6+
export function buildPopupCallbackUrls(
7+
frontendApi: string,
8+
buildUrlWithAuth: (url: string) => string,
9+
returnUrl: string,
10+
): { accountsOrigin: string; redirectUrl: string; redirectUrlComplete: string } {
11+
const accountsOrigin = buildAccountsBaseUrl(frontendApi);
12+
return {
13+
accountsOrigin,
14+
redirectUrl: buildUrlWithAuth(`${accountsOrigin}/popup-callback?return_url=${encodeURIComponent(returnUrl)}`),
15+
redirectUrlComplete: buildUrlWithAuth(`${accountsOrigin}/popup-callback`),
16+
};
17+
}
18+
19+
export function waitForPopupCallback(popup: Window, url: string, accountsOrigin: string): Promise<void> {
20+
return new Promise<void>((resolve, reject) => {
21+
let settled = false;
22+
const settle = (fn: () => void) => {
23+
if (settled) {
24+
return;
25+
}
26+
settled = true;
27+
window.removeEventListener('message', onMessage);
28+
clearInterval(interval);
29+
fn();
30+
};
31+
const onMessage = (event: MessageEvent) => {
32+
if (event.origin !== accountsOrigin || (event.source && event.source !== popup)) {
33+
return;
34+
}
35+
if (event.data?.session || event.data?.return_url) {
36+
settle(resolve);
37+
}
38+
};
39+
const interval = setInterval(() => {
40+
if (popup.closed) {
41+
clearInterval(interval);
42+
setTimeout(
43+
() =>
44+
settle(() =>
45+
reject(
46+
new ClerkRuntimeError('The popup was closed before the flow completed', {
47+
code: POPUP_CLOSED_ERROR_CODE,
48+
}),
49+
),
50+
),
51+
1000,
52+
);
53+
}
54+
}, 500);
55+
56+
window.addEventListener('message', onMessage);
57+
popup.location.href = url;
58+
});
59+
}

‎packages/shared/src/react/hooks/useReverification.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { validateReverificationConfig } from '../../authorization';
44
import { isReverificationHint, reverificationError } from '../../authorization-errors';
55
import { ClerkRuntimeError, isClerkAPIResponseError } from '../../error';
66
import { eventMethodCalled } from '../../telemetry';
7-
import type { Clerk, SessionVerificationLevel } from '../../types';
7+
import type { Clerk, OAuthFlow, SessionVerificationLevel } from '../../types';
88
import { createDeferredPromise } from '../../utils/createDeferredPromise';
99
import { useClerk } from './useClerk';
1010
import { useSafeLayoutEffect } from './useSafeLayoutEffect';
@@ -58,6 +58,12 @@ export type NeedsReverificationParameters = {
5858
* @interface
5959
*/
6060
export type UseReverificationOptions = {
61+
/**
62+
* Control whether enterprise SSO verification in the default UI uses a popup or a full-page redirect. `'auto'` follows the same heuristic as the sign-in component.
63+
*
64+
* @default `'popup'`
65+
*/
66+
oauthFlow?: OAuthFlow;
6167
/**
6268
* Handler for the reverification process. Opts out of using the default UI. Use this to build a custom UI.
6369
*
@@ -140,6 +146,7 @@ function createReverificationHandler(params: CreateReverificationHandlerParams)
140146
*/
141147
params.openUIComponent?.({
142148
level: level,
149+
oauthFlow: params.oauthFlow,
143150
afterVerification: complete,
144151
afterVerificationCancelled: cancel,
145152
});

‎packages/shared/src/types/clerk.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,11 @@ export type RoutingOptions =
18761876
| { path: string | undefined; routing?: Extract<RoutingStrategy, 'path'> }
18771877
| { path?: never; routing?: Extract<RoutingStrategy, 'hash'> };
18781878

1879+
/**
1880+
* Whether an OAuth or enterprise SSO round trip opens in a popup or navigates the page. `'auto'` opens a popup only where the current origin supports it.
1881+
*/
1882+
export type OAuthFlow = 'auto' | 'redirect' | 'popup';
1883+
18791884
/** @generateWithEmptyComment */
18801885
export type SignInProps = RoutingOptions & {
18811886
/**
@@ -1931,7 +1936,7 @@ export type SignInProps = RoutingOptions & {
19311936
/**
19321937
* Control whether OAuth flows use redirects or popups.
19331938
*/
1934-
oauthFlow?: 'auto' | 'redirect' | 'popup';
1939+
oauthFlow?: OAuthFlow;
19351940
/**
19361941
* Optional for `oauth_<provider>` or `enterprise_sso` strategies. The value to pass to the [OIDC prompt parameter](https://openid.net/specs/openid-connect-core-1_0.html#:~:text=prompt,reauthentication%20and%20consent.) in the generated OAuth redirect URL.
19371942
*/
@@ -1979,6 +1984,13 @@ export type __internal_UserVerificationProps = RoutingOptions & {
19791984
*/
19801985
afterVerificationCancelled?: () => void;
19811986

1987+
/**
1988+
* Control whether enterprise SSO verification uses a popup or a full-page redirect. `'auto'` follows the same heuristic as the sign-in component.
1989+
*
1990+
* @default `'popup'`
1991+
*/
1992+
oauthFlow?: OAuthFlow;
1993+
19821994
/**
19831995
* Defines the steps of the verification flow.
19841996
* When `multiFactor` is used, the user will be prompt for a first factor flow followed by a second factor flow.
@@ -2114,7 +2126,7 @@ export type SignUpProps = RoutingOptions & {
21142126
/**
21152127
* Control whether OAuth flows use redirects or popups.
21162128
*/
2117-
oauthFlow?: 'auto' | 'redirect' | 'popup';
2129+
oauthFlow?: OAuthFlow;
21182130
/**
21192131
* Optional for `oauth_<provider>` or `enterprise_sso` strategies. The value to pass to the [OIDC prompt parameter](https://openid.net/specs/openid-connect-core-1_0.html#:~:text=prompt,reauthentication%20and%20consent.) in the generated OAuth redirect URL.
21202132
*/

‎packages/shared/src/types/localization.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,10 @@ export type __internal_LocalizationResource = {
654654
subtitle: LocalizationValue;
655655
actionLink: LocalizationValue;
656656
};
657+
enterpriseSSO: {
658+
title: LocalizationValue;
659+
subtitle: LocalizationValue;
660+
};
657661
emailCode: {
658662
title: LocalizationValue;
659663
subtitle: LocalizationValue;
@@ -702,6 +706,7 @@ export type __internal_LocalizationResource = {
702706
blockButton__totp: LocalizationValue;
703707
blockButton__passkey: LocalizationValue;
704708
blockButton__backupCode: LocalizationValue;
709+
blockButton__enterpriseSSO: LocalizationValue<'name'>;
705710
getHelp: {
706711
title: LocalizationValue;
707712
content: LocalizationValue;

‎packages/shared/src/types/session.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ export type SessionVerifyPrepareFirstFactorParams =
540540
/**
541541
* @experimental
542542
*/
543-
| Omit<EnterpriseSSOConfig, 'actionCompleteRedirectUrl'>
543+
| (Omit<EnterpriseSSOConfig, 'actionCompleteRedirectUrl'> & { actionCompleteRedirectUrl?: string })
544544
/**
545545
* @experimental
546546
*/

‎packages/ui/src/common/ChooseEnterpriseConnectionCard.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type ChooseEnterpriseConnectionCardProps = {
1515
subtitle: LocalizationKey;
1616
onClick: (id: string) => Promise<void>;
1717
enterpriseConnections: Array<{ id: string; name: string; logoPublicUrl?: string | null; provider?: string }>;
18+
showLogo?: boolean;
1819
};
1920

2021
/**
@@ -25,14 +26,15 @@ export const ChooseEnterpriseConnectionCard = ({
2526
subtitle,
2627
onClick,
2728
enterpriseConnections,
29+
showLogo = true,
2830
children,
2931
}: PropsWithChildren<ChooseEnterpriseConnectionCardProps>) => {
3032
const card = useCardState();
3133

3234
return (
3335
<Card.Root>
3436
<Card.Content>
35-
<Header.Root showLogo>
37+
<Header.Root showLogo={showLogo}>
3638
<Header.Title localizationKey={title} />
3739
<Header.Subtitle localizationKey={subtitle} />
3840
</Header.Root>

0 commit comments

Comments
 (0)