Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 인증 만료로 리프래시 무한 호출 로직 수정 #199

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading