Skip to content

Commit

Permalink
fix: 토큰 무한 리프레쉬 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
PMtHk committed Nov 25, 2024
1 parent 1339e50 commit 2edcf5d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
15 changes: 2 additions & 13 deletions apps/client/src/lib/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,14 @@ axiosInstance.interceptors.request.use(
}
);

const REFRESH_MAX_RETRY_COUNT = 3;

/* eslint no-underscore-dangle: 0 */
axiosInstance.interceptors.response.use(
(response) => Promise.resolve(response),
async (error) => {
if (error.response?.status !== 401) {
if (error.response?.status !== 401 || error.config.url === '/auth/refresh') {
return Promise.reject(error);
}

const originalReq = error.config;

if (originalReq._retry >= REFRESH_MAX_RETRY_COUNT) {
localStorage.removeItem(AUTH_STORAGE_KEY);
return Promise.reject(error);
}

originalReq._retry = (originalReq._retry || 0) + 1;

try {
const response = await axiosInstance.post('/auth/refresh');
if (response.status !== 200) {
Expand All @@ -63,7 +52,7 @@ axiosInstance.interceptors.response.use(

localStorage.setItem(AUTH_STORAGE_KEY, JSON.stringify(authStorage));

const newConfig = { ...originalReq };
const newConfig = { ...error.config };
newConfig.headers.Authorization = `Bearer ${accessToken}`;

return axiosInstance(newConfig);
Expand Down
4 changes: 2 additions & 2 deletions apps/client/src/routes/__root.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createRootRouteWithContext, Outlet } from '@tanstack/react-router';
import { QueryClient } from '@tanstack/react-query';
import { AuthContext } from '@/contexts/authContext';
import { AuthContextValue } from '@/features/auth/AuthProvider.tsx';

interface RouterContext {
auth: AuthContext;
auth: AuthContextValue;
queryClient: QueryClient;
}

Expand Down

0 comments on commit 2edcf5d

Please sign in to comment.