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 06445d4 commit ebb196e
Showing 1 changed file with 26 additions and 31 deletions.
57 changes: 26 additions & 31 deletions frontend/src/apis/getApi.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const abortController = new AbortController();

const decodeToken = (token: string) => {
const tokenParts = token.split('.');
if (tokenParts.length !== 3) {
throw new Error('ํ† ํฐ์ด ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค.');
}

const decodedPayloadString = atob(tokenParts[1]);
console.log('decodedPayloadString', decodedPayloadString);

return JSON.parse(decodedPayloadString);
};

async function refreshToken(headers: Headers): Promise<string> {
console.log('L12 refreshTOken์ด ํ˜ธ์ถœ์€ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ', headers);
const accessToken = localStorage.getItem('userToken');
console.log('getAPI Line 14', accessToken);
try {
// ์„œ๋ฒ„์— ์ƒˆ๋กœ์šด ์—‘์„ธ์Šค ํ† ํฐ์„ ์š”์ฒญํ•˜๊ธฐ ์œ„ํ•œ ๋„คํŠธ์›Œํฌ ์š”์ฒญ์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค.
const refreshResponse = await fetch(`${DEFAULT_PROD_URL}/refresh-token`, {
Expand All @@ -21,6 +21,7 @@ async function refreshToken(headers: Headers): Promise<string> {
body: JSON.stringify({
accessToken: accessToken,
}),
signal: abortController.signal,
});

// ์„œ๋ฒ„ ์‘๋‹ต์ด ์„ฑ๊ณต์ ์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
Expand All @@ -39,42 +40,36 @@ async function refreshToken(headers: Headers): Promise<string> {
}
}

const isTokenExpired = (token: string) => {
const decodedPayloadObject = decodeToken(token);
return decodedPayloadObject.exp * 1000 < Date.now();
};

async function updateToken(headers: Headers) {
const newToken = await refreshToken(headers);
localStorage.setItem('userToken', newToken);
abortController.abort();
}

async function withTokenRefresh<T>(callback: () => Promise<T>): Promise<T> {
let userToken = localStorage.getItem('userToken');
console.log('userToken', userToken);
if (userToken) {
const decodedPayloadObject = decodeToken(userToken);
console.log('decodedPayloadObject', decodedPayloadObject);
// decodeToken์˜ ๊ฒฐ๊ณผ๋กœ ๋งŒ๋ฃŒ ์—ฌ๋ถ€ ํŒ๋‹จ
if (decodedPayloadObject.exp * 1000 < Date.now()) {
console.log('AccessToken ๋งŒ๋ฃŒ๋˜์–ด ์žฌ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค');

const headers: any = {
'content-type': 'application/json',
Authorization: `Bearer ${userToken}`,
};
console.log(`Authorization : Bearer ${userToken}`);
//์ƒˆ๋กœ์šด ํ† ํฐ ์žฌ๋ฐœ๊ธ‰
userToken = await refreshToken(headers);

console.log('L59 userToken', userToken);
localStorage.setItem('userToken', userToken);
console.log('localStorage์— ์ƒˆ๋กœ์šด ํ† ํฐ ์ ์šฉ ์„ฑ๊ณต!');
}
const userToken = localStorage.getItem('userToken');

if (userToken && isTokenExpired(userToken)) {
console.log('AccessToken ๋งŒ๋ฃŒ๋˜์–ด ์žฌ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค');

const headers: any = {
'content-type': 'application/json',
Authorization: `Bearer ${userToken}`,
};

await updateToken(headers);
}

//์ธ์ž๋กœ ๋„˜๊ฒจ์ค€ ์‹ค์ œ fetchํ•จ์ˆ˜ ์‹คํ–‰
console.log('callback ์‹คํ–‰');
return callback();
}

import { DEFAULT_PROD_URL } from '../constants';

const API_URL =
process.env.NODE_ENV === 'development'
? 'http://localhost:3000'
: DEFAULT_PROD_URL;

interface Headers {
'content-type': string;
[key: string]: string;
Expand Down

0 comments on commit ebb196e

Please sign in to comment.