@@ -22,7 +22,6 @@ import type {
2222 ConnectManageListRsp ,
2323} from '../api/v2' ;
2424import { CorbadoConnectApi , PasskeyEventType } from '../api/v2' ;
25- import type { AuthProcess } from '../models/authProcess' ;
2625import { ConnectFlags } from '../models/connect/connectFlags' ;
2726import { ConnectInvitation } from '../models/connect/connectInvitation' ;
2827import { ConnectLastLogin } from '../models/connect/connectLastLogin' ;
@@ -93,7 +92,7 @@ export class ConnectService {
9392 return out ;
9493 }
9594
96- #setApisV2( process ?: AuthProcess ) : void {
95+ #setApisV2( process ?: ConnectProcess ) : void {
9796 let frontendApiUrl = this . #getDefaultFrontendApiUrl( ) ;
9897 if ( process ?. frontendApiUrl && process ?. frontendApiUrl . length > 0 ) {
9998 frontendApiUrl = process . frontendApiUrl ;
@@ -124,9 +123,11 @@ export class ConnectService {
124123
125124 async loginInit ( abortController : AbortController ) : Promise < Result < ConnectLoginInitData , CorbadoError > > {
126125 const existingProcess = ConnectProcess . loadFromStorage ( this . #projectId) ;
126+ const maybeLoginData = existingProcess ?. getValidLoginData ( ) ;
127127 if (
128- existingProcess ?. loginData &&
129- ! existingProcess ?. loginData . loginAllowed &&
128+ existingProcess &&
129+ maybeLoginData &&
130+ ! maybeLoginData . loginAllowed &&
130131 ConnectInvitation . loadFromStorage ( ) ?. token
131132 ) {
132133 existingProcess . resetLoginData ( ) . persistToStorage ( ) ;
@@ -135,8 +136,8 @@ export class ConnectService {
135136 this . #setApisV2( existingProcess ) ;
136137
137138 // process has already been initialized
138- if ( existingProcess ?. loginData ) {
139- return Ok ( existingProcess . loginData ) ;
139+ if ( maybeLoginData ) {
140+ return Ok ( maybeLoginData ) ;
140141 }
141142 }
142143
@@ -168,20 +169,24 @@ export class ConnectService {
168169 loginAllowed : res . val . loginAllowed ,
169170 conditionalUIChallenge : res . val . conditionalUIChallenge ?? null ,
170171 flags : flags . getItemsObject ( ) ,
172+ expiresAt : res . val . expiresAt ,
171173 } ;
172174
173- // update local state
174- const newProcess = new ConnectProcess (
175- res . val . token ,
176- this . #projectId,
177- res . val . expiresAt ,
178- res . val . frontendApiUrl ,
179- loginData ,
180- null ,
181- null ,
182- ) ;
183- this . #setApisV2( newProcess ) ;
184- newProcess . persistToStorage ( ) ;
175+ if ( existingProcess && existingProcess . id === res . val . token ) {
176+ const p = existingProcess . copyWithLoginData ( loginData ) ;
177+ p . persistToStorage ( ) ;
178+ } else {
179+ const newProcess = new ConnectProcess (
180+ res . val . token ,
181+ this . #projectId,
182+ res . val . frontendApiUrl ,
183+ loginData ,
184+ null ,
185+ null ,
186+ ) ;
187+ this . #setApisV2( newProcess ) ;
188+ newProcess . persistToStorage ( ) ;
189+ }
185190
186191 // persist flags
187192 flags . persistToStorage ( this . #projectId) ;
@@ -192,7 +197,7 @@ export class ConnectService {
192197 async #getExistingProcess( generator : ( ) => Promise < Result < unknown , CorbadoError > > ) : Promise < ConnectProcess | null > {
193198 const existingProcess = ConnectProcess . loadFromStorage ( this . #projectId) ;
194199 if ( existingProcess ) {
195- log . debug ( 'process found' , existingProcess . expiresAt ) ;
200+ log . debug ( 'process found' ) ;
196201 return existingProcess ;
197202 }
198203
@@ -287,8 +292,9 @@ export class ConnectService {
287292 this . #setApisV2( existingProcess ) ;
288293
289294 // process has already been initialized
290- if ( existingProcess ?. appendData ) {
291- return Ok ( existingProcess . appendData ) ;
295+ const maybeAppendData = existingProcess ?. getValidAppendData ( ) ;
296+ if ( maybeAppendData ) {
297+ return Ok ( maybeAppendData ) ;
292298 }
293299 }
294300
@@ -311,17 +317,17 @@ export class ConnectService {
311317 const appendData : ConnectAppendInitData = {
312318 appendAllowed : res . val . appendAllowed ,
313319 flags : flags . getItemsObject ( ) ,
320+ expiresAt : res . val . expiresAt ,
314321 } ;
315322
316323 // update local state with process
317- if ( existingProcess ) {
318- const p = existingProcess . copyWithAppendData ( appendData , res . val . expiresAt ) ;
324+ if ( existingProcess && existingProcess . id === res . val . processID ) {
325+ const p = existingProcess . copyWithAppendData ( appendData ) ;
319326 p . persistToStorage ( ) ;
320327 } else {
321328 const newProcess = new ConnectProcess (
322329 res . val . processID ,
323330 this . #projectId,
324- res . val . expiresAt ,
325331 res . val . frontendApiUrl ,
326332 null ,
327333 appendData ,
@@ -435,8 +441,9 @@ export class ConnectService {
435441 this . #setApisV2( existingProcess ) ;
436442
437443 // process has already been initialized
438- if ( existingProcess ?. manageData ) {
439- return Ok ( existingProcess . manageData ) ;
444+ const maybeManageData = existingProcess ?. getValidManageData ( ) ;
445+ if ( maybeManageData ) {
446+ return Ok ( maybeManageData ) ;
440447 }
441448 }
442449
@@ -459,17 +466,17 @@ export class ConnectService {
459466 const manageData : ConnectManageInitData = {
460467 manageAllowed : res . val . manageAllowed ,
461468 flags : flags . getItemsObject ( ) ,
469+ expiresAt : res . val . expiresAt ,
462470 } ;
463471
464472 // update local state with process
465- if ( existingProcess ) {
466- const p = existingProcess . copyWithManageData ( manageData , res . val . expiresAt ) ;
473+ if ( existingProcess && existingProcess . id === res . val . processID ) {
474+ const p = existingProcess . copyWithManageData ( manageData ) ;
467475 p . persistToStorage ( ) ;
468476 } else {
469477 const newProcess = new ConnectProcess (
470478 res . val . processID ,
471479 this . #projectId,
472- res . val . expiresAt ,
473480 res . val . frontendApiUrl ,
474481 null ,
475482 null ,
0 commit comments