Skip to content

Commit

Permalink
Switch startAuthentication to options object
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterKale committed Oct 6, 2024
1 parent b7442d0 commit 6cd5003
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions packages/browser/src/methods/startAuthentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,25 @@ import { identifyAuthenticationError } from '../helpers/identifyAuthenticationEr
import { WebAuthnAbortService } from '../helpers/webAuthnAbortService';
import { toAuthenticatorAttachment } from '../helpers/toAuthenticatorAttachment';

export type StartAuthenticationOpts = {
optionsJSON: PublicKeyCredentialRequestOptionsJSON;
useBrowserAutofill?: boolean;
};

/**
* Begin authenticator "login" via WebAuthn assertion
*
* @param optionsJSON Output from **@simplewebauthn/server**'s `generateAuthenticationOptions()`
* @param useBrowserAutofill (Optional) Initialize conditional UI to enable logging in via browser autofill prompts. Defaults to `false`.
*/
export async function startAuthentication(
optionsJSON: PublicKeyCredentialRequestOptionsJSON,
useBrowserAutofill = false,
options: StartAuthenticationOpts,
): Promise<AuthenticationResponseJSON> {
const {
optionsJSON,
useBrowserAutofill = false,
} = options;

if (!browserSupportsWebAuthn()) {
throw new Error('WebAuthn is not supported in this browser');
}
Expand All @@ -44,7 +53,7 @@ export async function startAuthentication(
};

// Prepare options for `.get()`
const options: CredentialRequestOptions = {};
const getOptions: CredentialRequestOptions = {};

/**
* Set up the page to prompt the user to select a credential for authentication via the browser's
Expand All @@ -69,22 +78,22 @@ export async function startAuthentication(

// `CredentialMediationRequirement` doesn't know about "conditional" yet as of
// typescript@4.6.3
options.mediation = 'conditional' as CredentialMediationRequirement;
getOptions.mediation = 'conditional' as CredentialMediationRequirement;
// Conditional UI requires an empty allow list
publicKey.allowCredentials = [];
}

// Finalize options
options.publicKey = publicKey;
getOptions.publicKey = publicKey;
// Set up the ability to cancel this request if the user attempts another
options.signal = WebAuthnAbortService.createNewAbortSignal();
getOptions.signal = WebAuthnAbortService.createNewAbortSignal();

// Wait for the user to complete assertion
let credential;
try {
credential = (await navigator.credentials.get(options)) as AuthenticationCredential;
credential = (await navigator.credentials.get(getOptions)) as AuthenticationCredential;
} catch (err) {
throw identifyAuthenticationError({ error: err as Error, options });
throw identifyAuthenticationError({ error: err as Error, options: getOptions });
}

if (!credential) {
Expand Down

0 comments on commit 6cd5003

Please sign in to comment.