From 59f10b3db55594812af4b863cc50af1162c74928 Mon Sep 17 00:00:00 2001 From: Laura Beatris <48022589+LauraBeatris@users.noreply.github.com> Date: Tue, 3 Dec 2024 18:43:27 -0300 Subject: [PATCH] Rename `matchesEnterpriseConnection` to `hasEnterpriseSso` --- .../src/core/resources/EmailAddress.ts | 14 +++++------ .../ui/components/UserProfile/EmailForm.tsx | 25 +++++++++++-------- .../VerifyWithEnterpriseConnection.tsx | 1 - .../src/ui/components/UserProfile/utils.ts | 14 +---------- packages/types/src/emailAddress.ts | 6 ++--- packages/types/src/json.ts | 2 +- packages/types/src/verification.ts | 2 +- 7 files changed, 28 insertions(+), 36 deletions(-) diff --git a/packages/clerk-js/src/core/resources/EmailAddress.ts b/packages/clerk-js/src/core/resources/EmailAddress.ts index ea16f8090e..1016d26c0c 100644 --- a/packages/clerk-js/src/core/resources/EmailAddress.ts +++ b/packages/clerk-js/src/core/resources/EmailAddress.ts @@ -2,13 +2,13 @@ import { Poller } from '@clerk/shared/poller'; import type { AttemptEmailAddressVerificationParams, CreateEmailLinkFlowReturn, - CreateEnterpriseConnectionLinkFlowReturn, + CreateEnterpriseSsoLinkFlowReturn, EmailAddressJSON, EmailAddressResource, IdentificationLinkResource, PrepareEmailAddressVerificationParams, StartEmailLinkFlowParams, - StartEnterpriseConnectionLinkFlowParams, + StartEnterpriseSsoLinkFlowParams, VerificationResource, } from '@clerk/types'; @@ -18,7 +18,7 @@ import { BaseResource, IdentificationLink, Verification } from './internal'; export class EmailAddress extends BaseResource implements EmailAddressResource { id!: string; emailAddress = ''; - matchesEnterpriseConnection = false; + hasEnterpriseSso = false; linkedTo: IdentificationLinkResource[] = []; verification!: VerificationResource; @@ -80,15 +80,15 @@ export class EmailAddress extends BaseResource implements EmailAddressResource { return { startEmailLinkFlow, cancelEmailLinkFlow: stop }; }; - createEnterpriseSsoLinkFlow = (): CreateEnterpriseConnectionLinkFlowReturn< - StartEnterpriseConnectionLinkFlowParams, + createEnterpriseSsoLinkFlow = (): CreateEnterpriseSsoLinkFlowReturn< + StartEnterpriseSsoLinkFlowParams, EmailAddressResource > => { const { run, stop } = Poller(); const startEnterpriseSsoLinkFlow = async ({ redirectUrl, - }: StartEnterpriseConnectionLinkFlowParams): Promise => { + }: StartEnterpriseSsoLinkFlowParams): Promise => { if (!this.id) { clerkVerifyEmailAddressCalledBeforeCreate('SignUp'); } @@ -131,7 +131,7 @@ export class EmailAddress extends BaseResource implements EmailAddressResource { this.id = data.id; this.emailAddress = data.email_address; this.verification = new Verification(data.verification); - this.matchesEnterpriseConnection = data.matches_enterprise_connection; + this.hasEnterpriseSso = data.has_enterprise_sso; this.linkedTo = (data.linked_to || []).map(link => new IdentificationLink(link)); return this; } diff --git a/packages/clerk-js/src/ui/components/UserProfile/EmailForm.tsx b/packages/clerk-js/src/ui/components/UserProfile/EmailForm.tsx index 457612f36c..cb21bce78e 100644 --- a/packages/clerk-js/src/ui/components/UserProfile/EmailForm.tsx +++ b/packages/clerk-js/src/ui/components/UserProfile/EmailForm.tsx @@ -1,5 +1,5 @@ import { useReverification, useUser } from '@clerk/shared/react'; -import type { EmailAddressResource, PrepareEmailAddressVerificationParams } from '@clerk/types'; +import type { EmailAddressResource, EnvironmentResource, PrepareEmailAddressVerificationParams } from '@clerk/types'; import React from 'react'; import { useWizard, Wizard } from '../../common'; @@ -8,7 +8,6 @@ import { localizationKeys } from '../../customizables'; import type { FormProps } from '../../elements'; import { Form, FormButtons, FormContainer, useCardState, withCardStateProvider } from '../../elements'; import { handleError, useFormControl } from '../../utils'; -import { emailLinksEnabledForInstance } from './utils'; import { VerifyWithCode } from './VerifyWithCode'; import { VerifyWithEnterpriseConnection } from './VerifyWithEnterpriseConnection'; import { VerifyWithLink } from './VerifyWithLink'; @@ -22,9 +21,11 @@ export const EmailForm = withCardStateProvider((props: EmailFormProps) => { const { user } = useUser(); const environment = useEnvironment(); const preferEmailLinks = emailLinksEnabledForInstance(environment); + const [createEmailAddress] = useReverification(() => user?.createEmailAddress({ email: emailField.value })); + const emailAddressRef = React.useRef(user?.emailAddresses.find(a => a.id === id)); - const strategy = getEmailAddressVerificationStrategy(emailAddressRef.current, preferEmailLinks); + const strategy = getEmailAddressVerificationStrategy(emailAddressRef.current, environment); const wizard = useWizard({ defaultStep: emailAddressRef.current ? 1 : 0, onNextStep: () => card.setError(undefined), @@ -80,8 +81,7 @@ export const EmailForm = withCardStateProvider((props: EmailFormProps) => { { ); }); -// TODO - Handle `preferEmailLinks` -export const getEmailAddressVerificationStrategy = ( +function emailLinksEnabledForInstance(env: EnvironmentResource): boolean { + const { userSettings } = env; + const { email_address } = userSettings.attributes; + return email_address.enabled && email_address.verifications.includes('email_link'); +} + +const getEmailAddressVerificationStrategy = ( emailAddress: EmailAddressResource | undefined, - preferLinks: boolean, + env: EnvironmentResource, ): PrepareEmailAddressVerificationParams['strategy'] => { - if (emailAddress?.matchesEnterpriseConnection) { + if (emailAddress?.hasEnterpriseSso) { return 'enterprise_sso'; } - return preferLinks ? 'email_link' : 'email_code'; + return emailLinksEnabledForInstance(env) ? 'email_link' : 'email_code'; }; diff --git a/packages/clerk-js/src/ui/components/UserProfile/VerifyWithEnterpriseConnection.tsx b/packages/clerk-js/src/ui/components/UserProfile/VerifyWithEnterpriseConnection.tsx index c5ea328f03..7572cd51e3 100644 --- a/packages/clerk-js/src/ui/components/UserProfile/VerifyWithEnterpriseConnection.tsx +++ b/packages/clerk-js/src/ui/components/UserProfile/VerifyWithEnterpriseConnection.tsx @@ -44,7 +44,6 @@ export const VerifyWithEnterpriseConnection = (props: VerifyWithEnterpriseConnec return ( <> - {/* TODO - Handle localization */} (a: IDable) => (a.id === id ? export const defaultFirst = (a: PhoneNumberResource) => (a.defaultSecondFactor ? -1 : 1); -export function emailLinksEnabledForInstance(env: EnvironmentResource): boolean { - const { userSettings } = env; - const { email_address } = userSettings.attributes; - return email_address.enabled && email_address.verifications.includes('email_link'); -} - export function getSecondFactors(attributes: Attributes): string[] { const secondFactors: string[] = []; diff --git a/packages/types/src/emailAddress.ts b/packages/types/src/emailAddress.ts index e33b3d0ab5..8e14663269 100644 --- a/packages/types/src/emailAddress.ts +++ b/packages/types/src/emailAddress.ts @@ -3,7 +3,7 @@ import type { ClerkResource } from './resource'; import type { EmailCodeStrategy, EmailLinkStrategy, EnterpriseSSOStrategy } from './strategies'; import type { CreateEmailLinkFlowReturn, - CreateEnterpriseSsoFlowReturn, + CreateEnterpriseSsoLinkFlowReturn, StartEmailLinkFlowParams, StartEnterpriseSsoLinkFlowParams, VerificationResource, @@ -30,13 +30,13 @@ export interface EmailAddressResource extends ClerkResource { id: string; emailAddress: string; verification: VerificationResource; - matchesEnterpriseConnection: boolean; + hasEnterpriseSso: boolean; linkedTo: IdentificationLinkResource[]; toString: () => string; prepareVerification: (params: PrepareEmailAddressVerificationParams) => Promise; attemptVerification: (params: AttemptEmailAddressVerificationParams) => Promise; createEmailLinkFlow: () => CreateEmailLinkFlowReturn; - createEnterpriseSsoLinkFlow: () => CreateEnterpriseSsoFlowReturn< + createEnterpriseSsoLinkFlow: () => CreateEnterpriseSsoLinkFlowReturn< StartEnterpriseSsoLinkFlowParams, EmailAddressResource >; diff --git a/packages/types/src/json.ts b/packages/types/src/json.ts index 8c8943b9e8..32be1c8d38 100644 --- a/packages/types/src/json.ts +++ b/packages/types/src/json.ts @@ -139,7 +139,7 @@ export interface EmailAddressJSON extends ClerkResourceJSON { email_address: string; verification: VerificationJSON | null; linked_to: IdentificationLinkJSON[]; - matches_enterprise_connection: boolean; + has_enterprise_sso: boolean; } export interface IdentificationLinkJSON extends ClerkResourceJSON { diff --git a/packages/types/src/verification.ts b/packages/types/src/verification.ts index de5152d47e..d998a892e3 100644 --- a/packages/types/src/verification.ts +++ b/packages/types/src/verification.ts @@ -46,7 +46,7 @@ export interface StartEnterpriseSsoLinkFlowParams { redirectUrl: string; } -export type CreateEnterpriseSsoFlowReturn = { +export type CreateEnterpriseSsoLinkFlowReturn = { startEnterpriseSsoLinkFlow: (params: Params) => Promise; cancelEnterpriseSsoLinkFlow: () => void; };