Skip to content

Commit ed97b0f

Browse files
clear timeout when new auth timer is started (#121)
1 parent d5bdb83 commit ed97b0f

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

src/authorization/authorization.ts

+32-26
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export function initialize(
5656
return;
5757
}
5858

59-
let timer: NodeJS.Timeout | null = null;
6059
/*
6160
only set token interceptor if we're using a non-JWT key.
6261
Otherwise, we'll set it later once we generate the JWT
@@ -79,26 +78,38 @@ export function initialize(
7978
@param { string } jwt - JWT token to decode
8079
@param { (...args: any ) => Promise<any> } callback - promise to invoke before expiry
8180
*/
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>) => {
8985
if (timer) {
9086
/* clear existing timeout on JWT refresh */
9187
clearTimeout(timer);
88+
timer = null;
9289
}
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+
};
100109
};
101110

111+
const handleTokenExpiration = createTokenExpirationTimer();
112+
102113
const addEmailToRequest = (email: string) => {
103114
userInterceptor = baseAxiosRequest.interceptors.request.use((config) => {
104115
/*
@@ -454,11 +465,9 @@ export function initialize(
454465
/*
455466
important here to clear the timer first since there was one set up
456467
previously the first time the JWT was generated.
457-
*/
458-
if (timer) {
459-
clearTimeout(timer);
460-
}
461468
469+
handleTokenExpiration will clear the timeout for us.
470+
*/
462471
handleTokenExpiration(newToken, () => {
463472
/* re-run the JWT generation */
464473
return doRequest({ email: newEmail }).catch((e) => {
@@ -576,9 +585,8 @@ export function initialize(
576585
};
577586
return {
578587
clearRefresh: () => {
579-
if (timer) {
580-
clearTimeout(timer);
581-
}
588+
/* this will just clear the existing timeout */
589+
handleTokenExpiration('');
582590
},
583591
setEmail: (email: string) => {
584592
/* clear previous user */
@@ -709,10 +717,8 @@ export function initialize(
709717
/* clear fetched in-app messages */
710718
clearMessages();
711719

712-
if (timer) {
713-
/* prevent any refreshing of JWT */
714-
clearTimeout(timer);
715-
}
720+
/* this will just clear the existing timeout */
721+
handleTokenExpiration('');
716722

717723
if (typeof authInterceptor === 'number') {
718724
/* stop adding auth token to requests */

0 commit comments

Comments
 (0)