Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 471
fix(elements): Correct TS types for verification machine#3671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| --- | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -5,9 +5,9 @@ import type { | ||
| PasswordAttempt, | ||
| PhoneCodeAttempt, | ||
| PrepareFirstFactorParams, | ||
| PrepareSecondFactorParams, | ||
| ResetPasswordEmailCodeAttempt, | ||
| ResetPasswordPhoneCodeAttempt, | ||
| SignInFactor, | ||
| SignInFirstFactor, | ||
| SignInResource, | ||
| SignInSecondFactor, | ||
| @@ -19,7 +19,7 @@ import { assign, fromPromise, log, sendTo, setup } from 'xstate'; | ||
| import { RESENDABLE_COUNTDOWN_DEFAULT } from '~/internals/constants'; | ||
| import { ClerkElementsRuntimeError } from '~/internals/errors'; | ||
| import type { FormFields } from '~/internals/machines/form'; | ||
| import type { WithParams } from '~/internals/machines/shared'; | ||
| import type { SignInStrategyName, WithParams } from '~/internals/machines/shared'; | ||
| import { sendToLoading } from '~/internals/machines/shared'; | ||
| import { determineStartingSignInFactor, determineStartingSignInSecondFactor } from '~/internals/machines/sign-in/utils'; | ||
| import { assertActorEventError, assertIsDefined } from '~/internals/machines/utils/assert'; | ||
| @@ -35,11 +35,11 @@ export type DetermineStartingFactorInput = { | ||
| parent: SignInRouterMachineActorRef; | ||
| }; | ||
| export type PrepareFirstFactorInput = WithParams<SignInFirstFactor | null> & { | ||
| export type PrepareFirstFactorInput = WithParams<PrepareFirstFactorParams> & { | ||
| parent: SignInRouterMachineActorRef; | ||
| resendable: boolean; | ||
| }; | ||
| export type PrepareSecondFactorInput = WithParams<SignInSecondFactor | null> & { | ||
| export type PrepareSecondFactorInput = WithParams<PrepareSecondFactorParams> & { | ||
| parent: SignInRouterMachineActorRef; | ||
| resendable: boolean; | ||
| }; | ||
| @@ -67,8 +67,8 @@ export const SignInVerificationMachineId = 'SignInVerification'; | ||
| const SignInVerificationMachine = setup({ | ||
| actors: { | ||
| determineStartingFactor: fromPromise<SignInFactor | null, DetermineStartingFactorInput>(() => | ||
| Promise.reject(new ClerkElementsRuntimeError('Actor `determineStartingFactor` must be overridden')), | ||
| determineStartingFactor: fromPromise<SignInFirstFactor | SignInSecondFactor | null, DetermineStartingFactorInput>( | ||
| () => Promise.reject(new ClerkElementsRuntimeError('Actor `determineStartingFactor` must be overridden')), | ||
| ), | ||
| prepare: fromPromise<SignInResource, PrepareFirstFactorInput | PrepareSecondFactorInput>(() => | ||
| Promise.reject(new ClerkElementsRuntimeError('Actor `prepare` must be overridden')), | ||
| @@ -96,10 +96,7 @@ const SignInVerificationMachine = setup({ | ||
| // Only show these warnings in development! | ||
| if (process.env.NODE_ENV === 'development') { | ||
| if ( | ||
| !clerk.client.signIn.supportedFirstFactors.every(factor => | ||
| // TODO: These "as SignInFactor" assertions are incorrect, factor.strategy is SignInFactor['strategy']. This needs to be fixed together with SignInVerificationStrategyRegisterEvent and SignInStrategy React component | ||
| context.registeredStrategies.has(factor.strategy as unknown as SignInFactor), | ||
| ) | ||
| !clerk.client.signIn.supportedFirstFactors.every(factor => context.registeredStrategies.has(factor.strategy)) | ||
| ) { | ||
| console.warn( | ||
| `Clerk: Your instance is configured to support these strategies: ${clerk.client.signIn.supportedFirstFactors | ||
| @@ -112,9 +109,7 @@ const SignInVerificationMachine = setup({ | ||
| if ( | ||
| clerk.client.signIn.supportedSecondFactors && | ||
| !clerk.client.signIn.supportedSecondFactors.every(factor => | ||
| context.registeredStrategies.has(factor.strategy as unknown as SignInFactor), | ||
| ) | ||
| !clerk.client.signIn.supportedSecondFactors.every(factor => context.registeredStrategies.has(factor.strategy)) | ||
| ) { | ||
| console.warn( | ||
| `Clerk: Your instance is configured to support these 2FA strategies: ${clerk.client.signIn.supportedSecondFactors | ||
| @@ -125,26 +120,17 @@ const SignInVerificationMachine = setup({ | ||
| ); | ||
| } | ||
| // TODO: These "as SignInFactor" assertions are incorrect, factor.strategy is SignInFactor['strategy']. This needs to be fixed together with SignInVerificationStrategyRegisterEvent and SignInStrategy React component | ||
| // This type should also probably be SignInFirstFactor['strategy'] instead of SignInFactor['strategy'] !!! | ||
| const strategiesUsedButNotActivated = Array.from(context.registeredStrategies).filter( | ||
| strategy => | ||
| !clerk.client.signIn.supportedFirstFactors.some( | ||
| supported => (supported.strategy as unknown as SignInFactor) === strategy, | ||
| ), | ||
| ) as unknown as Array<SignInFactor['strategy']>; | ||
| strategy => !clerk.client.signIn.supportedFirstFactors.some(supported => supported.strategy === strategy), | ||
| ); | ||
| if (strategiesUsedButNotActivated.length > 0) { | ||
| console.warn( | ||
| `Clerk: These rendered strategies are not configured for your instance: ${strategiesUsedButNotActivated.join(', ')}. If this is unexpected, make sure to enable them in your Clerk dashboard: https://dashboard.clerk.com/last-active?path=/user-authentication/email-phone-username`, | ||
| ); | ||
| } | ||
| if ( | ||
| context.currentFactor?.strategy && | ||
| // TODO: These "as SignInFactor" assertions are incorrect, factor.strategy is SignInFactor['strategy']. This needs to be fixed together with SignInVerificationStrategyRegisterEvent and SignInStrategy React component | ||
| !context.registeredStrategies.has(context.currentFactor?.strategy as unknown as SignInFactor) | ||
| ) { | ||
| if (context.currentFactor?.strategy && !context.registeredStrategies.has(context.currentFactor?.strategy)) { | ||
| throw new ClerkElementsRuntimeError( | ||
| `Your sign-in attempt is missing a ${context.currentFactor?.strategy} strategy. Make sure <Strategy name="${context.currentFactor?.strategy}"> is rendered in your flow. More information: https://clerk.com/docs/elements/reference/sign-in#strategy`, | ||
| ); | ||
| @@ -195,7 +181,7 @@ const SignInVerificationMachine = setup({ | ||
| formRef: input.formRef, | ||
| loadingStep: 'verifications', | ||
| parent: input.parent, | ||
| registeredStrategies: new Set<SignInFactor>(), | ||
| registeredStrategies: new Set<SignInStrategyName>(), | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change stems from looking at | ||
| resendable: false, | ||
| resendableAfter: RESENDABLE_COUNTDOWN_DEFAULT, | ||
| }), | ||
| @@ -250,7 +236,7 @@ const SignInVerificationMachine = setup({ | ||
| input: ({ context }) => ({ | ||
| parent: context.parent, | ||
| resendable: context.resendable, | ||
| params: context.currentFactor as SignInFirstFactor | null, | ||
| params: context.currentFactor as PrepareFirstFactorParams, | ||
| }), | ||
| onDone: { | ||
| actions: 'resendableReset', | ||
| @@ -348,7 +334,7 @@ const SignInVerificationMachine = setup({ | ||
| src: 'attempt', | ||
| input: ({ context }) => ({ | ||
| parent: context.parent, | ||
| currentFactor: context.currentFactor as SignInFirstFactor, | ||
| currentFactor: context.currentFactor as SignInFirstFactor | null, | ||
| fields: context.formRef.getSnapshot().context.fields, | ||
| }), | ||
| onDone: { | ||
| @@ -380,7 +366,7 @@ export const SignInFirstFactorMachine = SignInVerificationMachine.provide({ | ||
| ); | ||
| }), | ||
| prepare: fromPromise(async ({ input }) => { | ||
| const { params, parent, resendable } = input; | ||
| const { params, parent, resendable } = input as PrepareFirstFactorInput; | ||
| const clerk = parent.getSnapshot().context.clerk; | ||
| // If a prepare call has already been fired recently, don't re-send | ||
| @@ -393,7 +379,7 @@ export const SignInFirstFactorMachine = SignInVerificationMachine.provide({ | ||
| assertIsDefined(params, 'First factor params'); | ||
| return await clerk.client.signIn.prepareFirstFactor(params as PrepareFirstFactorParams); | ||
| return await clerk.client.signIn.prepareFirstFactor(params); | ||
| }), | ||
| attempt: fromPromise(async ({ input }) => { | ||
| const { currentFactor, fields, parent } = input as AttemptFirstFactorInput; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SignInFactorisSignInFirstFactor | SignInSecondFactor. The changes in this file narrow the input and return types