Skip to content

Commit

Permalink
Rename matchesEnterpriseConnection to hasEnterpriseSso
Browse files Browse the repository at this point in the history
  • Loading branch information
LauraBeatris committed Dec 3, 2024
1 parent 7ed4e28 commit ccd2711
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 35 deletions.
14 changes: 7 additions & 7 deletions packages/clerk-js/src/core/resources/EmailAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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;

Expand Down Expand Up @@ -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<EmailAddressResource> => {
}: StartEnterpriseSsoLinkFlowParams): Promise<EmailAddressResource> => {
if (!this.id) {
clerkVerifyEmailAddressCalledBeforeCreate('SignUp');
}
Expand Down Expand Up @@ -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;
}
Expand Down
25 changes: 15 additions & 10 deletions packages/clerk-js/src/ui/components/UserProfile/EmailForm.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand All @@ -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<EmailAddressResource | undefined>(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),
Expand Down Expand Up @@ -80,8 +81,7 @@ export const EmailForm = withCardStateProvider((props: EmailFormProps) => {
<FormContainer
headerTitle={localizationKeys('userProfile.emailAddressPage.verifyTitle')}
headerSubtitle={
// TODO - Get localization per strategy
preferEmailLinks
strategy === 'enterprise_sso' || preferEmailLinks
? localizationKeys('userProfile.emailAddressPage.emailLink.formSubtitle', {
identifier: emailAddressRef.current?.emailAddress,
})
Expand Down Expand Up @@ -118,14 +118,19 @@ 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';
};
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export const VerifyWithEnterpriseConnection = (props: VerifyWithEnterpriseConnec

return (
<>
{/* TODO - Handle localization */}
<VerificationLink
resendButton={localizationKeys('userProfile.emailAddressPage.emailLink.resendButton')}
onResendCodeClicked={startVerification}
Expand Down
14 changes: 1 addition & 13 deletions packages/clerk-js/src/ui/components/UserProfile/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import type {
Attributes,
EmailAddressResource,
EnvironmentResource,
PhoneNumberResource,
UserResource,
} from '@clerk/types';
import type { Attributes, EmailAddressResource, PhoneNumberResource, UserResource } from '@clerk/types';

type IDable = { id: string };

Expand All @@ -16,12 +10,6 @@ export const currentSessionFirst = (id: string) => (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[] = [];

Expand Down
6 changes: 3 additions & 3 deletions packages/types/src/emailAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ClerkResource } from './resource';
import type { EmailCodeStrategy, EmailLinkStrategy, EnterpriseSSOStrategy } from './strategies';
import type {
CreateEmailLinkFlowReturn,
CreateEnterpriseSsoFlowReturn,
CreateEnterpriseSsoLinkFlowReturn,
StartEmailLinkFlowParams,
StartEnterpriseSsoLinkFlowParams,
VerificationResource,
Expand All @@ -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<EmailAddressResource>;
attemptVerification: (params: AttemptEmailAddressVerificationParams) => Promise<EmailAddressResource>;
createEmailLinkFlow: () => CreateEmailLinkFlowReturn<StartEmailLinkFlowParams, EmailAddressResource>;
createEnterpriseSsoLinkFlow: () => CreateEnterpriseSsoFlowReturn<
createEnterpriseSsoLinkFlow: () => CreateEnterpriseSsoLinkFlowReturn<
StartEnterpriseSsoLinkFlowParams,
EmailAddressResource
>;
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface StartEnterpriseSsoLinkFlowParams {
redirectUrl: string;
}

export type CreateEnterpriseSsoFlowReturn<Params, Resource> = {
export type CreateEnterpriseSsoLinkFlowReturn<Params, Resource> = {
startEnterpriseSsoLinkFlow: (params: Params) => Promise<Resource>;
cancelEnterpriseSsoLinkFlow: () => void;
};

0 comments on commit ccd2711

Please sign in to comment.