Skip to content

Commit

Permalink
Merge pull request #91 from innovationacademy-kr/fe/dev/fix_isLogin/#90
Browse files Browse the repository at this point in the history
[FE] FIX: isLogin 기능 삭제 #90
  • Loading branch information
42inshin authored Jan 8, 2024
2 parents dd21e42 + d0d46b5 commit 9aa38bb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
1 change: 0 additions & 1 deletion src/api/baseAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "/";
Expand Down
10 changes: 6 additions & 4 deletions src/api/cookie/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;`;
};
27 changes: 16 additions & 11 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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;
1 change: 0 additions & 1 deletion src/views/AuthView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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("/");
Expand Down

0 comments on commit 9aa38bb

Please sign in to comment.