Skip to content

Commit

Permalink
Merge pull request #51 from Kakaotech-10/feature/Login
Browse files Browse the repository at this point in the history
⚡️ 요구사항 정리 및 개선
  • Loading branch information
hardlife0 authored Oct 29, 2024
2 parents fc239d6 + 4544906 commit 704873f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 78 deletions.
69 changes: 0 additions & 69 deletions src/api/LoginHandeler.jsx

This file was deleted.

1 change: 1 addition & 0 deletions src/api/auth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const refreshToken = async () => {
return response.data;
} catch (error) {
console.error("Token refresh error:", error);
window.location.href = "/login";
throw error;
}
};
Expand Down
17 changes: 8 additions & 9 deletions src/api/interceptors.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// interceptors.jsx
import api from "./config";

// Request Interceptor
// Request Interceptor - 모든 요청에 토큰 추가
api.interceptors.request.use(
(config) => {
const token = localStorage.getItem("accessToken");
Expand All @@ -12,7 +13,7 @@ api.interceptors.request.use(
(error) => Promise.reject(error)
);

// Response Interceptor
// Response Interceptor - 401 에러 처리
api.interceptors.response.use(
(response) => response,
async (error) => {
Expand All @@ -23,25 +24,23 @@ api.interceptors.response.use(
originalRequest._retry = true;

try {
// refreshToken은 쿠키로 자동 전송되므로 body 없이 요청
const { data } = await api.post("/api/v1/auth/refresh");
// 토큰 재발급 요청
const { data } = await api.post("/v1/auth/refresh");
const newAccessToken = data.accessToken;

// 새 액세스 토큰 저장
localStorage.setItem("accessToken", newAccessToken);

// 새 토큰으로 헤더 업데이트
api.defaults.headers.common["Authorization"] =
`Bearer ${newAccessToken}`;
originalRequest.headers.Authorization = `Bearer ${newAccessToken}`;

// 원래 요청 재시도
return api(originalRequest);
} catch (error) {
} catch (refreshError) {
// 리프레시 토큰도 만료된 경우
localStorage.clear();
localStorage.removeItem("accessToken");
window.location.href = "/login";
return Promise.reject(error);
return Promise.reject(refreshError);
}
}
return Promise.reject(error);
Expand Down

0 comments on commit 704873f

Please sign in to comment.