11import { __internal_useUserEnterpriseConnections , useSession , useUser } from '@clerk/shared/react/index' ;
2- import type { EnterpriseConnectionResource } from '@clerk/shared/types' ;
2+ import type { EnterpriseConnectionResource , SignedInSessionResource , UserResource } from '@clerk/shared/types' ;
33import React , { type PropsWithChildren , useCallback } from 'react' ;
44
55import { useCardState } from '@/elements/contexts' ;
@@ -35,10 +35,16 @@ export interface ConfigureSSOData {
3535 * Creates a new enterprise connection.
3636 */
3737 createEnterpriseConnection : ( provider : ProviderType ) => Promise < void > ;
38+ /**
39+ * Determines if the user's domain is already wired to an enterprise connection that
40+ * doesn't belong to the org they're currently configuring
41+ */
42+ isDomainTakenByOtherOrg : boolean ;
3843}
3944
4045interface ConfigureSSOProviderProps {
4146 enterpriseConnection : EnterpriseConnectionResource | undefined ;
47+ hasSuccessfulTestRun : boolean ;
4248 contentRef : React . RefObject < HTMLDivElement > ;
4349}
4450
@@ -47,6 +53,7 @@ ConfigureSSOContext.displayName = 'ConfigureSSOContext';
4753
4854export const ConfigureSSOProvider = ( {
4955 enterpriseConnection,
56+ hasSuccessfulTestRun,
5057 contentRef,
5158 children,
5259} : PropsWithChildren < ConfigureSSOProviderProps > ) : JSX . Element => {
@@ -57,7 +64,9 @@ export const ConfigureSSOProvider = ({
5764 const { user } = useUser ( ) ;
5865 const { session } = useSession ( ) ;
5966 const card = useCardState ( ) ;
60- const initialStepId = deriveInitialStep ( enterpriseConnection ) ;
67+
68+ const isDomainTakenByOtherOrg = checkDomainTakenByOtherOrg ( user , session , enterpriseConnection ) ;
69+ const initialStepId = deriveInitialStep ( enterpriseConnection , { isDomainTakenByOtherOrg, hasSuccessfulTestRun } ) ;
6170
6271 const createEnterpriseConnection = useCallback (
6372 async ( provider : ProviderType ) : Promise < void > => {
@@ -85,14 +94,15 @@ export const ConfigureSSOProvider = ({
8594
8695 const value = React . useMemo < ConfigureSSOData > (
8796 ( ) => ( {
97+ provider,
98+ contentRef,
99+ setProvider,
88100 initialStepId,
89101 enterpriseConnection,
90- provider ,
102+ isDomainTakenByOtherOrg ,
91103 createEnterpriseConnection,
92- setProvider,
93- contentRef,
94104 } ) ,
95- [ initialStepId , enterpriseConnection , createEnterpriseConnection , provider , contentRef ] ,
105+ [ provider , contentRef , initialStepId , enterpriseConnection , createEnterpriseConnection , isDomainTakenByOtherOrg ] ,
96106 ) ;
97107
98108 return < ConfigureSSOContext . Provider value = { value } > { children } </ ConfigureSSOContext . Provider > ;
@@ -105,3 +115,20 @@ export const useConfigureSSO = (): ConfigureSSOData => {
105115 }
106116 return ctx ;
107117} ;
118+
119+ /**
120+ * Determines if the user's domain is already wired to an enterprise connection that
121+ * doesn't belong to the org they're currently configuring
122+ */
123+ const checkDomainTakenByOtherOrg = (
124+ user : UserResource | null | undefined ,
125+ session : SignedInSessionResource | null | undefined ,
126+ enterpriseConnection : EnterpriseConnectionResource | undefined ,
127+ ) : boolean => {
128+ const emailToVerify =
129+ user ?. primaryEmailAddress ?? user ?. emailAddresses ?. find ( e => e . verification . status !== 'verified' ) ;
130+ const isVerified = emailToVerify ?. verification . status === 'verified' ;
131+ const activeOrganizationId = session ?. lastActiveOrganizationId ?? null ;
132+
133+ return Boolean ( isVerified && enterpriseConnection && enterpriseConnection . organizationId !== activeOrganizationId ) ;
134+ } ;
0 commit comments