From 5dddf014ac7dcf5ebe30009537ee8e32882a59b3 Mon Sep 17 00:00:00 2001 From: afds4567 <33995840+afds4567@users.noreply.github.com> Date: Wed, 20 Sep 2023 11:32:31 +0900 Subject: [PATCH] =?UTF-8?q?hotfix:=20abortController=20=EC=97=AC=EB=9F=AC?= =?UTF-8?q?=EA=B0=9C=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/apis/getApi.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/frontend/src/apis/getApi.ts b/frontend/src/apis/getApi.ts index 45f012ef..0b3a5a8b 100644 --- a/frontend/src/apis/getApi.ts +++ b/frontend/src/apis/getApi.ts @@ -1,5 +1,3 @@ -const abortController = new AbortController(); - const decodeToken = (token: string) => { const tokenParts = token.split('.'); if (tokenParts.length !== 3) { @@ -11,7 +9,10 @@ const decodeToken = (token: string) => { return JSON.parse(decodedPayloadString); }; -async function refreshToken(headers: Headers): Promise { +async function refreshToken( + headers: Headers, + abortController: AbortController, +): Promise { const accessToken = localStorage.getItem('userToken'); try { // 서버에 새로운 엑세스 토큰을 요청하기 위한 네트워크 요청을 보냅니다. @@ -45,14 +46,15 @@ const isTokenExpired = (token: string) => { return decodedPayloadObject.exp * 1000 < Date.now(); }; -async function updateToken(headers: Headers) { - const newToken = await refreshToken(headers); +async function updateToken(headers: Headers, abortController: AbortController) { + const newToken = await refreshToken(headers, abortController); localStorage.setItem('userToken', newToken); abortController.abort(); } async function withTokenRefresh(callback: () => Promise): Promise { const userToken = localStorage.getItem('userToken'); + const abortController = new AbortController(); if (userToken && isTokenExpired(userToken)) { console.log('AccessToken 만료되어 재요청합니다'); @@ -62,7 +64,7 @@ async function withTokenRefresh(callback: () => Promise): Promise { Authorization: `Bearer ${userToken}`, }; - await updateToken(headers); + await updateToken(headers, abortController); } return callback();