-
Notifications
You must be signed in to change notification settings - Fork 460
feat(elements): Add backup_code verification strategy
#3627
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
Merged
Changes from all commits
Commits
Show all changes
3 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,25 @@ | ||
| --- | ||
| '@clerk/elements': minor | ||
| --- | ||
|
|
||
| Add `backup_code` verification strategy | ||
|
|
||
| ```tsx | ||
| <SignIn.Step name='choose-strategy'> | ||
| <SignIn.SupportedStrategy name='backup_code'>Use a backup code</SignIn.SupportedStrategy> | ||
| <SignIn.Step> | ||
| ``` | ||
|
|
||
| ```tsx | ||
| <SignIn.Step name='verifications'> | ||
| <SignIn.Strategy name='backup_code'> | ||
| <Clerk.Field name="backup_code"> | ||
| <Clerk.Label>Code:</Clerk.Label> | ||
| <Clerk.Input /> | ||
| <Clerk.FieldError /> | ||
| </Clerk.Field> | ||
|
|
||
| <Clerk.Action submit>Continue</Clerk.Action> | ||
| </SignIn.Strategy> | ||
| <SignIn.Step> | ||
| ``` |
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
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 |
|---|---|---|
|
|
@@ -73,6 +73,9 @@ const determineInputTypeFromName = (name: FormFieldProps['name']) => { | |
| if (name === 'code') { | ||
| return 'otp' as const; | ||
| } | ||
| if (name === 'backup_code') { | ||
| return 'backup_code' as const; | ||
| } | ||
|
|
||
| return 'text' as const; | ||
| }; | ||
|
|
@@ -199,11 +202,12 @@ const useInput = ({ | |
| }: FormInputProps) => { | ||
| // Inputs can be used outside a <Field> wrapper if desired, so safely destructure here | ||
| const fieldContext = useFieldContext(); | ||
| const name = inputName || fieldContext?.name; | ||
| const rawName = inputName || fieldContext?.name; | ||
| const name = rawName === 'backup_code' ? 'code' : rawName; // `backup_code` is a special case of `code` | ||
| const { state: fieldState } = useFieldState({ name }); | ||
| const validity = useValidityStateContext(); | ||
|
|
||
| if (!name) { | ||
| if (!rawName || !name) { | ||
| throw new Error('Clerk: <Input /> must be wrapped in a <Field> component or have a name prop.'); | ||
| } | ||
|
|
||
|
|
@@ -248,7 +252,7 @@ const useInput = ({ | |
| }); | ||
| const value = useFormSelector(fieldValueSelector(name)); | ||
| const hasValue = Boolean(value); | ||
| const type = inputType ?? determineInputTypeFromName(name); | ||
| const type = inputType ?? determineInputTypeFromName(rawName); | ||
| let shouldValidatePassword = false; | ||
|
|
||
| if (type === 'password' || type === 'text') { | ||
|
|
@@ -308,10 +312,6 @@ const useInput = ({ | |
| ref.send({ type: 'FIELD.UPDATE', field: { name, value: initialValue } }); | ||
| }, [name, ref, initialValue]); | ||
|
|
||
| if (!name) { | ||
| throw new Error('Clerk: <Input /> must be wrapped in a <Field> component or have a name prop.'); | ||
| } | ||
|
|
||
| // TODO: Implement clerk-js utils | ||
| const shouldBeHidden = false; | ||
|
|
||
|
|
@@ -337,8 +337,13 @@ const useInput = ({ | |
| type: 'text', | ||
| spellCheck: false, | ||
| }; | ||
| } | ||
| if (type === 'password' && shouldValidatePassword) { | ||
| } else if (type === 'backup_code') { | ||
|
Member
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. The The |
||
| props = { | ||
| autoComplete: 'off', | ||
| type: 'text', | ||
| spellCheck: false, | ||
| }; | ||
| } else if (type === 'password' && shouldValidatePassword) { | ||
| props = { | ||
| 'data-has-passed-validation': hasPassedValiation ? true : undefined, | ||
| }; | ||
|
|
@@ -798,7 +803,8 @@ const GlobalError = React.forwardRef<FormGlobalErrorElement, FormGlobalErrorProp | |
| const FieldError = React.forwardRef<FormFieldErrorElement, FormFieldErrorProps>( | ||
| ({ asChild = false, children, code, name, ...rest }, forwardedRef) => { | ||
| const fieldContext = useFieldContext(); | ||
| const fieldName = fieldContext?.name || name; | ||
| const rawFieldName = fieldContext?.name || name; | ||
| const fieldName = rawFieldName === 'backup_code' ? 'code' : rawFieldName; | ||
| const { feedback } = useFieldFeedback({ name: fieldName }); | ||
|
|
||
| if (!(feedback?.type === 'error')) { | ||
|
|
||
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.
Removed
throwas it puts users into a completely irrecoverable state even ifFieldErrororGlobalErrorare present.