From 6c8494defccdaa5decf794345a19e28ccaa1e05e Mon Sep 17 00:00:00 2001 From: 42inshin Date: Mon, 8 Jan 2024 16:52:18 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[FE]=20FIX:=20Cookie=20get,=20remove=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95=20#90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/cookie/cookies.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/api/cookie/cookies.ts b/src/api/cookie/cookies.ts index 90bff23..5b482a1 100644 --- a/src/api/cookie/cookies.ts +++ b/src/api/cookie/cookies.ts @@ -4,12 +4,14 @@ export const getCookie = () => { return document.cookie .split(";") .map((cookie) => cookie.trim()) - .filter((cookie) => tokenName === cookie.split("=")[0]) - .join("") - .split("=")[1]; + .find((cookie) => cookie.startsWith(`${tokenName}=`)) + ?.split("=")[1]; }; export const removeCookie = (): void => { - const domain = ".24hoursarenotenough.42seoul.kr"; + const hostname = window.location.hostname; + const domain = + hostname === "localhost" ? "" : ".24hoursarenotenough.42seoul.kr"; + document.cookie = `${tokenName}=; path=/; domain=${domain}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`; }; From bfa22279e8d04f4a625049825db21859ea5666df Mon Sep 17 00:00:00 2001 From: 42inshin Date: Mon, 8 Jan 2024 16:53:08 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[FE]=20FIX:=20isLogin=20=EC=82=AD=EC=A0=9C?= =?UTF-8?q?=20#90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/baseAPI.ts | 1 - src/router/index.ts | 27 ++++++++++++++++----------- src/views/AuthView.vue | 1 - 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/api/baseAPI.ts b/src/api/baseAPI.ts index dc66170..818a58a 100644 --- a/src/api/baseAPI.ts +++ b/src/api/baseAPI.ts @@ -30,7 +30,6 @@ instance.interceptors.response.use( error.response?.status === STATUS_401_UNAUTHORIZED || error.response?.status === undefined ) { - localStorage.removeItem("isLogin"); removeCookie(); clearStorage(); window.location.href = "/"; diff --git a/src/router/index.ts b/src/router/index.ts index a4f6e88..6742da3 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -7,7 +7,8 @@ import MoreView from "@/views/MoreView.vue"; import NotificationView from "@/views/NotificationView.vue"; import NotFoundViewVue from "@/views/NotFoundView.vue"; import ApplyCardViewVue from "@/views/ApplyCardView.vue"; -import { getCookie } from "@/api/cookie/cookies"; +import { getCookie, removeCookie } from "@/api/cookie/cookies"; +import { clearStorage } from "@/utils/localStorage"; const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), @@ -61,21 +62,25 @@ const router = createRouter({ router.beforeEach((to, from, next) => { const token = getCookie(); - const isLogin = localStorage.getItem("isLogin"); - if (to.name === "login" && isLogin === "true" && !!token) { - next({ name: "home" }); + + // 이미 로그인된 상태에서 로그인 페이지로 이동 시 홈으로 리다이렉트 + const isNavigatingToLogin = to.name === "login"; + if (isNavigatingToLogin && token) { + return next({ name: "home" }); } - if ( - to.name !== "login" && - to.name !== "auth" && - (isLogin !== "true" || !token) - ) { + // 로그인 및 인증 페이지가 아니고, 토큰이 없는 경우 로그인 페이지로 이동 + const isProtectedRoute = to.name !== "login" && to.name !== "auth"; + if (isProtectedRoute && !token) { + removeCookie(); + clearStorage(); next({ name: "login" }); alert("로그인 정보가 유효하지 않습니다.\n다시 로그인해주세요."); - } else { - next(); + return; } + + // 다른 경우는 정상적으로 라우트 진행 + next(); }); export default router; diff --git a/src/views/AuthView.vue b/src/views/AuthView.vue index aaeb23e..bbc216a 100644 --- a/src/views/AuthView.vue +++ b/src/views/AuthView.vue @@ -11,7 +11,6 @@ onMounted(() => { setTimeout(async () => { const isLogin = await getIsLogin(); if (isLogin?.status === STATUS_204_NO_CONTENT) { - localStorage.setItem("isLogin", "true"); router.push("/home"); } else { router.push("/");