@@ -27,40 +27,54 @@ const passkeys = {
27
27
const publicKeyCredential = await webauthnJSON . create ( credentialCreateOptions ) ;
28
28
29
29
// Return encoded PublicKeyCredential to server
30
- await fetch ( "/cbsecurity/passkeys/registration" , {
30
+ const registrationResponse = await fetch ( "/cbsecurity/passkeys/registration" , {
31
31
method : "POST" ,
32
32
headers : {
33
33
"Content-Type" : "application/json"
34
34
} ,
35
35
body : JSON . stringify ( {
36
36
"publicKeyCredentialJson" : JSON . stringify ( publicKeyCredential )
37
37
} )
38
- } )
39
- window . location = redirectLocation ;
38
+ } ) ;
39
+
40
+ if ( registrationResponse . ok ) {
41
+ window . location = redirectLocation ;
42
+ } else {
43
+ console . error ( "cbsecurity-passkeys - Registration failed:" , registrationResponse ) ;
44
+ }
40
45
} ,
41
- async login ( username , redirectLocation = "/" ) {
42
- // Make the call that returns the credentialGetJson above
43
- const credentialGetOptions = await fetch ( "/cbsecurity/passkeys/authentication/new?" + new URLSearchParams ( {
44
- "username" : username
45
- } ) )
46
- . then ( resp => resp . json ( ) )
47
- . then ( json => JSON . parse ( json ) ) ;
46
+ async login ( username , redirectLocation = "/" , additionalParams = { } ) {
47
+ if ( ! username ) {
48
+ username = "" ;
49
+ }
50
+ // Make the call that returns the credentialGetJson above
51
+ const credentialGetOptions = await fetch ( "/cbsecurity/passkeys/authentication/new?" + new URLSearchParams ( {
52
+ ...additionalParams ,
53
+ "username" : username ,
54
+ } ) )
55
+ . then ( resp => resp . json ( ) )
56
+ . then ( json => JSON . parse ( json ) ) ;
57
+
58
+ // Call WebAuthn ceremony using webauthn-json wrapper
59
+ const publicKeyCredential = await webauthnJSON . get ( credentialGetOptions ) ;
48
60
49
- // Call WebAuthn ceremony using webauthn-json wrapper
50
- const publicKeyCredential = await webauthnJSON . get ( credentialGetOptions ) ;
61
+ // Return encoded PublicKeyCredential to server
62
+ const authenticationResponse = await fetch ( "/cbsecurity/passkeys/authentication" , {
63
+ method : "POST" ,
64
+ headers : {
65
+ "Content-Type" : "application/json"
66
+ } ,
67
+ body : JSON . stringify ( {
68
+ ...additionalParams ,
69
+ "publicKeyCredentialJson" : JSON . stringify ( publicKeyCredential )
70
+ } )
71
+ } ) ;
51
72
52
- // Return encoded PublicKeyCredential to server
53
- await fetch ( "/cbsecurity/passkeys/authentication" , {
54
- method : "POST" ,
55
- headers : {
56
- "Content-Type" : "application/json"
57
- } ,
58
- body : JSON . stringify ( {
59
- "publicKeyCredentialJson" : JSON . stringify ( publicKeyCredential )
60
- } )
61
- } ) . then ( ( ) => {
62
- window . location = redirectLocation ;
63
- } ) ;
73
+ if ( authenticationResponse . ok ) {
74
+ window . location = redirectLocation ;
75
+ } else {
76
+ console . error ( "cbsecurity-passkeys - Authentication failed:" , authenticationResponse ) ;
77
+ }
64
78
} ,
65
79
} ;
66
80
0 commit comments