From f21a8bf78498398a8e5be36b24cd8cb53cae73b9 Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Thu, 13 Jun 2024 20:12:13 -0300 Subject: [PATCH] feat: create HydraApi --- src/main/services/hydra-api.ts | 47 +++++++++++++++++----------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/main/services/hydra-api.ts b/src/main/services/hydra-api.ts index e65d70d2..824f41e3 100644 --- a/src/main/services/hydra-api.ts +++ b/src/main/services/hydra-api.ts @@ -36,33 +36,32 @@ export class HydraApi { if (error.response.status === 401 && !originalRequest._retry) { originalRequest._retry = true; const refreshToken = this.refreshToken; - if (refreshToken) { - try { - const response = await axios.post( - `${import.meta.env.MAIN_VITE_API_URL}/auth/refresh`, - { refreshToken } - ); - const newAccessToken = response.data.accessToken; - this.authToken = newAccessToken; - userPreferencesRepository.upsert( - { - id: 1, - accessToken: newAccessToken, - }, - ["id"] - ); + if (!refreshToken) return error; - originalRequest.headers.Authorization = `Bearer ${newAccessToken}`; - return axios(originalRequest); //recall Api with new token - } catch (error) { - this.authToken = ""; - this.refreshToken = ""; - return error; - } + try { + const response = await axios.post( + `${import.meta.env.MAIN_VITE_API_URL}/auth/refresh`, + { refreshToken } + ); + const newAccessToken = response.data.accessToken; + this.authToken = newAccessToken; + + userPreferencesRepository.upsert( + { + id: 1, + accessToken: newAccessToken, + }, + ["id"] + ); + + originalRequest.headers.Authorization = `Bearer ${newAccessToken}`; + return axios(originalRequest); //recall Api with new token + } catch (err) { + this.authToken = ""; + this.refreshToken = ""; + return error; } - - return error; } return error;