|
| 1 | +import { useClerk } from '@clerk/shared/react/index'; |
| 2 | +import type { ComponentType } from 'react'; |
| 3 | + |
| 4 | +import { buildSSOCallbackURL, withRedirect } from '@/ui/common'; |
| 5 | +import { ChooseEnterpriseConnectionCard } from '@/ui/common/ChooseEnterpriseConnectionCard'; |
| 6 | +import { useCoreSignIn, useEnvironment, useSignInContext } from '@/ui/contexts'; |
| 7 | +import { Flow, localizationKeys } from '@/ui/customizables'; |
| 8 | +import { withCardStateProvider } from '@/ui/elements/contexts'; |
| 9 | +import type { AvailableComponentProps } from '@/ui/types'; |
| 10 | + |
| 11 | +import { hasMultipleEnterpriseConnections } from './shared'; |
| 12 | + |
| 13 | +/** |
| 14 | + * @experimental |
| 15 | + */ |
| 16 | +const SignInFactorOneEnterpriseConnectionsInternal = () => { |
| 17 | + const ctx = useSignInContext(); |
| 18 | + const { displayConfig } = useEnvironment(); |
| 19 | + |
| 20 | + const clerk = useClerk(); |
| 21 | + const signIn = clerk.client.signIn; |
| 22 | + |
| 23 | + if (!hasMultipleEnterpriseConnections(signIn.supportedFirstFactors)) { |
| 24 | + // This should not happen due to the HOC guard, but provides type safety |
| 25 | + return null; |
| 26 | + } |
| 27 | + |
| 28 | + const enterpriseConnections = signIn.supportedFirstFactors.map(ff => ({ |
| 29 | + id: ff.enterpriseConnectionId, |
| 30 | + name: ff.enterpriseConnectionName, |
| 31 | + })); |
| 32 | + |
| 33 | + const handleEnterpriseSSO = (enterpriseConnectionId: string) => { |
| 34 | + const redirectUrl = buildSSOCallbackURL(ctx, displayConfig.signInUrl); |
| 35 | + const redirectUrlComplete = ctx.afterSignInUrl || '/'; |
| 36 | + |
| 37 | + return signIn.authenticateWithRedirect({ |
| 38 | + strategy: 'enterprise_sso', |
| 39 | + redirectUrl, |
| 40 | + redirectUrlComplete, |
| 41 | + oidcPrompt: ctx.oidcPrompt, |
| 42 | + continueSignIn: true, |
| 43 | + enterpriseConnectionId, |
| 44 | + }); |
| 45 | + }; |
| 46 | + |
| 47 | + return ( |
| 48 | + <Flow.Part part='enterpriseConnections'> |
| 49 | + <ChooseEnterpriseConnectionCard |
| 50 | + title={localizationKeys('signIn.enterpriseConnections.title')} |
| 51 | + subtitle={localizationKeys('signIn.enterpriseConnections.subtitle')} |
| 52 | + onClick={handleEnterpriseSSO} |
| 53 | + enterpriseConnections={enterpriseConnections} |
| 54 | + /> |
| 55 | + </Flow.Part> |
| 56 | + ); |
| 57 | +}; |
| 58 | + |
| 59 | +const withEnterpriseConnectionsGuard = <P extends AvailableComponentProps>(Component: ComponentType<P>) => { |
| 60 | + const displayName = Component.displayName || Component.name || 'Component'; |
| 61 | + Component.displayName = displayName; |
| 62 | + |
| 63 | + const HOC = (props: P) => { |
| 64 | + const signIn = useCoreSignIn(); |
| 65 | + const signInCtx = useSignInContext(); |
| 66 | + |
| 67 | + return withRedirect( |
| 68 | + Component, |
| 69 | + () => !hasMultipleEnterpriseConnections(signIn.supportedFirstFactors), |
| 70 | + ({ clerk }) => signInCtx.signInUrl || clerk.buildSignInUrl(), |
| 71 | + 'There are no enterprise connections available to sign-in. Clerk is redirecting to the `signInUrl` instead.', |
| 72 | + )(props); |
| 73 | + }; |
| 74 | + |
| 75 | + HOC.displayName = `withEnterpriseConnectionsGuard(${displayName})`; |
| 76 | + |
| 77 | + return HOC; |
| 78 | +}; |
| 79 | + |
| 80 | +export const SignInFactorOneEnterpriseConnections = withCardStateProvider( |
| 81 | + withEnterpriseConnectionsGuard(SignInFactorOneEnterpriseConnectionsInternal), |
| 82 | +); |
0 commit comments