Skip to content

Commit 70b5e4b

Browse files
committed
fix(ui): pass selected provider to createConnection and restore reverification
- createConnection now accepts the provider as a parameter instead of reading it from React state (which was stale within the same tick as setProvider) - Restore the useReverification wrap on createConnection, matching the rest of @clerk/ui's user.* mutation pattern (createEmailAddress, delete, createTOTP, setPrimary, createWeb3Wallet, …) — handles backend session-reverification policy transparently when present, no-ops otherwise
1 parent 0c08efd commit 70b5e4b

3 files changed

Lines changed: 29 additions & 26 deletions

File tree

packages/ui/src/components/ConfigureSSO/ConfigureSSOContext.tsx

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { __internal_useUserEnterpriseConnections, useSession, useUser } from '@clerk/shared/react';
1+
import { __internal_useUserEnterpriseConnections, useReverification, useSession, useUser } from '@clerk/shared/react';
22
import type { EnterpriseConnectionResource } from '@clerk/shared/types';
33
import React, { type PropsWithChildren } from 'react';
44

@@ -30,12 +30,11 @@ export interface ConfigureSSOData {
3030
*/
3131
setProvider: (provider: ProviderType) => void;
3232
/**
33-
* Creates the enterprise connection from the current provider selection,
34-
* the user's primary email domain, and the session's active organization.
35-
* No-ops when an enterprise connection already exists so callers can
36-
* safely re-trigger.
33+
* Creates the enterprise connection for the supplied provider, the user's
34+
* primary email domain, and the session's active organization. No-ops when
35+
* an enterprise connection already exists so callers can safely re-trigger.
3736
*/
38-
createConnection: () => Promise<void>;
37+
createConnection: (provider: ProviderType) => Promise<void>;
3938
}
4039

4140
interface ConfigureSSOFlowProviderProps {
@@ -61,26 +60,28 @@ export const ConfigureSSOFlowProvider = ({
6160

6261
const initialStepId = deriveInitialStep(enterpriseConnection);
6362

64-
const createConnection = React.useCallback(async () => {
65-
if (enterpriseConnection) {
66-
return;
67-
}
68-
if (!provider) {
69-
throw new Error('Provider not selected');
70-
}
71-
if (!user?.primaryEmailAddress) {
72-
throw new Error('Primary email required');
73-
}
63+
const createConnectionFetcher = React.useCallback(
64+
async (selectedProvider: ProviderType) => {
65+
if (enterpriseConnection) {
66+
return;
67+
}
68+
if (!user?.primaryEmailAddress) {
69+
throw new Error('Primary email required');
70+
}
7471

75-
const emailDomain = user.primaryEmailAddress.emailAddress.split('@')[1];
76-
const organizationId = session?.lastActiveOrganizationId ?? null;
72+
const emailDomain = user.primaryEmailAddress.emailAddress.split('@')[1];
73+
const organizationId = session?.lastActiveOrganizationId ?? null;
7774

78-
await createEnterpriseConnection({
79-
provider: 'saml_okta',
80-
name: emailDomain,
81-
organizationId,
82-
});
83-
}, [enterpriseConnection, provider, user, session, createEnterpriseConnection]);
75+
await createEnterpriseConnection({
76+
provider: selectedProvider,
77+
name: emailDomain,
78+
organizationId,
79+
});
80+
},
81+
[enterpriseConnection, user, session, createEnterpriseConnection],
82+
);
83+
84+
const createConnection = useReverification(createConnectionFetcher);
8485

8586
const value = React.useMemo<ConfigureSSOData>(
8687
() => ({
@@ -91,7 +92,7 @@ export const ConfigureSSOFlowProvider = ({
9192
setProvider,
9293
createConnection,
9394
}),
94-
[initialStepId, enterpriseConnection, isLoading, provider, setProvider, createConnection],
95+
[initialStepId, enterpriseConnection, isLoading, provider, createConnection],
9596
);
9697

9798
return <ConfigureSSOFlowContext.Provider value={value}>{children}</ConfigureSSOFlowContext.Provider>;

packages/ui/src/components/ConfigureSSO/steps/SelectProviderStep.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const SelectProviderStep = (): JSX.Element => {
5757
card.setLoading();
5858

5959
try {
60-
await createConnection();
60+
await createConnection(selected);
6161
void goNext();
6262
} catch (err) {
6363
handleError(err as Error, [], card.setError);

packages/ui/src/components/ConfigureSSO/steps/__tests__/SelectProviderStep.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ describe('SelectProviderStep', () => {
163163

164164
expect(setProvider).toHaveBeenCalledWith('saml_okta');
165165
expect(createConnection).toHaveBeenCalledTimes(1);
166+
expect(createConnection).toHaveBeenCalledWith('saml_okta');
166167
expect(callOrder).toEqual(['setProvider', 'createConnection', 'goNext']);
167168
});
168169

@@ -180,6 +181,7 @@ describe('SelectProviderStep', () => {
180181

181182
expect(setProvider).toHaveBeenCalledWith('saml_custom');
182183
expect(createConnection).toHaveBeenCalledTimes(1);
184+
expect(createConnection).toHaveBeenCalledWith('saml_custom');
183185
});
184186

185187
it('shows loading state while createConnection is pending', async () => {

0 commit comments

Comments
 (0)