Skip to content

Commit

Permalink
Feat: 로그아웃 기능 추가 (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangjaehyuk authored Jan 24, 2024
1 parent 088ade9 commit 702e26a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/api/instanceApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down
18 changes: 14 additions & 4 deletions src/components/common/header/logoutButton/LogoutButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
}
};

Expand Down

0 comments on commit 702e26a

Please sign in to comment.