@@ -20,6 +20,7 @@ async function syncTokens(
2020 const { tokens, status } = await synchroniseTokensAsync ( oidc ) (
2121 updateTokens ,
2222 0 ,
23+ 0 ,
2324 forceRefresh ,
2425 extras ,
2526 scope ,
@@ -200,7 +201,8 @@ const synchroniseTokensAsync =
200201 ( oidc : Oidc ) =>
201202 async (
202203 updateTokens ,
203- index = 0 ,
204+ tryNumber = 0 ,
205+ backgroundTry = 0 ,
204206 forceRefresh = false ,
205207 extras : StringMap = null ,
206208 scope : string = null ,
@@ -209,6 +211,9 @@ const synchroniseTokensAsync =
209211 return { tokens : oidc . tokens , status : 'GIVE_UP' } ;
210212 }
211213 let numberTryOnline = 6 ;
214+ const maxTries = 5 ;
215+ const maxBackgroundTries = 5 ;
216+
212217 while ( ! navigator . onLine && numberTryOnline > 0 ) {
213218 await sleepAsync ( { milliseconds : 1000 } ) ;
214219 numberTryOnline -- ;
@@ -217,16 +222,15 @@ const synchroniseTokensAsync =
217222 } ) ;
218223 }
219224 const isDocumentHidden = document . hidden ;
220- const nextIndex = isDocumentHidden ? index : index + 1 ;
221- if ( index > 4 ) {
222- if ( isDocumentHidden ) {
223- return { tokens : oidc . tokens , status : 'GIVE_UP' } ;
224- } else {
225- updateTokens ( null ) ;
226- oidc . publishEvent ( eventNames . refreshTokensAsync_error , { message : 'refresh token' } ) ;
227- return { tokens : null , status : 'SESSION_LOST' } ;
228- }
225+ const nextTry = isDocumentHidden ? tryNumber : tryNumber + 1 ;
226+ const nextBackgroundTry = isDocumentHidden ? backgroundTry + 1 : backgroundTry ;
227+
228+ if ( tryNumber >= maxTries || backgroundTry >= maxBackgroundTries ) {
229+ updateTokens ( null ) ;
230+ oidc . publishEvent ( eventNames . refreshTokensAsync_error , { message : 'refresh token' } ) ;
231+ return { tokens : null , status : 'SESSION_LOST' } ;
229232 }
233+
230234 if ( ! extras ) {
231235 extras = { } ;
232236 }
@@ -281,7 +285,8 @@ const synchroniseTokensAsync =
281285 } ) ;
282286 return await synchroniseTokensAsync ( oidc ) (
283287 updateTokens ,
284- nextIndex ,
288+ nextTry ,
289+ nextBackgroundTry ,
285290 forceRefresh ,
286291 extras ,
287292 scope ,
@@ -331,7 +336,7 @@ const synchroniseTokensAsync =
331336 return { tokens : oidc . tokens , status : 'GIVE_UP' } ;
332337 }
333338
334- oidc . publishEvent ( eventNames . refreshTokensAsync_begin , { tryNumber : index } ) ;
339+ oidc . publishEvent ( eventNames . refreshTokensAsync_begin , { tryNumber : tryNumber } ) ;
335340 return await localSilentLoginAsync ( ) ;
336341 default : {
337342 if (
@@ -346,8 +351,10 @@ const synchroniseTokensAsync =
346351 oidc . publishEvent ( eventNames . refreshTokensAsync_begin , {
347352 refreshToken : tokens . refreshToken ,
348353 status,
349- tryNumber : index ,
354+ tryNumber : tryNumber ,
355+ backgroundTry : backgroundTry ,
350356 } ) ;
357+
351358 if ( ! tokens . refreshToken ) {
352359 return await localSilentLoginAsync ( ) ;
353360 }
@@ -444,7 +451,8 @@ const synchroniseTokensAsync =
444451
445452 return await synchroniseTokensAsync ( oidc ) (
446453 updateTokens ,
447- nextIndex ,
454+ nextTry ,
455+ nextBackgroundTry ,
448456 forceRefresh ,
449457 extras ,
450458 scope ,
@@ -456,6 +464,7 @@ const synchroniseTokensAsync =
456464 }
457465 } catch ( exception : any ) {
458466 console . error ( exception ) ;
467+
459468 oidc . publishEvent ( eventNames . refreshTokensAsync_silent_error , {
460469 message : 'exception' ,
461470 exception : exception . message ,
@@ -465,7 +474,14 @@ const synchroniseTokensAsync =
465474 // so we need to brake calls chain and delay next call
466475 return new Promise ( ( resolve , reject ) => {
467476 setTimeout ( ( ) => {
468- synchroniseTokensAsync ( oidc ) ( updateTokens , nextIndex , forceRefresh , extras , scope )
477+ synchroniseTokensAsync ( oidc ) (
478+ updateTokens ,
479+ nextTry ,
480+ nextBackgroundTry ,
481+ forceRefresh ,
482+ extras ,
483+ scope ,
484+ )
469485 . then ( resolve )
470486 . catch ( reject ) ;
471487 } , 1000 ) ;
0 commit comments