Skip to content

Commit

Permalink
feat: mutex 락 여부에 따라 내부 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
alstn2468 committed Jan 1, 2025
1 parent 541e766 commit 81e0949
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions packages/api/src/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,23 @@ export const instance = ky.create({
afterResponse: [
async (request, options, response) => {
if (!response.ok && response.status === 401 && !request.url.includes('logout')) {
if (tokenRefreshMutex.isLocked()) {
await tokenRefreshMutex.waitForUnlock();
}
try {
tokenRefreshMutex.acquire();
let accessToken: string | undefined;

if (tokenRefreshMutex.isLocked()) {
await tokenRefreshMutex.waitForUnlock();

const newAccessToken = window.localStorage.getItem(LOCAL_STORAGE.ACCESS_TOKEN);

if (newAccessToken) {
accessToken = newAccessToken;
}
} else {
await tokenRefreshMutex.acquire();
accessToken = await refreshAccessToken();
}

const newAccessToken = await refreshAccessToken();
request.headers.set('Authorization', `Bearer ${newAccessToken}`);
request.headers.set('Authorization', `Bearer ${accessToken}`);
return ky(request, options);
} catch (e) {
if (e instanceof HTTPError && e.response.url.includes('/login/refresh')) {
Expand Down

0 comments on commit 81e0949

Please sign in to comment.