Skip to content

Commit

Permalink
hotfix: abortController 여러개 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
jiwonh423 committed Sep 20, 2023
1 parent 97c724e commit 5dddf01
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions frontend/src/apis/getApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const abortController = new AbortController();

const decodeToken = (token: string) => {
const tokenParts = token.split('.');
if (tokenParts.length !== 3) {
Expand All @@ -11,7 +9,10 @@ const decodeToken = (token: string) => {
return JSON.parse(decodedPayloadString);
};

async function refreshToken(headers: Headers): Promise<string> {
async function refreshToken(
headers: Headers,
abortController: AbortController,
): Promise<string> {
const accessToken = localStorage.getItem('userToken');
try {
// 서버에 새로운 엑세스 토큰을 요청하기 위한 네트워크 요청을 보냅니다.
Expand Down Expand Up @@ -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<T>(callback: () => Promise<T>): Promise<T> {
const userToken = localStorage.getItem('userToken');
const abortController = new AbortController();

if (userToken && isTokenExpired(userToken)) {
console.log('AccessToken 만료되어 재요청합니다');
Expand All @@ -62,7 +64,7 @@ async function withTokenRefresh<T>(callback: () => Promise<T>): Promise<T> {
Authorization: `Bearer ${userToken}`,
};

await updateToken(headers);
await updateToken(headers, abortController);
}

return callback();
Expand Down

0 comments on commit 5dddf01

Please sign in to comment.