From c031b503fce3e1a0a99f4896b8a6dd96ab012645 Mon Sep 17 00:00:00 2001 From: panteliselef Date: Tue, 19 Mar 2024 13:55:25 +0200 Subject: [PATCH 1/2] fix(clerk-js): Define priority for strategy passkey when user is missing that first factor --- .../src/ui/components/SignIn/utils.ts | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/clerk-js/src/ui/components/SignIn/utils.ts b/packages/clerk-js/src/ui/components/SignIn/utils.ts index 90826ad0b34..162a0530163 100644 --- a/packages/clerk-js/src/ui/components/SignIn/utils.ts +++ b/packages/clerk-js/src/ui/components/SignIn/utils.ts @@ -1,6 +1,7 @@ import { titleize } from '@clerk/shared'; import type { PreferredSignInStrategy, SignInFactor, SignInResource, SignInStrategy } from '@clerk/types'; +import { isWebAuthnSupported } from '../../../utils/passkeys'; import { PREFERRED_SIGN_IN_STRATEGIES } from '../../common/constants'; import { otpPrefFactorComparator, passwordPrefFactorComparator } from '../../utils/factorSorting'; @@ -55,6 +56,22 @@ const factorForIdentifier = (i: string | null) => (f: SignInFactor) => { return 'safeIdentifier' in f && f.safeIdentifier === i; }; +function determineStrategyWhenPasskeyIsPreferred( + factors: SignInFactor[], + identifier: string | null, +): SignInFactor | null { + if (isWebAuthnSupported()) { + // @ts-ignore + const passkeyFactor = factors.find(({ strategy }) => strategy === 'passkey'); + + if (passkeyFactor) { + return passkeyFactor; + } + } + + return determineStrategyWhenOTPIsPreferred(factors, identifier); +} + function determineStrategyWhenPasswordIsPreferred( factors: SignInFactor[], identifier: string | null, @@ -90,10 +107,8 @@ export function determineStartingSignInFactor( return null; } - //TODO: Create proper function like `determineStrategyWhenOTPIsPreferred` if (preferredSignInStrategy === PREFERRED_SIGN_IN_STRATEGIES.Passkey) { - // @ts-ignore - return firstFactors.find(f => f.strategy === PREFERRED_SIGN_IN_STRATEGIES.Passkey); + return determineStrategyWhenPasskeyIsPreferred(firstFactors, identifier); } return preferredSignInStrategy === PREFERRED_SIGN_IN_STRATEGIES.Password ? determineStrategyWhenPasswordIsPreferred(firstFactors, identifier) From cf61ecc57022cf3fe1db868993c4f6a9b3a94294 Mon Sep 17 00:00:00 2001 From: panteliselef Date: Tue, 19 Mar 2024 20:21:24 +0200 Subject: [PATCH 2/2] chore(clerk-js): Add changeset --- .changeset/odd-rats-smash.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/odd-rats-smash.md diff --git a/.changeset/odd-rats-smash.md b/.changeset/odd-rats-smash.md new file mode 100644 index 00000000000..de078ea107c --- /dev/null +++ b/.changeset/odd-rats-smash.md @@ -0,0 +1,5 @@ +--- +'@clerk/clerk-js': patch +--- + +Fix: fallback to other first factors when preferred strategy is passkey but the user has not registered a passkey yet.