Skip to content

Commit

Permalink
fix(clerk-js): Correctly call authenticatePasskey for the appropriate…
Browse files Browse the repository at this point in the history
… flow
  • Loading branch information
panteliselef committed Mar 19, 2024
1 parent b9f8bf3 commit 78ee4fd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
9 changes: 4 additions & 5 deletions packages/clerk-js/src/core/resources/SignIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,9 @@ export class SignIn extends BaseResource implements SignInResource {
};

public __experimental_authenticateWithPasskey = async (params?: {
triggerAutofill?: boolean;
flow?: 'autofill' | 'discoverable';
}): Promise<SignInResource> => {
const defaultParams = { triggerAutofill: false };
const { triggerAutofill } = { ...defaultParams, ...params };
const { flow } = params || {};

/**
* The UI should always prevent from this method being called if WebAuthn is not supported.
Expand All @@ -277,7 +276,7 @@ export class SignIn extends BaseResource implements SignInResource {
});
}

if (!this.firstFactorVerification.nonce && !this.id) {
if (flow === 'autofill' || flow === 'discoverable') {
// @ts-ignore As this is experimental we want to support it at runtime, but not at the type level
await this.create({ strategy: 'passkey' });
} else {
Expand All @@ -304,7 +303,7 @@ export class SignIn extends BaseResource implements SignInResource {

let canUseConditionalUI = false;

if (triggerAutofill) {
if (flow === 'autofill') {
/**
* If autofill is not supported gracefully handle the result, we don't need to throw.
* The caller should always check this before calling this method.
Expand Down
13 changes: 9 additions & 4 deletions packages/clerk-js/src/ui/components/SignIn/SignInStart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
import { ERROR_CODES } from '../../../core/constants';
import { clerkInvalidFAPIResponse } from '../../../core/errors';
import { getClerkQueryParam, removeClerkQueryParam } from '../../../utils';
import { isWebAuthnAutofillSupported } from '../../../utils/passkeys';
import { isWebAuthnAutofillSupported, isWebAuthnSupported } from '../../../utils/passkeys';
import type { SignInStartIdentifier } from '../../common';
import { getIdentifierControlDisplayValues, groupIdentifiers, withRedirectToAfterSignIn } from '../../common';
import { buildSSOCallbackURL } from '../../common/redirects';
Expand Down Expand Up @@ -42,7 +42,7 @@ const useAutoFillPasskey = () => {
return;
}

await authenticateWithPasskey({ triggerAutofill: true });
await authenticateWithPasskey({ flow: 'autofill' });
}

if (passkeySettings.allow_autofill) {
Expand All @@ -69,8 +69,13 @@ export function _SignInStart(): JSX.Element {
() => groupIdentifiers(userSettings.enabledFirstFactorIdentifiers),
[userSettings.enabledFirstFactorIdentifiers],
);

/**
* Passkeys
*/
const { isWebAuthnAutofillSupported } = useAutoFillPasskey();
const authenticateWithPasskey = useHandleAuthenticateWithPasskey();
const isWebSupported = isWebAuthnSupported();

const onlyPhoneNumberInitialValueExists =
!!ctx.initialValues?.phoneNumber && !(ctx.initialValues.emailAddress || ctx.initialValues.username);
Expand Down Expand Up @@ -358,11 +363,11 @@ export function _SignInStart(): JSX.Element {
</Form.Root>
) : null}
</SocialButtonsReversibleContainerWithDivider>
{userSettings.passkeySettings.show_sign_in_button && (
{userSettings.passkeySettings.show_sign_in_button && isWebSupported && (
<Card.Action elementId={'usePasskey'}>
<Card.ActionLink
localizationKey={localizationKeys('signIn.start.actionLink__use_passkey')}
onClick={() => authenticateWithPasskey()}
onClick={() => authenticateWithPasskey({ flow: 'discoverable' })}
/>
</Card.Action>
)}
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/signIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export interface SignInResource extends ClerkResource {

authenticateWithMetamask: () => Promise<SignInResource>;

__experimental_authenticateWithPasskey: (params?: { triggerAutofill?: boolean }) => Promise<SignInResource>;
__experimental_authenticateWithPasskey: (params?: { flow?: 'autofill' | 'discoverable' }) => Promise<SignInResource>;

createEmailLinkFlow: () => CreateEmailLinkFlowReturn<SignInStartEmailLinkFlowParams, SignInResource>;

Expand Down

0 comments on commit 78ee4fd

Please sign in to comment.