From 7943d50119d8e240f98a32e96d4d9ce950f054b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gangloff?= Date: Thu, 4 May 2023 18:53:29 +0200 Subject: [PATCH] fix(auth): :adhesive_bandage: refresh the token only when the API responds 401 --- package.json | 2 +- src/index.ts | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index a2819ab..5891532 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "scolengo-api", - "version": "1.1.2", + "version": "1.1.3", "types": "types/index.d.ts", "homepage": "https://github.com/maelgangloff/scolengo-api#readme", "bugs": { diff --git a/src/index.ts b/src/index.ts index 8c231e9..40a17c4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -930,14 +930,18 @@ export class Skolengo { try { return await this.httpClient.request(axiosConfig) } catch (e) { - const error = e as AxiosError<{ errors: SkolengoError[] }, D> + const error = e as AxiosError const response = error.response - if (((response?.data.errors) != null) && (response.data.errors.length > 0)) { + + if (response === undefined || response.status !== 401) throw error + if (response.data.errors instanceof Array && response.data.errors.length > 0) { throw new SkolengoError(response.data.errors[0]) } const newTokenSet = await this.oidClient.refresh(this.tokenSet) + this.onTokenRefresh(newTokenSet) this.tokenSet = newTokenSet + return await this.httpClient.request({ ...axiosConfig, headers: {