11import { encodeURLEncodedBase64 , decodeURLEncodedBase64 } from '../utils.js' ;
2- import { showElem , hideElem } from '../utils/dom.js' ;
2+ import { showElem } from '../utils/dom.js' ;
33
44const { appSubUrl, csrfToken} = window . config ;
55
66export async function initUserAuthWebAuthn ( ) {
7- hideElem ( '#webauthn-error' ) ;
8-
97 const elPrompt = document . querySelector ( '.user.signin.webauthn-prompt' ) ;
108 if ( ! elPrompt ) {
119 return ;
@@ -25,21 +23,21 @@ export async function initUserAuthWebAuthn() {
2523 for ( const cred of options . publicKey . allowCredentials ) {
2624 cred . id = decodeURLEncodedBase64 ( cred . id ) ;
2725 }
28- const credential = await navigator . credentials . get ( {
29- publicKey : options . publicKey
30- } ) ;
3126 try {
27+ const credential = await navigator . credentials . get ( {
28+ publicKey : options . publicKey
29+ } ) ;
3230 await verifyAssertion ( credential ) ;
3331 } catch ( err ) {
3432 if ( ! options . publicKey . extensions ?. appid ) {
3533 webAuthnError ( 'general' , err . message ) ;
3634 return ;
3735 }
3836 delete options . publicKey . extensions . appid ;
39- const credential = await navigator . credentials . get ( {
40- publicKey : options . publicKey
41- } ) ;
4237 try {
38+ const credential = await navigator . credentials . get ( {
39+ publicKey : options . publicKey
40+ } ) ;
4341 await verifyAssertion ( credential ) ;
4442 } catch ( err ) {
4543 webAuthnError ( 'general' , err . message ) ;
@@ -48,7 +46,7 @@ export async function initUserAuthWebAuthn() {
4846}
4947
5048async function verifyAssertion ( assertedCredential ) {
51- // Move data into Arrays incase it is super long
49+ // Move data into Arrays in case it is super long
5250 const authData = new Uint8Array ( assertedCredential . response . authenticatorData ) ;
5351 const clientDataJSON = new Uint8Array ( assertedCredential . response . clientDataJSON ) ;
5452 const rawId = new Uint8Array ( assertedCredential . rawId ) ;
@@ -137,15 +135,11 @@ function webAuthnError(errorType, message) {
137135
138136function detectWebAuthnSupport ( ) {
139137 if ( ! window . isSecureContext ) {
140- document . getElementById ( 'register-button' ) . disabled = true ;
141- document . getElementById ( 'login-button' ) . disabled = true ;
142138 webAuthnError ( 'insecure' ) ;
143139 return false ;
144140 }
145141
146142 if ( typeof window . PublicKeyCredential !== 'function' ) {
147- document . getElementById ( 'register-button' ) . disabled = true ;
148- document . getElementById ( 'login-button' ) . disabled = true ;
149143 webAuthnError ( 'browser' ) ;
150144 return false ;
151145 }
@@ -158,15 +152,13 @@ export function initUserAuthWebAuthnRegister() {
158152 if ( ! elRegister ) {
159153 return ;
160154 }
161-
162- hideElem ( '#webauthn-error' ) ;
163-
164- elRegister . addEventListener ( 'click' , ( e ) => {
155+ if ( ! detectWebAuthnSupport ( ) ) {
156+ elRegister . disabled = true ;
157+ return ;
158+ }
159+ elRegister . addEventListener ( 'click' , async ( e ) => {
165160 e . preventDefault ( ) ;
166- if ( ! detectWebAuthnSupport ( ) ) {
167- return ;
168- }
169- webAuthnRegisterRequest ( ) ;
161+ await webAuthnRegisterRequest ( ) ;
170162 } ) ;
171163}
172164
@@ -203,15 +195,12 @@ async function webAuthnRegisterRequest() {
203195 }
204196 }
205197
206- let credential ;
207198 try {
208- credential = await navigator . credentials . create ( {
199+ const credential = await navigator . credentials . create ( {
209200 publicKey : options . publicKey
210201 } ) ;
202+ await webauthnRegistered ( credential ) ;
211203 } catch ( err ) {
212204 webAuthnError ( 'unknown' , err ) ;
213- return ;
214205 }
215-
216- webauthnRegistered ( credential ) ;
217206}
0 commit comments