Skip to content

Commit 090b468

Browse files
authored
More consistently early-return when WebAuthn is unavailable (#34)
Fixes #3
1 parent fadf145 commit 090b468

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/SDK.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Result<T, E> =
2424
| { ok: false, error: E, more?: unknown }
2525

2626
type WebAuthnError =
27+
| 'webauthn_unavailable'
2728
| 'timeout'
2829
| 'network_error'
2930
| 'bad_request'
@@ -63,14 +64,10 @@ class SDK {
6364
return !!window.PublicKeyCredential
6465
}
6566

66-
private requireWebAuthn() {
67-
// if (!this.isWebAuthnAvailable) {
68-
// throw new Error
69-
// }
70-
}
71-
7267
async startAuth(user: UserAuthenticationInfo): Promise<AuthResponse> {
73-
this.requireWebAuthn()
68+
if (!this.isWebAuthnAvailable) {
69+
return { ok: false, error: 'webauthn_unavailable' }
70+
}
7471
const res = await this.api('/auth/createOptions', { user }) as Result<CredentialRequestOptionsJSON, WebAuthnError>
7572
if (!res.ok) {
7673
return res
@@ -80,12 +77,14 @@ class SDK {
8077
}
8178

8279
async startRegister(user: UserRegistrationInfo): Promise<RegisterResponse> {
80+
if (!this.isWebAuthnAvailable) {
81+
return { ok: false, error: 'webauthn_unavailable' }
82+
}
8383
// If you do this inside the try/catch it seems to fail. Some sort of race
8484
// condition w/ the other request being canceled AFAICT. Doesn't make total
8585
// sense to me and may be a browser specific issue.
8686
const signal = this.cancelExistingRequests()
8787
try {
88-
this.requireWebAuthn()
8988
// If user info provided, send only the id or handle. Do NOT send name or
9089
// displayName.
9190
let remoteUserData: UserIdOrHandle | undefined
@@ -117,6 +116,9 @@ class SDK {
117116
}
118117

119118
async handleAutofill(callback: (arg0: AuthResponse) => void) {
119+
if (!this.isWebAuthnAvailable) {
120+
return false
121+
}
120122
if (!PublicKeyCredential.isConditionalMediationAvailable) {
121123
return false
122124
}

0 commit comments

Comments
 (0)