Skip to content

Commit

Permalink
Merge pull request #199 from Nexters/fix/refresh-token
Browse files Browse the repository at this point in the history
fix: 인증 만료로 리프래시 무한 호출 로직 수정
  • Loading branch information
alstn2468 authored Sep 27, 2024
2 parents 9ee445d + 541b0ac commit d50dcfd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
21 changes: 21 additions & 0 deletions apps/admin/src/atoms/useAuthAtom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LOCAL_STORAGE } from '@boolti/api';
import { atom, useAtom } from 'jotai';
import { useEffect } from 'react';

const storageMethod = {
getItem: (key: string, initialValue: string | null) => {
Expand Down Expand Up @@ -39,6 +40,26 @@ export const useAuthAtom = () => {

const isLogin = () => !!accessToken && !!refreshToken;

useEffect(() => {
const handler = (e: StorageEvent) => {
switch (e.key) {
case LOCAL_STORAGE.ACCESS_TOKEN: {
setAccessToken(e.newValue);
return;
}
case LOCAL_STORAGE.REFRESH_TOKEN: {
setRefreshToken(e.newValue);
return;
}
}
};
window.addEventListener('storage', handler);

return () => {
window.removeEventListener('storage', handler);
};
}, [setAccessToken, setRefreshToken]);

return {
setToken,
removeToken,
Expand Down
40 changes: 20 additions & 20 deletions packages/api/src/fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Options, ResponsePromise } from 'ky';
import ky from 'ky';
import ky, { HTTPError } from 'ky';

import BooltiHTTPError, { isBooltiHTTPError } from './BooltiHTTPError';
import { isBooltiHTTPError } from './BooltiHTTPError';
import { LOCAL_STORAGE } from './constants';

const API_URL = import.meta.env.VITE_BASE_API_URL;
Expand All @@ -24,7 +24,6 @@ const postRefreshToken = async () => {
},
},
);

return await response.json<PostRefreshTokenResponse>();
}
};
Expand Down Expand Up @@ -55,7 +54,6 @@ export const instance = ky.create({
if (!response.ok && response.status === 401 && !request.url.includes('logout')) {
try {
const { accessToken, refreshToken } = (await postRefreshToken()) ?? {};

if (accessToken && refreshToken) {
window.localStorage.setItem(LOCAL_STORAGE.ACCESS_TOKEN, accessToken);
window.localStorage.setItem(LOCAL_STORAGE.REFRESH_TOKEN, refreshToken);
Expand All @@ -64,24 +62,26 @@ export const instance = ky.create({

return ky(request, options);
}
} catch (error) {
throw new BooltiHTTPError(response, request, options);
}
}

if (!response.ok) {
const body = await response.json();

if (body) {
throw new BooltiHTTPError(response, request, options, {
errorTraceId: body.errorTraceId,
type: body.type,
detail: body.detail,
});
} catch (e) {
if (e instanceof HTTPError && e.response.url.includes('/login/refresh')) {
window.localStorage.removeItem(LOCAL_STORAGE.ACCESS_TOKEN);
window.localStorage.removeItem(LOCAL_STORAGE.REFRESH_TOKEN);
window.dispatchEvent(
new StorageEvent('storage', {
key: LOCAL_STORAGE.REFRESH_TOKEN,
newValue: undefined,
}),
);
window.dispatchEvent(
new StorageEvent('storage', {
key: LOCAL_STORAGE.ACCESS_TOKEN,
newValue: undefined,
}),
);
}
}

throw new BooltiHTTPError(response, request, options);
}
return response;
},
],
},
Expand Down

0 comments on commit d50dcfd

Please sign in to comment.