@@ -56,7 +56,6 @@ export function initialize(
56
56
return ;
57
57
}
58
58
59
- let timer : NodeJS . Timeout | null = null ;
60
59
/*
61
60
only set token interceptor if we're using a non-JWT key.
62
61
Otherwise, we'll set it later once we generate the JWT
@@ -79,26 +78,38 @@ export function initialize(
79
78
@param { string } jwt - JWT token to decode
80
79
@param { (...args: any ) => Promise<any> } callback - promise to invoke before expiry
81
80
*/
82
- const handleTokenExpiration = (
83
- jwt : string ,
84
- callback : ( ...args : any ) => Promise < any >
85
- ) => {
86
- const expTime = getEpochExpiryTimeInMS ( jwt ) ;
87
- const millisecondsToExpired = getEpochDifferenceInMS ( Date . now ( ) , expTime ) ;
88
- timer = setTimeout ( ( ) => {
81
+ const createTokenExpirationTimer = ( ) => {
82
+ let timer : NodeJS . Timeout | null ;
83
+
84
+ return ( jwt : string , callback ?: ( ...args : any ) => Promise < any > ) => {
89
85
if ( timer ) {
90
86
/* clear existing timeout on JWT refresh */
91
87
clearTimeout ( timer ) ;
88
+ timer = null ;
92
89
}
93
- /* get new token */
94
- return callback ( ) . catch ( ( e ) => {
95
- console . warn ( e ) ;
96
- console . warn ( 'Could not refresh JWT. Try identifying the user again.' ) ;
97
- } ) ;
98
- /* try to refresh one minute until expiry */
99
- } , millisecondsToExpired - ONE_MINUTE ) ;
90
+
91
+ if ( callback ) {
92
+ const expTime = getEpochExpiryTimeInMS ( jwt ) ;
93
+ const millisecondsToExpired = getEpochDifferenceInMS (
94
+ Date . now ( ) ,
95
+ expTime
96
+ ) ;
97
+ timer = setTimeout ( ( ) => {
98
+ /* get new token */
99
+ return callback ( ) . catch ( ( e ) => {
100
+ console . warn ( e ) ;
101
+ console . warn (
102
+ 'Could not refresh JWT. Try identifying the user again.'
103
+ ) ;
104
+ } ) ;
105
+ /* try to refresh one minute until expiry */
106
+ } , millisecondsToExpired - ONE_MINUTE ) ;
107
+ }
108
+ } ;
100
109
} ;
101
110
111
+ const handleTokenExpiration = createTokenExpirationTimer ( ) ;
112
+
102
113
const addEmailToRequest = ( email : string ) => {
103
114
userInterceptor = baseAxiosRequest . interceptors . request . use ( ( config ) => {
104
115
/*
@@ -454,11 +465,9 @@ export function initialize(
454
465
/*
455
466
important here to clear the timer first since there was one set up
456
467
previously the first time the JWT was generated.
457
- */
458
- if ( timer ) {
459
- clearTimeout ( timer ) ;
460
- }
461
468
469
+ handleTokenExpiration will clear the timeout for us.
470
+ */
462
471
handleTokenExpiration ( newToken , ( ) => {
463
472
/* re-run the JWT generation */
464
473
return doRequest ( { email : newEmail } ) . catch ( ( e ) => {
@@ -576,9 +585,8 @@ export function initialize(
576
585
} ;
577
586
return {
578
587
clearRefresh : ( ) => {
579
- if ( timer ) {
580
- clearTimeout ( timer ) ;
581
- }
588
+ /* this will just clear the existing timeout */
589
+ handleTokenExpiration ( '' ) ;
582
590
} ,
583
591
setEmail : ( email : string ) => {
584
592
/* clear previous user */
@@ -709,10 +717,8 @@ export function initialize(
709
717
/* clear fetched in-app messages */
710
718
clearMessages ( ) ;
711
719
712
- if ( timer ) {
713
- /* prevent any refreshing of JWT */
714
- clearTimeout ( timer ) ;
715
- }
720
+ /* this will just clear the existing timeout */
721
+ handleTokenExpiration ( '' ) ;
716
722
717
723
if ( typeof authInterceptor === 'number' ) {
718
724
/* stop adding auth token to requests */
0 commit comments