diff options
author | Matthew Miller <matthew@millerti.me> | 2022-07-04 08:04:44 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-04 08:04:44 -0700 |
commit | 184cbb4457285817db4ded722d7b54528988e2e0 (patch) | |
tree | 3b895f8c273e924dd6fdd52c50fd2528575dc41d /packages/browser/src/helpers/browserSupportsWebAuthnAutofill.ts | |
parent | 524e7f881624f36aa17f406bb25fa23d02449652 (diff) | |
parent | 5a5b5a3bdf7c709493fc9e63f0f02eed99f25baf (diff) |
Merge pull request #214 from MasterKale/feat/conditional-ui
feat/conditional-ui
Diffstat (limited to 'packages/browser/src/helpers/browserSupportsWebAuthnAutofill.ts')
-rw-r--r-- | packages/browser/src/helpers/browserSupportsWebAuthnAutofill.ts | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/packages/browser/src/helpers/browserSupportsWebAuthnAutofill.ts b/packages/browser/src/helpers/browserSupportsWebAuthnAutofill.ts new file mode 100644 index 0000000..ffca77d --- /dev/null +++ b/packages/browser/src/helpers/browserSupportsWebAuthnAutofill.ts @@ -0,0 +1,28 @@ +/* eslint-disable @typescript-eslint/ban-ts-comment */ +import { PublicKeyCredentialFuture } from '@simplewebauthn/typescript-types'; + +/** + * Determine if the browser supports conditional UI, so that WebAuthn credentials can + * be shown to the user in the browser's typical password autofill popup. + */ +export async function browserSupportsWebAuthnAutofill(): Promise<boolean> { + // Just for Chrome Canary right now; the PublicKeyCredential logic below is the real API + // @ts-ignore + if (navigator.credentials.conditionalMediationSupported) { + return true; + } + + /** + * I don't like the `as unknown` here but there's a `declare var PublicKeyCredential` in + * TS' DOM lib that's making it difficult for me to just go `as PublicKeyCredentialFuture` as I + * want. I think I'm fine with this for now since it's _supposed_ to be temporary, until TS types + * have a chance to catch up. + */ + const globalPublicKeyCredential = + window.PublicKeyCredential as unknown as PublicKeyCredentialFuture; + + return ( + globalPublicKeyCredential.isConditionalMediationAvailable !== undefined && + globalPublicKeyCredential.isConditionalMediationAvailable() + ); +} |