Skip to content

Commit

Permalink
fix(auth): 🩹 refresh the token only when the API responds 401
Browse files Browse the repository at this point in the history
  • Loading branch information
maelgangloff committed May 4, 2023
1 parent 316a916 commit 7943d50
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -930,14 +930,18 @@ export class Skolengo {
try {
return await this.httpClient.request<T, R, D>(axiosConfig)
} catch (e) {
const error = e as AxiosError<{ errors: SkolengoError[] }, D>
const error = e as AxiosError<any>
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<T, R, D>({
...axiosConfig,
headers: {
Expand Down

0 comments on commit 7943d50

Please sign in to comment.