Skip to content

Commit c26ce0b

Browse files
committed
Start refresh after cache check
1 parent 697f7c3 commit c26ce0b

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

packages/app-check/src/internal-api.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,8 @@ export function addTokenListener(
168168
...state,
169169
tokenObservers: [...state.tokenObservers, tokenObserver]
170170
};
171-
/**
172-
* Invoke the listener with the valid token, then start the token refresher
173-
*/
174-
if (!newState.tokenRefresher) {
175-
const tokenRefresher = createTokenRefresher(appCheck);
176-
newState.tokenRefresher = tokenRefresher;
177-
}
178171

179-
// Create the refresher but don't start it if `isTokenAutoRefreshEnabled`
180-
// is not true.
181-
if (!newState.tokenRefresher.isRunning() && state.isTokenAutoRefreshEnabled) {
182-
newState.tokenRefresher.start();
183-
}
172+
let cacheCheckPromise = Promise.resolve();
184173

185174
// Invoke the listener async immediately if there is a valid token
186175
// in memory.
@@ -194,7 +183,7 @@ export function addTokenListener(
194183
} else if (state.token == null) {
195184
// Only check cache if there was no token. If the token was invalid,
196185
// skip this and rely on exchange endpoint.
197-
void state
186+
cacheCheckPromise = state
198187
.cachedTokenPromise! // Storage token promise. Always populated in `activate()`.
199188
.then(cachedToken => {
200189
if (cachedToken && isValid(cachedToken)) {
@@ -206,6 +195,25 @@ export function addTokenListener(
206195
});
207196
}
208197

198+
// Wait for any cached token promise to resolve before starting the token
199+
// refresher. The refresher checks to see if there is an existing token
200+
// in state and calls the exchange endpoint if not. We should first let the
201+
// IndexedDB check have a chance to populate state if it can.
202+
void cacheCheckPromise.then(() => {
203+
if (!newState.tokenRefresher) {
204+
const tokenRefresher = createTokenRefresher(appCheck);
205+
newState.tokenRefresher = tokenRefresher;
206+
}
207+
// Create the refresher but don't start it if `isTokenAutoRefreshEnabled`
208+
// is not true.
209+
if (
210+
!newState.tokenRefresher.isRunning() &&
211+
state.isTokenAutoRefreshEnabled
212+
) {
213+
newState.tokenRefresher.start();
214+
}
215+
});
216+
209217
setState(app, newState);
210218
}
211219

packages/app-check/src/providers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export class ReCaptchaV3Provider implements AppCheckProvider {
117117
/**
118118
* Set throttle data to block requests until after a certain time
119119
* depending on the failed request's status code.
120-
* @param httpStatus Status code of failed request.
120+
* @param httpStatus - Status code of failed request.
121121
* @returns Data about current throttle state and expiration time.
122122
*/
123123
private _setBackoff(httpStatus: number): ThrottleData {

0 commit comments

Comments
 (0)