Skip to content

Commit e8a727d

Browse files
authored
Merge c3995b5 into e35db6f
2 parents e35db6f + c3995b5 commit e8a727d

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ export async function getToken(
8484
}
8585
}
8686

87+
if (token && !isValid(token)) {
88+
// If an invalid token was found in memory or indexedDB, clear token from
89+
// memory and the local variable.
90+
setState(app, { ...state, token: undefined });
91+
token = undefined;
92+
}
93+
8794
// Return the cached token (from either memory or indexedDB) if it's valid
8895
if (!forceRefresh && token && isValid(token)) {
8996
return {
@@ -107,14 +114,14 @@ export async function getToken(
107114
state.exchangeTokenPromise = exchangeToken(
108115
getExchangeDebugTokenRequest(app, await getDebugToken()),
109116
appCheck.heartbeatServiceProvider
110-
).then(token => {
117+
).finally(() => {
118+
// Clear promise when settled - either resolved or rejected.
111119
state.exchangeTokenPromise = undefined;
112-
return token;
113120
});
114121
shouldCallListeners = true;
115122
}
116123
const tokenFromDebugExchange: AppCheckTokenInternal =
117-
await state.exchangeTokenPromise;
124+
await state.exchangeTokenPromise!;
118125
// Write debug token to indexedDB.
119126
await writeTokenToStorage(app, tokenFromDebugExchange);
120127
// Write debug token to state.
@@ -131,9 +138,9 @@ export async function getToken(
131138
// state.provider is populated in initializeAppCheck()
132139
// ensureActivated() at the top of this function checks that
133140
// initializeAppCheck() has been called.
134-
state.exchangeTokenPromise = state.provider!.getToken().then(token => {
141+
state.exchangeTokenPromise = state.provider!.getToken().finally(() => {
142+
// Clear promise when settled - either resolved or rejected.
135143
state.exchangeTokenPromise = undefined;
136-
return token;
137144
});
138145
shouldCallListeners = true;
139146
}
@@ -151,7 +158,7 @@ export async function getToken(
151158
}
152159

153160
let interopTokenResult: AppCheckTokenResult | undefined;
154-
if (!token) {
161+
if (!token || error) {
155162
// if token is undefined, there must be an error.
156163
// we return a dummy token along with the error
157164
interopTokenResult = makeDummyTokenResult(error!);
@@ -290,7 +297,7 @@ function createTokenRefresher(appCheck: AppCheckService): Refresher {
290297
let nextRefreshTimeMillis =
291298
state.token.issuedAtTimeMillis +
292299
(state.token.expireTimeMillis - state.token.issuedAtTimeMillis) *
293-
0.5 +
300+
0.5 +
294301
5 * 60 * 1000;
295302
// Do not allow refresh time to be past (expireTime - 5 minutes)
296303
const latestAllowableRefresh =

0 commit comments

Comments
 (0)