From 93bb2d6d8519abcef1050bec46040220db744926 Mon Sep 17 00:00:00 2001 From: choiseohyeon Date: Thu, 4 Jul 2024 04:26:28 +0900 Subject: [PATCH] =?UTF-8?q?[refactor]=20apis=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/axiosConfig.js | 79 ------------------------------------- src/pages/signup/signup.tsx | 2 +- 2 files changed, 1 insertion(+), 80 deletions(-) delete mode 100644 src/apis/axiosConfig.js diff --git a/src/apis/axiosConfig.js b/src/apis/axiosConfig.js deleted file mode 100644 index 0e018d1..0000000 --- a/src/apis/axiosConfig.js +++ /dev/null @@ -1,79 +0,0 @@ -import axios from "axios"; -import { useRecoilState } from "recoil"; -import { authState } from "../atoms/authState.ts"; -import { useNavigate } from "react-router-dom"; - -// Axios 인스턴스 생성 -const axiosInstance = axios.create({ - baseURL: "https://cogo.life", - withCredentials: true, - headers: { - "Content-Type": "application/json", - }, -}); - -// Axios 인터셉터 설정 -axiosInstance.interceptors.response.use( - (response) => response, - async (error) => { - const originalRequest = error.config; - const [auth, setAuth] = useRecoilState(authState); - - if (error.response.status === 401 && !originalRequest._retry) { - originalRequest._retry = true; - - try { - const res = await axiosInstance.post("/reissue"); - - if (res.status === 200) { - const accessToken = res.headers["access"]; - setAuth({ - isLoggedIn: true, - username: null, - token: accessToken, - }); - localStorage.setItem("token", accessToken); - - originalRequest.headers["Authorization"] = `Bearer ${accessToken}`; - return axiosInstance(originalRequest); - } - } catch (reissueError) { - console.error("Token reissue failed:", reissueError); - setAuth({ isLoggedIn: false, username: null, token: null }); - localStorage.removeItem("token"); - localStorage.setItem("isLoggedIn", "false"); - } - } - - if (error.response.status === 302) { - const location = error.response.headers["location"]; - if (location && location.includes("/login")) { - try { - const res = await axiosInstance.post("/reissue"); - - if (res.status === 200) { - const accessToken = res.headers["access"]; - setAuth({ - isLoggedIn: true, - username: null, - token: accessToken, - }); - localStorage.setItem("token", accessToken); - - originalRequest.headers["Authorization"] = `Bearer ${accessToken}`; - return axiosInstance(originalRequest); - } - } catch (reissueError) { - console.error("Token reissue failed:", reissueError); - setAuth({ isLoggedIn: false, username: null, token: null }); - localStorage.removeItem("token"); - localStorage.setItem("isLoggedIn", "false"); - } - } - } - - return Promise.reject(error); - } -); - -export default axiosInstance; diff --git a/src/pages/signup/signup.tsx b/src/pages/signup/signup.tsx index fb3e49c..e4a3af6 100644 --- a/src/pages/signup/signup.tsx +++ b/src/pages/signup/signup.tsx @@ -62,9 +62,9 @@ function SignUp() { }, }) .then((response) => { + alert("이메일 전송 성공! 📨"); const receivedCode = response.data; localStorage.setItem("authCode", receivedCode); - alert("이메일 전송 성공! 📨"); }) .catch((error) => { console.error("Error: ", error);