Skip to content

Commit 2af1b12

Browse files
committed
Fix continuous TDEI auth refresh
1 parent 75de266 commit 2af1b12

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

services/tdei.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,13 @@ export class TdeiClient extends BaseHttpClient implements ICancelableClient {
169169
}
170170

171171
async refreshToken() {
172-
const response = await super._send('refresh-token', 'POST', this.#auth.refreshToken);
173-
const body = await response.json();
172+
const response = await super._post('refresh-token', this.#auth.refreshToken);
173+
174+
if (response.status === 401) {
175+
this.#auth.clear();
176+
}
174177

175-
this.#setAuth(this.#auth.username, body);
178+
this.#setAuth(this.#auth.username, await response.json());
176179
}
177180

178181
async tryRefreshAuth() {
@@ -188,14 +191,15 @@ export class TdeiClient extends BaseHttpClient implements ICancelableClient {
188191
this.stopAutoAuthRefresh();
189192

190193
if (!this.#auth.refreshTokenExpired) {
191-
const refreshFn = this.refreshToken.bind(this);
194+
const refreshFn = this.#onAutoRefreshToken.bind(this);
192195
this.#refreshTimer = setTimeout(refreshFn, this.#auth.nextRefreshMs);
193196
console.info(`Refreshing TDEI tokens in ${this.#auth.nextRefreshMs} ms.`)
194197
}
195198
}
196199

197200
stopAutoAuthRefresh() {
198201
clearTimeout(this.#refreshTimer);
202+
this.#refreshTimer = undefined;
199203
}
200204

201205
async getDatasetInfo(tdeiRecordId: string) {
@@ -346,7 +350,10 @@ export class TdeiClient extends BaseHttpClient implements ICancelableClient {
346350
this.#auth.store();
347351

348352
this.#setAuthHeader();
349-
this.restartAutoAuthRefresh();
353+
354+
if (this.#refreshTimer) {
355+
this.restartAutoAuthRefresh();
356+
}
350357
}
351358

352359
#setAuthHeader() {
@@ -355,6 +362,14 @@ export class TdeiClient extends BaseHttpClient implements ICancelableClient {
355362
}
356363
}
357364

365+
async #onAutoRefreshToken() {
366+
await this.refreshToken();
367+
368+
if (this.#auth.ok) {
369+
this.restartAutoAuthRefresh();
370+
}
371+
}
372+
358373
async _send(url: string, method: string, body?: any, config?: object): Promise<Response> {
359374
try {
360375
if (this.#auth.needsRefresh) {

0 commit comments

Comments
 (0)