-
Notifications
You must be signed in to change notification settings - Fork 460
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
Merged
LekoArts
merged 5 commits into
main
from
lekoarts/sdk-1845-correct-ts-types-for-strategy-verification-machine
Jul 9, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| --- | ||
| --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>(), | ||
|
Contributor
Author
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; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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