From 702e26a9a4b4c963eb463eb0352d122de70aeb7e Mon Sep 17 00:00:00 2001 From: Jaehyuk Yang <37584686+yangjaehyuk@users.noreply.github.com> Date: Thu, 25 Jan 2024 01:36:57 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=EB=A1=9C=EA=B7=B8=EC=95=84=EC=9B=83=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80=20(#57)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/instanceApi.ts | 7 ++++--- .../header/logoutButton/LogoutButton.tsx | 18 ++++++++++++++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/api/instanceApi.ts b/src/api/instanceApi.ts index 3b3ef500..d6c74919 100644 --- a/src/api/instanceApi.ts +++ b/src/api/instanceApi.ts @@ -2,7 +2,6 @@ import axios, { AxiosInstance } from "axios"; import { isAccessTokenExpired } from "@/utils/checkToken"; import { getCookie, removeCookie, setCookie } from "@/utils/cookies"; - const instance: AxiosInstance = axios.create({ baseURL: import.meta.env.VITE_API_BASE_URL, timeout: 5000, @@ -48,9 +47,11 @@ instance.interceptors.response.use( window.location.pathname !== "login" && window.location.pathname !== "signup" ) { - localStorage.removeItem("accessToken"); + localStorage.clear(); removeCookie(); - window.location.replace("/login"); + setTimeout(() => { + window.location.replace("/login"); + }, 1000); } return Promise.reject(error); } diff --git a/src/components/common/header/logoutButton/LogoutButton.tsx b/src/components/common/header/logoutButton/LogoutButton.tsx index 27ffa2a2..bd057f47 100644 --- a/src/components/common/header/logoutButton/LogoutButton.tsx +++ b/src/components/common/header/logoutButton/LogoutButton.tsx @@ -7,6 +7,7 @@ import { MdLogout } from "react-icons/md"; import { userState } from "@/states/userState"; import instance from "@/api/instanceApi"; import { removeCookie } from "@/utils/cookies"; +import { AxiosError } from "axios"; const LogoutButton = () => { const [modalVisible, setModalVisible] = useState(false); @@ -24,15 +25,24 @@ const LogoutButton = () => { onClick: () => { const logOut = async () => { try { - const res = await instance.post(`/api/auth/signout`); + await instance.delete(`/api/auth/logout`); localStorage.removeItem("accessToken"); removeCookie(); setUserInfo(null); navigate("/"); - - return res; } catch (error) { - console.log(error); + if (error instanceof AxiosError) { + const res = error.response?.data.code; + if (res === 1005) { + alert("회원 정보를 찾을 수 없습니다."); + removeCookie(); + setUserInfo(null); + } else { + alert("이미 로그아웃한 회원입니다."); + removeCookie(); + setUserInfo(null); + } + } } };