diff options
author | Matthew Miller <matthew@millerti.me> | 2023-10-02 22:34:41 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-02 22:34:41 -0700 |
commit | fc17d1a4c91fda50ddb07693d3aea09d74cdc3c7 (patch) | |
tree | 77a57851214b5ebb105cae182f68935461b8ba0f /packages/browser/src/helpers/webAuthnAbortService.ts | |
parent | 1c70e39302d81eb6fe2ba23da07c60f2dc397498 (diff) | |
parent | 3bc51ff209c71c8d2dd1e7a324eab487c5213c7f (diff) |
Merge pull request #449 from MasterKale/feat/448-manual-ceremony-cancellation
feat/448-manual-ceremony-cancellation
Diffstat (limited to 'packages/browser/src/helpers/webAuthnAbortService.ts')
-rw-r--r-- | packages/browser/src/helpers/webAuthnAbortService.ts | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/packages/browser/src/helpers/webAuthnAbortService.ts b/packages/browser/src/helpers/webAuthnAbortService.ts index 50e00ba..395ceea 100644 --- a/packages/browser/src/helpers/webAuthnAbortService.ts +++ b/packages/browser/src/helpers/webAuthnAbortService.ts @@ -1,13 +1,10 @@ -/** - * A way to cancel an existing WebAuthn request, for example to cancel a - * WebAuthn autofill authentication request for a manual authentication attempt. - */ -class WebAuthnAbortService { +class BaseWebAuthnAbortService { private controller: AbortController | undefined; /** * Prepare an abort signal that will help support multiple auth attempts without needing to - * reload the page + * reload the page. This is automatically called whenever `startRegistration()` and + * `startAuthentication()` are called. */ createNewAbortSignal() { // Abort any existing calls to navigator.credentials.create() or navigator.credentials.get() @@ -24,6 +21,28 @@ class WebAuthnAbortService { this.controller = newController; return newController.signal; } + + /** + * Manually cancel any active WebAuthn registration or authentication attempt. + */ + cancelCeremony() { + if (this.controller) { + const abortError = new Error( + 'Manually cancelling existing WebAuthn API call', + ); + abortError.name = 'AbortError'; + this.controller.abort(abortError); + + this.controller = undefined; + } + } } -export const webauthnAbortService = new WebAuthnAbortService(); +/** + * A service singleton to help ensure that only a single WebAuthn ceremony is active at a time. + * + * Users of **@simplewebauthn/browser** shouldn't typically need to use this, but it can help e.g. + * developers building projects that use client-side routing to better control the behavior of + * their UX in response to router navigation events. + */ +export const WebAuthnAbortService = new BaseWebAuthnAbortService(); |