@clerk/shared
Version:
Internal package utils used by the Clerk SDKs
1 lines • 5.98 kB
Source Map (JSON)
{"version":3,"file":"nextStep.mjs","names":[],"sources":["../../../src/internal/clerk-js/nextStep.ts"],"sourcesContent":["import type { ProtectCheckResource, SignUpField, SignUpIdentificationField, VerificationResource } from '../../types';\n\ntype SharedNextStep =\n | { type: 'protect' }\n | { type: 'transfer' }\n | { type: 'complete'; createdSessionId: string | null }\n | { type: 'unknown' };\n\nexport type SignInNextStep =\n | SharedNextStep\n | { type: 'identifier' }\n | { type: 'first_factor'; strategy: string | null; sent: boolean }\n | { type: 'second_factor'; strategy: string | null; sent: boolean }\n | { type: 'client_trust' }\n | { type: 'new_password' };\n\nexport type SignUpNextStep =\n | SharedNextStep\n | { type: 'start' }\n | { type: 'enterprise_sso' }\n | { type: 'fields'; fields: SignUpField[] }\n | { type: 'verify_email'; target: string | null; sent: boolean }\n | { type: 'verify_phone'; target: string | null; sent: boolean };\n\nexport type NextStep = SignInNextStep | SignUpNextStep;\n\ntype VerificationInput = Pick<VerificationResource, 'status' | 'strategy'>;\n\nexport type SignInNextStepInput = {\n status: string | null;\n protectCheck: ProtectCheckResource | null;\n isTransferable: boolean;\n firstFactorVerification: VerificationInput;\n secondFactorVerification: VerificationInput;\n createdSessionId: string | null;\n};\n\nexport type SignUpNextStepInput = {\n id?: string | undefined;\n status: string | null;\n protectCheck: ProtectCheckResource | null;\n isTransferable: boolean;\n missingFields: SignUpField[];\n unverifiedFields: SignUpIdentificationField[];\n emailAddress: string | null;\n phoneNumber: string | null;\n verifications: {\n emailAddress: { nextAction: string };\n phoneNumber: { nextAction: string };\n };\n createdSessionId: string | null;\n};\n\n/**\n * Computes the next step of a sign-in from the fields FAPI sends today.\n *\n * @internal\n */\nexport function getSignInNextStep(signIn: SignInNextStepInput): SignInNextStep {\n if (signIn.protectCheck) {\n return { type: 'protect' };\n }\n if (signIn.isTransferable) {\n return { type: 'transfer' };\n }\n switch (signIn.status) {\n case null:\n case 'needs_identifier':\n return { type: 'identifier' };\n case 'needs_first_factor':\n return { type: 'first_factor', ...verificationState(signIn.firstFactorVerification) };\n case 'needs_second_factor':\n return { type: 'second_factor', ...verificationState(signIn.secondFactorVerification) };\n case 'needs_client_trust':\n return { type: 'client_trust' };\n case 'needs_new_password':\n return { type: 'new_password' };\n case 'needs_protect_check':\n return { type: 'protect' };\n case 'complete':\n return { type: 'complete', createdSessionId: signIn.createdSessionId };\n default:\n return { type: 'unknown' };\n }\n}\n\n/**\n * Computes the next step of a sign-up from the fields FAPI sends today.\n *\n * @internal\n */\nexport function getSignUpNextStep(signUp: SignUpNextStepInput): SignUpNextStep {\n if (!signUp.id) {\n return { type: 'start' };\n }\n if (signUp.status === 'complete') {\n return { type: 'complete', createdSessionId: signUp.createdSessionId };\n }\n if (signUp.status !== 'missing_requirements') {\n return { type: 'unknown' };\n }\n if (signUp.protectCheck || signUp.missingFields.includes('protect_check')) {\n return { type: 'protect' };\n }\n if (signUp.isTransferable) {\n return { type: 'transfer' };\n }\n if (signUp.missingFields.includes('enterprise_sso')) {\n return { type: 'enterprise_sso' };\n }\n if (signUp.unverifiedFields.includes('email_address')) {\n return {\n type: 'verify_email',\n target: signUp.emailAddress,\n sent: signUp.verifications.emailAddress.nextAction === 'needs_attempt',\n };\n }\n if (signUp.unverifiedFields.includes('phone_number')) {\n return {\n type: 'verify_phone',\n target: signUp.phoneNumber,\n sent: signUp.verifications.phoneNumber.nextAction === 'needs_attempt',\n };\n }\n if (signUp.missingFields.length > 0) {\n return { type: 'fields', fields: signUp.missingFields };\n }\n // TODO: FAPI computes `MissingRequirements` in `checkStatus` but does not serialize it, so a sign-up blocked only\n // by an unmet identifier requirement lands here. Return `fields` from `missing_requirements` once FAPI sends it.\n return { type: 'unknown' };\n}\n\nfunction verificationState(verification: VerificationInput): { strategy: string | null; sent: boolean } {\n return { strategy: verification.strategy, sent: verification.status === 'unverified' };\n}\n"],"mappings":";;;;;;AA0DA,SAAgB,kBAAkB,QAA6C;CAC7E,IA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