diff --git a/src/api/LoginHandeler.jsx b/src/api/LoginHandeler.jsx deleted file mode 100644 index c83c09d..0000000 --- a/src/api/LoginHandeler.jsx +++ /dev/null @@ -1,69 +0,0 @@ -//LoginHandeler.jsx - -import { useNavigate } from "react-router-dom"; -import { useEffect, useState } from "react"; -import axios from "axios"; - -const LoginHandler = () => { - const navigate = useNavigate(); - const [isLoading, setIsLoading] = useState(true); - const [error, setError] = useState(null); - const code = new URL(window.location.href).searchParams.get("code"); - - useEffect(() => { - const kakaoLogin = async () => { - try { - const response = await axios({ - method: "GET", - url: `${import.meta.env.VITE_REACT_APP_REDIRECT_URL}/?code=${code}`, - headers: { - "Content-Type": "application/json;charset=utf-8", - "Access-Control-Allow-Origin": "*", - }, - }); - - console.log(response); - localStorage.setItem("name", response.data.account.kakaoName); - navigate("/mainpage"); - } catch (err) { - console.error("Login error:", err); - setError("로그인 중 오류가 발생했습니다."); - } finally { - setIsLoading(false); - } - }; - - if (code) { - kakaoLogin(); - } else { - setError("인가 코드를 찾을 수 없습니다."); - setIsLoading(false); - } - }, [code, navigate]); - - if (isLoading) { - return ( -
-
-

로그인 중입니다.

-

잠시만 기다려주세요.

-
-
-
- ); - } - - if (error) { - return ( -
-
-

{error}

-
-
- ); - } - - return null; -}; - -export default LoginHandler; diff --git a/src/api/auth.jsx b/src/api/auth.jsx index e532585..d12fc5c 100644 --- a/src/api/auth.jsx +++ b/src/api/auth.jsx @@ -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; } }; diff --git a/src/api/interceptors.jsx b/src/api/interceptors.jsx index 8e84433..6fc6720 100644 --- a/src/api/interceptors.jsx +++ b/src/api/interceptors.jsx @@ -1,6 +1,7 @@ +// interceptors.jsx import api from "./config"; -// Request Interceptor +// Request Interceptor - 모든 요청에 토큰 추가 api.interceptors.request.use( (config) => { const token = localStorage.getItem("accessToken"); @@ -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) => { @@ -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);