-
Notifications
You must be signed in to change notification settings - Fork 414
feat(clerk-js,localizations,types): email_code & email_link as 2FA when signing in on new device
#7116
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
feat(clerk-js,localizations,types): email_code & email_link as 2FA when signing in on new device
#7116
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
341f537
feat(clerk-js, types): Add clientTrustState and untrustedFirstFactors…
chriscanin fb16e60
feat(clerk-js, types): Add client_trust_state and untrusted_first_fac…
chriscanin 25fe87f
refactor(client-resource): remove client_trust_state from dummy data
chriscanin 78041a4
feat(client-resource): update fraud protection fields in Client and S…
chriscanin 701995a
chore: Remove untrusted_first_factors
tmilewski 307185c
chore: Update changeset
tmilewski a2e2450
feat(clerk-js, types): Add clientTrustState and untrustedFirstFactors…
chriscanin f1e2307
chore: Remove untrusted_first_factors
tmilewski 4381e03
feat: Prep work to support email_code as a second factor
tmilewski b70b5db
chore: Ensure types pull from shared pkg
tmilewski 67ec01a
feat: SignInFactorTwoCode
tmilewski 820d80d
feat: Email Link as Second Factor
tmilewski df941c2
chore: Clean up
tmilewski 32c9066
chore: Update Bundlewatch config
tmilewski 59d9c5b
chore: Remove test keys
tmilewski 6af8a9f
chore: Clean up
tmilewski a119a90
chore: Clean up
tmilewski 749ca74
fix: build
tmilewski 35c2b76
chore: Remove unused prop
tmilewski 8778783
chore: Update localization files
tmilewski 2bcf09a
chore: Update localization files
tmilewski 0d07fae
fix: Allow formTitle to be undefined
tmilewski 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,7 @@ | ||
| --- | ||
| '@clerk/localizations': minor | ||
| '@clerk/clerk-js': minor | ||
| '@clerk/types': minor | ||
| --- | ||
|
|
||
| Support for `email_code` and `email_link` as a second factor when user is signing in on a new device. |
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
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
30 changes: 30 additions & 0 deletions
30
packages/clerk-js/src/ui/components/SignIn/SignInFactorTwoEmailCodeCard.tsx
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,30 @@ | ||
| import type { EmailCodeFactor } from '@clerk/shared/types'; | ||
|
|
||
| import { useCoreSignIn } from '../../contexts'; | ||
| import { Flow, localizationKeys } from '../../customizables'; | ||
| import type { SignInFactorTwoCodeCard } from './SignInFactorTwoCodeForm'; | ||
| import { SignInFactorTwoCodeForm } from './SignInFactorTwoCodeForm'; | ||
|
|
||
| type SignInFactorTwoEmailCodeCardProps = SignInFactorTwoCodeCard & { factor: EmailCodeFactor }; | ||
|
|
||
| export const SignInFactorTwoEmailCodeCard = (props: SignInFactorTwoEmailCodeCardProps) => { | ||
| const signIn = useCoreSignIn(); | ||
|
|
||
| const prepare = () => { | ||
| const { emailAddressId, strategy } = props.factor; | ||
| return signIn.prepareSecondFactor({ emailAddressId, strategy }); | ||
| }; | ||
|
|
||
| return ( | ||
| <Flow.Part part='emailCode2Fa'> | ||
| <SignInFactorTwoCodeForm | ||
| {...props} | ||
| cardTitle={localizationKeys('signIn.emailCodeMfa.title')} | ||
| cardSubtitle={localizationKeys('signIn.emailCodeMfa.subtitle')} | ||
| inputLabel={localizationKeys('signIn.emailCodeMfa.formTitle')} | ||
| resendButton={localizationKeys('signIn.emailCodeMfa.resendButton')} | ||
| prepare={prepare} | ||
| /> | ||
| </Flow.Part> | ||
| ); | ||
| }; |
102 changes: 102 additions & 0 deletions
102
packages/clerk-js/src/ui/components/SignIn/SignInFactorTwoEmailLinkCard.tsx
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,102 @@ | ||
| import { isUserLockedError } from '@clerk/shared/error'; | ||
| import { useClerk } from '@clerk/shared/react'; | ||
| import type { EmailLinkFactor, SignInResource } from '@clerk/shared/types'; | ||
| import React from 'react'; | ||
|
|
||
| import type { VerificationCodeCardProps } from '@/ui/elements/VerificationCodeCard'; | ||
| import { VerificationLinkCard } from '@/ui/elements/VerificationLinkCard'; | ||
| import { handleError } from '@/ui/utils/errorHandler'; | ||
|
|
||
| import { EmailLinkStatusCard } from '../../common'; | ||
| import { buildVerificationRedirectUrl } from '../../common/redirects'; | ||
| import { useCoreSignIn, useSignInContext } from '../../contexts'; | ||
| import { Flow, localizationKeys, useLocalizations } from '../../customizables'; | ||
| import { useCardState } from '../../elements/contexts'; | ||
| import { useEmailLink } from '../../hooks/useEmailLink'; | ||
|
|
||
| type SignInFactorTwoEmailLinkCardProps = Pick<VerificationCodeCardProps, 'onShowAlternativeMethodsClicked'> & { | ||
| factor: EmailLinkFactor; | ||
| factorAlreadyPrepared: boolean; | ||
| onFactorPrepare: () => void; | ||
| }; | ||
|
|
||
| const isNewDevice = (resource: SignInResource) => resource.clientTrustState === 'new'; | ||
|
|
||
| export const SignInFactorTwoEmailLinkCard = (props: SignInFactorTwoEmailLinkCardProps) => { | ||
| const { t } = useLocalizations(); | ||
| const card = useCardState(); | ||
| const signIn = useCoreSignIn(); | ||
| const signInContext = useSignInContext(); | ||
| const { signInUrl } = signInContext; | ||
| const { afterSignInUrl } = useSignInContext(); | ||
| const { setActive } = useClerk(); | ||
| const { startEmailLinkFlow, cancelEmailLinkFlow } = useEmailLink(signIn); | ||
| const [showVerifyModal, setShowVerifyModal] = React.useState(false); | ||
| const clerk = useClerk(); | ||
|
|
||
| React.useEffect(() => { | ||
| void startEmailLinkVerification(); | ||
| }, []); | ||
|
|
||
| const restartVerification = () => { | ||
| cancelEmailLinkFlow(); | ||
| void startEmailLinkVerification(); | ||
| }; | ||
|
|
||
| const startEmailLinkVerification = () => { | ||
| startEmailLinkFlow({ | ||
| emailAddressId: props.factor.emailAddressId, | ||
| redirectUrl: buildVerificationRedirectUrl({ ctx: signInContext, baseUrl: signInUrl, intent: 'sign-in' }), | ||
| }) | ||
| .then(res => handleVerificationResult(res)) | ||
| .catch(err => { | ||
| if (isUserLockedError(err)) { | ||
| // @ts-expect-error -- private method for the time being | ||
| return clerk.__internal_navigateWithError('..', err.errors[0]); | ||
| } | ||
|
|
||
| handleError(err, [], card.setError); | ||
| }); | ||
| }; | ||
|
|
||
| const handleVerificationResult = async (si: SignInResource) => { | ||
| const ver = si.secondFactorVerification; | ||
| if (ver.status === 'expired') { | ||
| card.setError(t(localizationKeys('formFieldError__verificationLinkExpired'))); | ||
| } else if (ver.verifiedFromTheSameClient()) { | ||
| setShowVerifyModal(true); | ||
| } else { | ||
| await setActive({ | ||
| session: si.createdSessionId, | ||
| redirectUrl: afterSignInUrl, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| if (showVerifyModal) { | ||
| return ( | ||
| <EmailLinkStatusCard | ||
| title={localizationKeys('signIn.emailLink.verifiedSwitchTab.titleNewTab')} | ||
| subtitle={localizationKeys('signIn.emailLink.verifiedSwitchTab.subtitleNewTab')} | ||
| status='verified_switch_tab' | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <Flow.Part part='emailLink'> | ||
| <VerificationLinkCard | ||
| cardTitle={localizationKeys('signIn.emailLinkMfa.title')} | ||
| cardSubtitle={localizationKeys('signIn.emailLinkMfa.subtitle')} | ||
| cardNotice={isNewDevice(signIn) ? localizationKeys('signIn.newDeviceVerificationNotice') : undefined} | ||
| formTitle={localizationKeys('signIn.emailLinkMfa.formTitle')} | ||
| formSubtitle={localizationKeys('signIn.emailLinkMfa.formSubtitle')} | ||
| resendButton={localizationKeys('signIn.emailLinkMfa.resendButton')} | ||
| onResendCodeClicked={restartVerification} | ||
| safeIdentifier={props.factor.safeIdentifier} | ||
| profileImageUrl={signIn.userData.imageUrl} | ||
| onShowAlternativeMethodsClicked={props.onShowAlternativeMethodsClicked} | ||
| /> | ||
| </Flow.Part> | ||
| ); | ||
tmilewski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.