@@ -2,7 +2,9 @@ import {buildHeaders, handleTokenExpired} from "./helpers";
22import {
33 AddAuthenticatorRequest ,
44 AddAuthenticatorResponse ,
5+ AuthenticationOptsRequest ,
56 AuthenticationOptsResponse ,
7+ ChallengeRequest ,
68 ChallengeResponse ,
79 ErrorResponse ,
810 PasskeyAuthenticatorResponse ,
@@ -28,16 +30,23 @@ export class PasskeyApiClient {
2830 token,
2931 username,
3032 authenticatorAttachment,
33+ useCookies,
3134 } : { token : string } & RegistrationOptsRequest ) : Promise < RegistrationOptsResponse | ErrorResponse > {
3235 const body : RegistrationOptsRequest = Boolean ( authenticatorAttachment )
3336 ? { username, authenticatorAttachment}
3437 : { username} ;
3538
36- const response = await fetch ( `${ this . baseUrl } /client/user-authenticators/passkey/registration-options/web` , {
39+ const url = useCookies
40+ ? `${ this . baseUrl } /client/user-authenticators/passkey/registration-options/web`
41+ : `${ this . baseUrl } /client/user-authenticators/passkey/registration-options` ;
42+
43+ const credentials = useCookies ? "include" : "same-origin" ;
44+
45+ const response = await fetch ( url , {
3746 method : "POST" ,
3847 headers : buildHeaders ( { token, tenantId : this . tenantId } ) ,
3948 body : JSON . stringify ( body ) ,
40- credentials : "include" ,
49+ credentials,
4150 } ) ;
4251
4352 const responseJson = await response . json ( ) ;
@@ -47,12 +56,17 @@ export class PasskeyApiClient {
4756 return responseJson ;
4857 }
4958
50- async authenticationOptions ( { token} : { token ?: string } ) : Promise < AuthenticationOptsResponse | ErrorResponse > {
59+ async authenticationOptions ( {
60+ token,
61+ useCookies,
62+ } : {
63+ token ?: string ;
64+ } & AuthenticationOptsRequest ) : Promise < AuthenticationOptsResponse | ErrorResponse > {
5165 const response = await fetch ( `${ this . baseUrl } /client/user-authenticators/passkey/authentication-options` , {
5266 method : "POST" ,
5367 headers : buildHeaders ( { token, tenantId : this . tenantId } ) ,
5468 body : JSON . stringify ( { } ) ,
55- credentials : "include" ,
69+ credentials : useCookies ? "include" : "same-origin ",
5670 } ) ;
5771
5872 const responseJson = await response . json ( ) ;
@@ -81,17 +95,20 @@ export class PasskeyApiClient {
8195 token,
8296 registrationCredential,
8397 conditionalCreate,
98+ challengeId,
99+ useCookies,
84100 } : { token : string } & AddAuthenticatorRequest ) : Promise < AddAuthenticatorResponse | ErrorResponse > {
85101 const body : AddAuthenticatorRequest = {
86102 registrationCredential,
87103 conditionalCreate,
104+ challengeId,
88105 } ;
89106
90107 const response = await fetch ( `${ this . baseUrl } /client/user-authenticators/passkey` , {
91108 method : "POST" ,
92109 headers : buildHeaders ( { token, tenantId : this . tenantId } ) ,
93110 body : JSON . stringify ( body ) ,
94- credentials : "include" ,
111+ credentials : useCookies ? "include" : "same-origin ",
95112 } ) ;
96113
97114 const responseJson = await response . json ( ) ;
@@ -105,14 +122,15 @@ export class PasskeyApiClient {
105122 authenticationCredential,
106123 token,
107124 deviceId,
125+ useCookies,
108126 } : { token ?: string } & VerifyRequest ) : Promise < VerifyResponse | ErrorResponse > {
109127 const body : VerifyRequest = { authenticationCredential, deviceId} ;
110128
111129 const response = await fetch ( `${ this . baseUrl } /client/verify/passkey` , {
112130 method : "POST" ,
113131 headers : buildHeaders ( { token, tenantId : this . tenantId } ) ,
114132 body : JSON . stringify ( body ) ,
115- credentials : "include" ,
133+ credentials : useCookies ? "include" : "same-origin ",
116134 } ) ;
117135
118136 const responseJson = await response . json ( ) ;
@@ -139,14 +157,14 @@ export class PasskeyApiClient {
139157 return response . json ( ) ;
140158 }
141159
142- async challenge ( action : string ) : Promise < ChallengeResponse | ErrorResponse > {
143- const url = `${ this . baseUrl } /client/challenge/web` ;
160+ async challenge ( { action, useCookies } : ChallengeRequest ) : Promise < ChallengeResponse | ErrorResponse > {
161+ const url = useCookies ? `${ this . baseUrl } /client/challenge/web` : ` ${ this . baseUrl } /client/challenge `;
144162
145163 const response = await fetch ( url , {
146164 method : "POST" ,
147165 headers : buildHeaders ( { tenantId : this . tenantId } ) ,
148166 body : JSON . stringify ( { action} ) ,
149- credentials : "include" ,
167+ credentials : useCookies ? "include" : "same-origin ",
150168 } ) ;
151169
152170 const responseJson = await response . json ( ) ;
0 commit comments