From fbebf8fb8b90023d27218e0ca946da726819f9b8 Mon Sep 17 00:00:00 2001 From: panteliselef Date: Fri, 30 Aug 2024 14:29:44 +0300 Subject: [PATCH 1/2] chore(clerk-js, types): Move session verification under SessionResource --- .changeset/light-trees-battle.md | 12 ++ .../clerk-js/src/core/resources/Session.ts | 103 ++++++++++++++++++ packages/clerk-js/src/core/resources/User.ts | 103 ------------------ .../UserVerification/UVFactorOneCodeForm.tsx | 14 +-- .../UVFactorTwoBackupCodeCard.tsx | 8 +- .../UserVerification/UVFactorTwoCodeForm.tsx | 10 +- .../UVFactorTwoPhoneCodeCard.tsx | 6 +- .../UserVerificationFactorOnePassword.tsx | 8 +- .../useUserVerificationSession.tsx | 6 +- packages/types/src/session.ts | 45 ++++++++ packages/types/src/user.ts | 44 -------- 11 files changed, 186 insertions(+), 173 deletions(-) create mode 100644 .changeset/light-trees-battle.md diff --git a/.changeset/light-trees-battle.md b/.changeset/light-trees-battle.md new file mode 100644 index 00000000000..17330312e40 --- /dev/null +++ b/.changeset/light-trees-battle.md @@ -0,0 +1,12 @@ +--- +"@clerk/clerk-js": minor +"@clerk/types": minor +--- + +Move SessionVerification methods from UserResource to SessionResource: + +- `user.__experimental_verifySession` -> `session.__experimental_startVerification` +- `user.__experimental_verifySessionPrepareFirstFactor` -> `session.__experimental_prepareFirstFactorVerification` +- `user.__experimental_verifySessionAttemptFirstFactor` -> `session.__experimental_attemptFirstFactorVerification` +- `user.__experimental_verifySessionPrepareSecondFactor` -> `session.__experimental_prepareSecondFactorVerification` +- `user.__experimental_verifySessionAttemptSecondFactor` -> `session.__experimental_attemptSecondFactorVerification` diff --git a/packages/clerk-js/src/core/resources/Session.ts b/packages/clerk-js/src/core/resources/Session.ts index 1600c8d7649..82a1fe30cf9 100644 --- a/packages/clerk-js/src/core/resources/Session.ts +++ b/packages/clerk-js/src/core/resources/Session.ts @@ -1,10 +1,19 @@ import { runWithExponentialBackOff } from '@clerk/shared'; import { is4xxError } from '@clerk/shared/error'; import type { + __experimental_SessionVerificationJSON, + __experimental_SessionVerificationResource, + __experimental_SessionVerifyAttemptFirstFactorParams, + __experimental_SessionVerifyAttemptSecondFactorParams, + __experimental_SessionVerifyCreateParams, + __experimental_SessionVerifyPrepareFirstFactorParams, + __experimental_SessionVerifyPrepareSecondFactorParams, ActJWTClaim, CheckAuthorization, + EmailCodeConfig, GetToken, GetTokenOptions, + PhoneCodeConfig, SessionJSON, SessionResource, SessionStatus, @@ -13,9 +22,11 @@ import type { } from '@clerk/types'; import { unixEpochToDate } from '../../utils/date'; +import { clerkInvalidStrategy } from '../errors'; import { eventBus, events } from '../events'; import { SessionTokenCache } from '../tokenCache'; import { BaseResource, PublicUserData, Token, User } from './internal'; +import { SessionVerification } from './SessionVerification'; export class Session extends BaseResource implements SessionResource { pathRoot = '/client/sessions'; @@ -123,6 +134,98 @@ export class Session extends BaseResource implements SessionResource { return [this.id, template, resolvedOrganizationId, this.updatedAt.getTime()].filter(Boolean).join('-'); } + __experimental_startVerification = async ({ + level, + maxAge, + }: __experimental_SessionVerifyCreateParams): Promise<__experimental_SessionVerificationResource> => { + const json = ( + await BaseResource._fetch({ + method: 'POST', + path: `/client/sessions/${this.id}/verify`, + body: { + level, + maxAge, + } as any, + }) + )?.response as unknown as __experimental_SessionVerificationJSON; + + return new SessionVerification(json); + }; + + __experimental_prepareFirstFactorVerification = async ( + factor: __experimental_SessionVerifyPrepareFirstFactorParams, + ): Promise<__experimental_SessionVerificationResource> => { + let config; + switch (factor.strategy) { + case 'email_code': + config = { emailAddressId: factor.emailAddressId } as EmailCodeConfig; + break; + case 'phone_code': + config = { + phoneNumberId: factor.phoneNumberId, + default: factor.default, + } as PhoneCodeConfig; + break; + default: + clerkInvalidStrategy('Session.prepareFirstFactorVerification', (factor as any).strategy); + } + + const json = ( + await BaseResource._fetch({ + method: 'POST', + path: `/client/sessions/${this.id}/verify/prepare_first_factor`, + body: { + ...config, + strategy: factor.strategy, + } as any, + }) + )?.response as unknown as __experimental_SessionVerificationJSON; + + return new SessionVerification(json); + }; + + __experimental_attemptFirstFactorVerification = async ( + attemptFactor: __experimental_SessionVerifyAttemptFirstFactorParams, + ): Promise<__experimental_SessionVerificationResource> => { + const json = ( + await BaseResource._fetch({ + method: 'POST', + path: `/client/sessions/${this.id}/verify/attempt_first_factor`, + body: { ...attemptFactor, strategy: attemptFactor.strategy } as any, + }) + )?.response as unknown as __experimental_SessionVerificationJSON; + + return new SessionVerification(json); + }; + + __experimental_prepareSecondFactorVerification = async ( + params: __experimental_SessionVerifyPrepareSecondFactorParams, + ): Promise<__experimental_SessionVerificationResource> => { + const json = ( + await BaseResource._fetch({ + method: 'POST', + path: `/client/sessions/${this.id}/verify/prepare_second_factor`, + body: params as any, + }) + )?.response as unknown as __experimental_SessionVerificationJSON; + + return new SessionVerification(json); + }; + + __experimental_attemptSecondFactorVerification = async ( + params: __experimental_SessionVerifyAttemptSecondFactorParams, + ): Promise<__experimental_SessionVerificationResource> => { + const json = ( + await BaseResource._fetch({ + method: 'POST', + path: `/client/sessions/${this.id}/verify/attempt_second_factor`, + body: params as any, + }) + )?.response as unknown as __experimental_SessionVerificationJSON; + + return new SessionVerification(json); + }; + protected fromJSON(data: SessionJSON | null): this { if (!data) { return this; diff --git a/packages/clerk-js/src/core/resources/User.ts b/packages/clerk-js/src/core/resources/User.ts index e30b96ecc7f..7fe40eda9c6 100644 --- a/packages/clerk-js/src/core/resources/User.ts +++ b/packages/clerk-js/src/core/resources/User.ts @@ -1,11 +1,4 @@ import type { - __experimental_SessionVerificationJSON, - __experimental_SessionVerificationResource, - __experimental_SessionVerifyAttemptFirstFactorParams, - __experimental_SessionVerifyAttemptSecondFactorParams, - __experimental_SessionVerifyCreateParams, - __experimental_SessionVerifyPrepareFirstFactorParams, - __experimental_SessionVerifyPrepareSecondFactorParams, BackupCodeJSON, BackupCodeResource, CreateEmailAddressParams, @@ -15,7 +8,6 @@ import type { DeletedObjectJSON, DeletedObjectResource, EmailAddressResource, - EmailCodeConfig, ExternalAccountJSON, ExternalAccountResource, GetOrganizationMemberships, @@ -24,7 +16,6 @@ import type { ImageResource, OrganizationMembershipResource, PasskeyResource, - PhoneCodeConfig, PhoneNumberResource, RemoveUserPasswordParams, SamlAccountResource, @@ -42,7 +33,6 @@ import type { import { unixEpochToDate } from '../../utils/date'; import { normalizeUnsafeMetadata } from '../../utils/resourceParams'; import { getFullName } from '../../utils/user'; -import { clerkInvalidStrategy } from '../errors'; import { BackupCode } from './BackupCode'; import { BaseResource, @@ -60,7 +50,6 @@ import { UserOrganizationInvitation, Web3Wallet, } from './internal'; -import { SessionVerification } from './SessionVerification'; export class User extends BaseResource implements UserResource { pathRoot = '/me'; @@ -250,98 +239,6 @@ export class User extends BaseResource implements UserResource { return this._baseDelete({ path: '/me' }); }; - __experimental_verifySession = async ({ - level, - maxAge, - }: __experimental_SessionVerifyCreateParams): Promise<__experimental_SessionVerificationResource> => { - const json = ( - await BaseResource._fetch({ - method: 'POST', - path: `/me/sessions/verify`, - body: { - level, - maxAge, - } as any, - }) - )?.response as unknown as __experimental_SessionVerificationJSON; - - return new SessionVerification(json); - }; - - __experimental_verifySessionPrepareFirstFactor = async ( - factor: __experimental_SessionVerifyPrepareFirstFactorParams, - ): Promise<__experimental_SessionVerificationResource> => { - let config; - switch (factor.strategy) { - case 'email_code': - config = { emailAddressId: factor.emailAddressId } as EmailCodeConfig; - break; - case 'phone_code': - config = { - phoneNumberId: factor.phoneNumberId, - default: factor.default, - } as PhoneCodeConfig; - break; - default: - clerkInvalidStrategy('User.verifySessionPrepareFirstFactor', (factor as any).strategy); - } - - const json = ( - await BaseResource._fetch({ - method: 'POST', - path: `/me/sessions/verify/prepare_first_factor`, - body: { - ...config, - strategy: factor.strategy, - } as any, - }) - )?.response as unknown as __experimental_SessionVerificationJSON; - - return new SessionVerification(json); - }; - - __experimental_verifySessionAttemptFirstFactor = async ( - attemptFactor: __experimental_SessionVerifyAttemptFirstFactorParams, - ): Promise<__experimental_SessionVerificationResource> => { - const json = ( - await BaseResource._fetch({ - method: 'POST', - path: `/me/sessions/verify/attempt_first_factor`, - body: { ...attemptFactor, strategy: attemptFactor.strategy } as any, - }) - )?.response as unknown as __experimental_SessionVerificationJSON; - - return new SessionVerification(json); - }; - - __experimental_verifySessionPrepareSecondFactor = async ( - params: __experimental_SessionVerifyPrepareSecondFactorParams, - ): Promise<__experimental_SessionVerificationResource> => { - const json = ( - await BaseResource._fetch({ - method: 'POST', - path: `/me/sessions/verify/prepare_second_factor`, - body: params as any, - }) - )?.response as unknown as __experimental_SessionVerificationJSON; - - return new SessionVerification(json); - }; - - __experimental_verifySessionAttemptSecondFactor = async ( - params: __experimental_SessionVerifyAttemptSecondFactorParams, - ): Promise<__experimental_SessionVerificationResource> => { - const json = ( - await BaseResource._fetch({ - method: 'POST', - path: `/me/sessions/verify/attempt_second_factor`, - body: params as any, - }) - )?.response as unknown as __experimental_SessionVerificationJSON; - - return new SessionVerification(json); - }; - getSessions = async (): Promise => { if (this.cachedSessionsWithActivities) { return this.cachedSessionsWithActivities; diff --git a/packages/clerk-js/src/ui/components/UserVerification/UVFactorOneCodeForm.tsx b/packages/clerk-js/src/ui/components/UserVerification/UVFactorOneCodeForm.tsx index 1e4cf289af8..c9091fd9fcf 100644 --- a/packages/clerk-js/src/ui/components/UserVerification/UVFactorOneCodeForm.tsx +++ b/packages/clerk-js/src/ui/components/UserVerification/UVFactorOneCodeForm.tsx @@ -1,4 +1,4 @@ -import { useUser } from '@clerk/shared/react'; +import { useSession } from '@clerk/shared/react'; import type { EmailCodeFactor, PhoneCodeFactor } from '@clerk/types'; import React from 'react'; @@ -25,7 +25,7 @@ export type UVFactorOneCodeFormProps = UVFactorOneCodeCard & { }; export const UVFactorOneCodeForm = (props: UVFactorOneCodeFormProps) => { - const { user } = useUser(); + const { session } = useSession(); const card = useCardState(); const { handleVerificationResponse } = useAfterVerification(); @@ -37,15 +37,15 @@ export const UVFactorOneCodeForm = (props: UVFactorOneCodeFormProps) => { }, []); const prepare = () => { - void user! - .__experimental_verifySessionPrepareFirstFactor(props.factor) + void session! + .__experimental_prepareFirstFactorVerification(props.factor) .then(() => props.onFactorPrepare()) .catch(err => handleError(err, [], card.setError)); }; const action: VerificationCodeCardProps['onCodeEntryFinishedAction'] = (code, resolve, reject) => { - user! - .__experimental_verifySessionAttemptFirstFactor({ strategy: props.factor.strategy, code }) + session! + .__experimental_attemptFirstFactorVerification({ strategy: props.factor.strategy, code }) .then(async res => { await resolve(); return handleVerificationResponse(res); @@ -62,7 +62,7 @@ export const UVFactorOneCodeForm = (props: UVFactorOneCodeFormProps) => { onCodeEntryFinishedAction={action} onResendCodeClicked={prepare} safeIdentifier={props.factor.safeIdentifier} - profileImageUrl={user?.imageUrl} + profileImageUrl={session?.user?.imageUrl} onShowAlternativeMethodsClicked={props.onShowAlternativeMethodsClicked} showAlternativeMethods={props.showAlternativeMethods} onBackLinkClicked={props.onBackLinkClicked} diff --git a/packages/clerk-js/src/ui/components/UserVerification/UVFactorTwoBackupCodeCard.tsx b/packages/clerk-js/src/ui/components/UserVerification/UVFactorTwoBackupCodeCard.tsx index a9248708659..d414981b93f 100644 --- a/packages/clerk-js/src/ui/components/UserVerification/UVFactorTwoBackupCodeCard.tsx +++ b/packages/clerk-js/src/ui/components/UserVerification/UVFactorTwoBackupCodeCard.tsx @@ -1,4 +1,4 @@ -import { useUser } from '@clerk/shared/react'; +import { useSession } from '@clerk/shared/react'; import React from 'react'; import { Col, descriptors, localizationKeys } from '../../customizables'; @@ -12,7 +12,7 @@ type UVFactorTwoBackupCodeCardProps = { export const UVFactorTwoBackupCodeCard = (props: UVFactorTwoBackupCodeCardProps) => { const { onShowAlternativeMethodsClicked } = props; - const { user } = useUser(); + const { session } = useSession(); const { handleVerificationResponse } = useAfterVerification(); const card = useCardState(); @@ -24,8 +24,8 @@ export const UVFactorTwoBackupCodeCard = (props: UVFactorTwoBackupCodeCardProps) const handleBackupCodeSubmit: React.FormEventHandler = e => { e.preventDefault(); - return user! - .__experimental_verifySessionAttemptSecondFactor({ strategy: 'backup_code', code: codeControl.value }) + return session! + .__experimental_attemptSecondFactorVerification({ strategy: 'backup_code', code: codeControl.value }) .then(handleVerificationResponse) .catch(err => handleError(err, [codeControl], card.setError)); }; diff --git a/packages/clerk-js/src/ui/components/UserVerification/UVFactorTwoCodeForm.tsx b/packages/clerk-js/src/ui/components/UserVerification/UVFactorTwoCodeForm.tsx index 6a8e3fe47ba..6191100d5ee 100644 --- a/packages/clerk-js/src/ui/components/UserVerification/UVFactorTwoCodeForm.tsx +++ b/packages/clerk-js/src/ui/components/UserVerification/UVFactorTwoCodeForm.tsx @@ -1,4 +1,4 @@ -import { useUser } from '@clerk/shared/react'; +import { useSession } from '@clerk/shared/react'; import type { __experimental_SessionVerificationResource, PhoneCodeFactor, TOTPFactor } from '@clerk/types'; import React from 'react'; @@ -24,7 +24,7 @@ type SignInFactorTwoCodeFormProps = UVFactorTwoCodeCard & { export const UVFactorTwoCodeForm = (props: SignInFactorTwoCodeFormProps) => { const card = useCardState(); - const { user } = useUser(); + const { session } = useSession(); const { handleVerificationResponse } = useAfterVerification(); React.useEffect(() => { @@ -44,8 +44,8 @@ export const UVFactorTwoCodeForm = (props: SignInFactorTwoCodeFormProps) => { : undefined; const action: VerificationCodeCardProps['onCodeEntryFinishedAction'] = (code, resolve, reject) => { - user! - .__experimental_verifySessionAttemptSecondFactor({ strategy: props.factor.strategy, code }) + session! + .__experimental_attemptSecondFactorVerification({ strategy: props.factor.strategy, code }) .then(async res => { await resolve(); return handleVerificationResponse(res); @@ -62,7 +62,7 @@ export const UVFactorTwoCodeForm = (props: SignInFactorTwoCodeFormProps) => { onCodeEntryFinishedAction={action} onResendCodeClicked={prepare} safeIdentifier={'safeIdentifier' in props.factor ? props.factor.safeIdentifier : undefined} - profileImageUrl={user!.imageUrl} + profileImageUrl={session?.user?.imageUrl} onShowAlternativeMethodsClicked={props.onShowAlternativeMethodsClicked} /> ); diff --git a/packages/clerk-js/src/ui/components/UserVerification/UVFactorTwoPhoneCodeCard.tsx b/packages/clerk-js/src/ui/components/UserVerification/UVFactorTwoPhoneCodeCard.tsx index aa133de1b71..162c3c21075 100644 --- a/packages/clerk-js/src/ui/components/UserVerification/UVFactorTwoPhoneCodeCard.tsx +++ b/packages/clerk-js/src/ui/components/UserVerification/UVFactorTwoPhoneCodeCard.tsx @@ -1,4 +1,4 @@ -import { useUser } from '@clerk/shared/react'; +import { useSession } from '@clerk/shared/react'; import type { PhoneCodeFactor } from '@clerk/types'; import { Flow, localizationKeys } from '../../customizables'; @@ -8,11 +8,11 @@ import { UVFactorTwoCodeForm } from './UVFactorTwoCodeForm'; type UVFactorTwoPhoneCodeCardProps = UVFactorTwoCodeCard & { factor: PhoneCodeFactor }; export const UVFactorTwoPhoneCodeCard = (props: UVFactorTwoPhoneCodeCardProps) => { - const { user } = useUser(); + const { session } = useSession(); const prepare = () => { const { phoneNumberId, strategy } = props.factor; - return user!.__experimental_verifySessionPrepareSecondFactor({ phoneNumberId, strategy }); + return session!.__experimental_prepareSecondFactorVerification({ phoneNumberId, strategy }); }; return ( diff --git a/packages/clerk-js/src/ui/components/UserVerification/UserVerificationFactorOnePassword.tsx b/packages/clerk-js/src/ui/components/UserVerification/UserVerificationFactorOnePassword.tsx index d4783d5725d..1345a853a10 100644 --- a/packages/clerk-js/src/ui/components/UserVerification/UserVerificationFactorOnePassword.tsx +++ b/packages/clerk-js/src/ui/components/UserVerification/UserVerificationFactorOnePassword.tsx @@ -1,4 +1,4 @@ -import { useUser } from '@clerk/shared/react'; +import { useSession } from '@clerk/shared/react'; import React from 'react'; import { Col, descriptors, Flow, localizationKeys } from '../../customizables'; @@ -13,7 +13,7 @@ type UserVerificationFactorOnePasswordProps = { export function UserVerificationFactorOnePasswordCard(props: UserVerificationFactorOnePasswordProps): JSX.Element { const { onShowAlternativeMethodsClick } = props; - const { user } = useUser(); + const { session } = useSession(); const { handleVerificationResponse } = useAfterVerification(); const card = useCardState(); @@ -29,8 +29,8 @@ export function UserVerificationFactorOnePasswordCard(props: UserVerificationFac const handlePasswordSubmit: React.FormEventHandler = async e => { e.preventDefault(); - return user - ?.__experimental_verifySessionAttemptFirstFactor({ + return session + ?.__experimental_attemptFirstFactorVerification({ strategy: 'password', password: passwordControl.value, }) diff --git a/packages/clerk-js/src/ui/components/UserVerification/useUserVerificationSession.tsx b/packages/clerk-js/src/ui/components/UserVerification/useUserVerificationSession.tsx index 1cd1b26a38b..eafc6775a91 100644 --- a/packages/clerk-js/src/ui/components/UserVerification/useUserVerificationSession.tsx +++ b/packages/clerk-js/src/ui/components/UserVerification/useUserVerificationSession.tsx @@ -1,14 +1,14 @@ -import { useUser } from '@clerk/shared/react'; +import { useSession } from '@clerk/shared/react'; import { useUserVerification } from '../../contexts'; import { LoadingCard } from '../../elements'; import { useFetch } from '../../hooks'; const useUserVerificationSession = () => { - const { user } = useUser(); + const { session } = useSession(); const { level } = useUserVerification(); const data = useFetch( - user ? user.__experimental_verifySession : undefined, + session ? session.__experimental_startVerification : undefined, { level: level || 'L2.secondFactor', // TODO(STEP-UP): Figure out if this needs to be a prop diff --git a/packages/types/src/session.ts b/packages/types/src/session.ts index b77a0492c43..b1ecc743846 100644 --- a/packages/types/src/session.ts +++ b/packages/types/src/session.ts @@ -1,3 +1,13 @@ +import type { + BackupCodeAttempt, + EmailCodeAttempt, + EmailCodeConfig, + PasswordAttempt, + PhoneCodeAttempt, + PhoneCodeConfig, + PhoneCodeSecondFactorConfig, + TOTPAttempt, +} from './factors'; import type { ActJWTClaim } from './jwt'; import type { OrganizationCustomPermissionKey, @@ -5,6 +15,11 @@ import type { OrganizationPermissionKey, } from './organizationMembership'; import type { ClerkResource } from './resource'; +import type { + __experimental_SessionVerificationLevel, + __experimental_SessionVerificationMaxAge, + __experimental_SessionVerificationResource, +} from './sessionVerification'; import type { TokenResource } from './token'; import type { UserResource } from './user'; @@ -54,6 +69,22 @@ export interface SessionResource extends ClerkResource { clearCache: () => void; createdAt: Date; updatedAt: Date; + + __experimental_startVerification: ( + params: __experimental_SessionVerifyCreateParams, + ) => Promise<__experimental_SessionVerificationResource>; + __experimental_prepareFirstFactorVerification: ( + factor: __experimental_SessionVerifyPrepareFirstFactorParams, + ) => Promise<__experimental_SessionVerificationResource>; + __experimental_attemptFirstFactorVerification: ( + attemptFactor: __experimental_SessionVerifyAttemptFirstFactorParams, + ) => Promise<__experimental_SessionVerificationResource>; + __experimental_prepareSecondFactorVerification: ( + params: __experimental_SessionVerifyPrepareSecondFactorParams, + ) => Promise<__experimental_SessionVerificationResource>; + __experimental_attemptSecondFactorVerification: ( + params: __experimental_SessionVerifyAttemptSecondFactorParams, + ) => Promise<__experimental_SessionVerificationResource>; } export interface ActiveSessionResource extends SessionResource { @@ -102,3 +133,17 @@ export type GetTokenOptions = { skipCache?: boolean; }; export type GetToken = (options?: GetTokenOptions) => Promise; + +export type __experimental_SessionVerifyCreateParams = { + level: __experimental_SessionVerificationLevel; + maxAge: __experimental_SessionVerificationMaxAge; +}; + +export type __experimental_SessionVerifyPrepareFirstFactorParams = EmailCodeConfig | PhoneCodeConfig; +export type __experimental_SessionVerifyAttemptFirstFactorParams = + | EmailCodeAttempt + | PhoneCodeAttempt + | PasswordAttempt; + +export type __experimental_SessionVerifyPrepareSecondFactorParams = PhoneCodeSecondFactorConfig; +export type __experimental_SessionVerifyAttemptSecondFactorParams = PhoneCodeAttempt | TOTPAttempt | BackupCodeAttempt; diff --git a/packages/types/src/user.ts b/packages/types/src/user.ts index 588efc7f145..d45359d6342 100644 --- a/packages/types/src/user.ts +++ b/packages/types/src/user.ts @@ -2,16 +2,6 @@ import type { BackupCodeResource } from './backupCode'; import type { DeletedObjectResource } from './deletedObject'; import type { EmailAddressResource } from './emailAddress'; import type { ExternalAccountResource } from './externalAccount'; -import type { - BackupCodeAttempt, - EmailCodeAttempt, - EmailCodeConfig, - PasswordAttempt, - PhoneCodeAttempt, - PhoneCodeConfig, - PhoneCodeSecondFactorConfig, - TOTPAttempt, -} from './factors'; import type { ImageResource } from './image'; import type { UserJSON } from './json'; import type { OAuthScope } from './oauth'; @@ -24,11 +14,6 @@ import type { PhoneNumberResource } from './phoneNumber'; import type { ClerkResource } from './resource'; import type { SamlAccountResource } from './samlAccount'; import type { SessionWithActivitiesResource } from './session'; -import type { - __experimental_SessionVerificationLevel, - __experimental_SessionVerificationMaxAge, - __experimental_SessionVerificationResource, -} from './sessionVerification'; import type { OAuthStrategy } from './strategies'; import type { TOTPResource } from './totp'; import type { UserOrganizationInvitationResource } from './userOrganizationInvitation'; @@ -124,21 +109,6 @@ export interface UserResource extends ClerkResource { verifyTOTP: (params: VerifyTOTPParams) => Promise; disableTOTP: () => Promise; createBackupCode: () => Promise; - __experimental_verifySession: ( - params: __experimental_SessionVerifyCreateParams, - ) => Promise<__experimental_SessionVerificationResource>; - __experimental_verifySessionPrepareFirstFactor: ( - factor: __experimental_SessionVerifyPrepareFirstFactorParams, - ) => Promise<__experimental_SessionVerificationResource>; - __experimental_verifySessionAttemptFirstFactor: ( - attemptFactor: __experimental_SessionVerifyAttemptFirstFactorParams, - ) => Promise<__experimental_SessionVerificationResource>; - __experimental_verifySessionPrepareSecondFactor: ( - params: __experimental_SessionVerifyPrepareSecondFactorParams, - ) => Promise<__experimental_SessionVerificationResource>; - __experimental_verifySessionAttemptSecondFactor: ( - params: __experimental_SessionVerifyAttemptSecondFactorParams, - ) => Promise<__experimental_SessionVerificationResource>; get verifiedExternalAccounts(): ExternalAccountResource[]; @@ -151,20 +121,6 @@ export interface UserResource extends ClerkResource { get hasVerifiedPhoneNumber(): boolean; } -export type __experimental_SessionVerifyCreateParams = { - level: __experimental_SessionVerificationLevel; - maxAge: __experimental_SessionVerificationMaxAge; -}; - -export type __experimental_SessionVerifyPrepareFirstFactorParams = EmailCodeConfig | PhoneCodeConfig; -export type __experimental_SessionVerifyAttemptFirstFactorParams = - | EmailCodeAttempt - | PhoneCodeAttempt - | PasswordAttempt; - -export type __experimental_SessionVerifyPrepareSecondFactorParams = PhoneCodeSecondFactorConfig; -export type __experimental_SessionVerifyAttemptSecondFactorParams = PhoneCodeAttempt | TOTPAttempt | BackupCodeAttempt; - export type CreateEmailAddressParams = { email: string }; export type CreatePhoneNumberParams = { phoneNumber: string }; export type CreateWeb3WalletParams = { web3Wallet: string }; From 3c3a3df579bd1d9d4338d521ba3f858f277dd4b1 Mon Sep 17 00:00:00 2001 From: panteliselef Date: Mon, 2 Sep 2024 12:37:12 +0300 Subject: [PATCH 2/2] fix tests --- .../__tests__/UVFactorOne.test.tsx | 18 ++++++++--------- .../__tests__/UVFactorTwo.test.tsx | 20 +++++++++---------- .../src/ui/utils/test/createFixtures.tsx | 2 +- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/packages/clerk-js/src/ui/components/UserVerification/__tests__/UVFactorOne.test.tsx b/packages/clerk-js/src/ui/components/UserVerification/__tests__/UVFactorOne.test.tsx index c1cc5ec37b5..87a27836eaa 100644 --- a/packages/clerk-js/src/ui/components/UserVerification/__tests__/UVFactorOne.test.tsx +++ b/packages/clerk-js/src/ui/components/UserVerification/__tests__/UVFactorOne.test.tsx @@ -20,7 +20,7 @@ describe('UserVerificationFactorOne', () => { const { wrapper, fixtures } = await createFixtures(f => { f.withUser({ username: 'clerkuser' }); }); - fixtures.user?.__experimental_verifySession.mockResolvedValue({ + fixtures.session?.__experimental_startVerification.mockResolvedValue({ status: 'needs_first_factor', supportedFirstFactors: [{ strategy: 'password' }], }); @@ -38,11 +38,11 @@ describe('UserVerificationFactorOne', () => { f.withUser({ username: 'clerkuser' }); f.withPreferredSignInStrategy({ strategy: 'otp' }); }); - fixtures.user?.__experimental_verifySession.mockResolvedValue({ + fixtures.session?.__experimental_startVerification.mockResolvedValue({ status: 'needs_first_factor', supportedFirstFactors: [{ strategy: 'password' }, { strategy: 'email_code' }], }); - fixtures.user?.__experimental_verifySessionPrepareFirstFactor.mockResolvedValue({}); + fixtures.session?.__experimental_prepareFirstFactorVerification.mockResolvedValue({}); const { getByLabelText, getByText } = render(, { wrapper }); await waitFor(() => { @@ -58,15 +58,15 @@ describe('UserVerificationFactorOne', () => { const { wrapper, fixtures } = await createFixtures(f => { f.withUser({ username: 'clerkuser' }); }); - fixtures.user?.__experimental_verifySession.mockResolvedValue({ + fixtures.session?.__experimental_startVerification.mockResolvedValue({ status: 'needs_first_factor', supportedFirstFactors: [{ strategy: 'password' }], }); - fixtures.user?.__experimental_verifySessionAttemptFirstFactor.mockResolvedValue({ + fixtures.session?.__experimental_attemptFirstFactorVerification.mockResolvedValue({ status: 'needs_second_factor', supportedFirstFactors: [{ strategy: 'password' }], }); - fixtures.user?.__experimental_verifySessionPrepareSecondFactor.mockResolvedValue({ + fixtures.session?.__experimental_prepareSecondFactorVerification.mockResolvedValue({ status: 'needs_second_factor', supportedFirstFactors: [{ strategy: 'password' }], }); @@ -77,7 +77,7 @@ describe('UserVerificationFactorOne', () => { await userEvent.type(getByLabelText(/^password/i), 'testtest'); await userEvent.click(getByText('Continue')); - expect(fixtures.user?.__experimental_verifySessionAttemptFirstFactor).toHaveBeenCalledWith({ + expect(fixtures.session?.__experimental_attemptFirstFactorVerification).toHaveBeenCalledWith({ strategy: 'password', password: 'testtest', }); @@ -89,11 +89,11 @@ describe('UserVerificationFactorOne', () => { const { wrapper, fixtures } = await createFixtures(f => { f.withUser({ username: 'clerkuser' }); }); - fixtures.user?.__experimental_verifySession.mockResolvedValue({ + fixtures.session?.__experimental_startVerification.mockResolvedValue({ status: 'needs_first_factor', supportedFirstFactors: [{ strategy: 'password' }], }); - fixtures.user?.__experimental_verifySessionAttemptFirstFactor.mockResolvedValue({ + fixtures.session?.__experimental_attemptFirstFactorVerification.mockResolvedValue({ status: 'complete', session: { id: '123', diff --git a/packages/clerk-js/src/ui/components/UserVerification/__tests__/UVFactorTwo.test.tsx b/packages/clerk-js/src/ui/components/UserVerification/__tests__/UVFactorTwo.test.tsx index 68f9067124f..5449844774f 100644 --- a/packages/clerk-js/src/ui/components/UserVerification/__tests__/UVFactorTwo.test.tsx +++ b/packages/clerk-js/src/ui/components/UserVerification/__tests__/UVFactorTwo.test.tsx @@ -20,12 +20,12 @@ describe('UserVerificationFactorTwo', () => { const { wrapper, fixtures } = await createFixtures(f => { f.withUser({ username: 'clerkuser' }); }); - fixtures.user?.__experimental_verifySession.mockResolvedValue({ + fixtures.session?.__experimental_startVerification.mockResolvedValue({ status: 'needs_second_factor', supportedSecondFactors: [{ strategy: 'phone_code' }], }); - fixtures.user?.__experimental_verifySessionPrepareSecondFactor.mockResolvedValue({ + fixtures.session?.__experimental_prepareSecondFactorVerification.mockResolvedValue({ status: 'needs_second_factor', supportedSecondFactors: [{ strategy: 'phone_code' }], }); @@ -43,12 +43,12 @@ describe('UserVerificationFactorTwo', () => { const { wrapper, fixtures } = await createFixtures(f => { f.withUser({ username: 'clerkuser' }); }); - fixtures.user?.__experimental_verifySession.mockResolvedValue({ + fixtures.session?.__experimental_startVerification.mockResolvedValue({ status: 'needs_second_factor', supportedSecondFactors: [{ strategy: 'totp' }], }); - fixtures.user?.__experimental_verifySessionPrepareSecondFactor.mockResolvedValue({ + fixtures.session?.__experimental_prepareSecondFactorVerification.mockResolvedValue({ status: 'needs_second_factor', supportedSecondFactors: [{ strategy: 'totp' }], }); @@ -65,12 +65,12 @@ describe('UserVerificationFactorTwo', () => { const { wrapper, fixtures } = await createFixtures(f => { f.withUser({ username: 'clerkuser' }); }); - fixtures.user?.__experimental_verifySession.mockResolvedValue({ + fixtures.session?.__experimental_startVerification.mockResolvedValue({ status: 'needs_second_factor', supportedSecondFactors: [{ strategy: 'backup_code' }], }); - fixtures.user?.__experimental_verifySessionPrepareSecondFactor.mockResolvedValue({ + fixtures.session?.__experimental_prepareSecondFactorVerification.mockResolvedValue({ status: 'needs_second_factor', supportedSecondFactors: [{ strategy: 'backup_code' }], }); @@ -88,7 +88,7 @@ describe('UserVerificationFactorTwo', () => { const { wrapper, fixtures } = await createFixtures(f => { f.withUser({ username: 'clerkuser' }); }); - fixtures.user?.__experimental_verifySession.mockResolvedValue({ + fixtures.session?.__experimental_startVerification.mockResolvedValue({ status: 'needs_first_factor', }); render(, { wrapper }); @@ -102,17 +102,17 @@ describe('UserVerificationFactorTwo', () => { const { wrapper, fixtures } = await createFixtures(f => { f.withUser({ username: 'clerkuser' }); }); - fixtures.user?.__experimental_verifySession.mockResolvedValue({ + fixtures.session?.__experimental_startVerification.mockResolvedValue({ status: 'needs_second_factor', supportedSecondFactors: [{ strategy: 'phone_code' }], }); - fixtures.user?.__experimental_verifySessionPrepareSecondFactor.mockResolvedValue({ + fixtures.session?.__experimental_prepareSecondFactorVerification.mockResolvedValue({ status: 'needs_second_factor', supportedSecondFactors: [{ strategy: 'phone_code' }], }); - fixtures.user?.__experimental_verifySessionAttemptSecondFactor.mockResolvedValue({ + fixtures.session?.__experimental_attemptSecondFactorVerification.mockResolvedValue({ status: 'complete', supportedSecondFactors: [], session: { diff --git a/packages/clerk-js/src/ui/utils/test/createFixtures.tsx b/packages/clerk-js/src/ui/utils/test/createFixtures.tsx index df68b9920aa..a1deee34314 100644 --- a/packages/clerk-js/src/ui/utils/test/createFixtures.tsx +++ b/packages/clerk-js/src/ui/utils/test/createFixtures.tsx @@ -67,7 +67,7 @@ const unboundCreateFixtures = [ const fixtures = { clerk: clerkMock, - user: clerkMock.user, + session: clerkMock.session, signIn: clerkMock.client.signIn, signUp: clerkMock.client.signUp, environment: environmentMock,