From fc3bba488c534c5d9e04164a18a30fd8d2f72ea7 Mon Sep 17 00:00:00 2001 From: Heeyeun Ko Date: Fri, 15 Nov 2024 23:10:18 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20accessToken=20=EC=97=86=EC=9D=84=20?= =?UTF-8?q?=EC=8B=B1=EC=97=90=20loingPage=EB=A1=9C=20navigate=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/constant.ts | 3 -- src/routes/Routes.tsx | 75 +++++++++++++++++++++----------------- src/routes/TokenRouter.tsx | 24 ++++++++++++ src/routes/path.ts | 1 + 4 files changed, 66 insertions(+), 37 deletions(-) create mode 100644 src/routes/TokenRouter.tsx diff --git a/src/api/constant.ts b/src/api/constant.ts index 34b89f6..1b098b1 100644 --- a/src/api/constant.ts +++ b/src/api/constant.ts @@ -1,4 +1 @@ export const API_BASE_URL = import.meta.env.VITE_API_BASE_URL; - -export const ACCESS_TOKEN = "accessToken"; -export const REFRESH_TOKEN = "refreshToken"; \ No newline at end of file diff --git a/src/routes/Routes.tsx b/src/routes/Routes.tsx index 483f36c..b779762 100644 --- a/src/routes/Routes.tsx +++ b/src/routes/Routes.tsx @@ -8,46 +8,53 @@ import LoadingPage from "../pages/LoadingPage/LoadingPage"; // 로딩 페이지 import AnalysisPage from "../pages/Analysis/AnalysisPage"; import NotFoundPage from "../pages/NotFound/NotFoundPage"; import CameraPage from "../pages/Camera/CameraPage"; +import TokenRouter from "./TokenRouter"; import { RouterPath } from "./path"; // 경로 상수 가져오기 // 라우터 정의 const router = createBrowserRouter( [ { - path: RouterPath.root, - element: , - }, - { - path: RouterPath.login, - element: , - }, - { - path: RouterPath.signup, - element: , - }, - { - path: RouterPath.main, - element: , - }, - { - path: RouterPath.book, - element: , - }, - { - path: RouterPath.loading, - element: , - }, - { - path: RouterPath.analysis, - element: , - }, - { - path: RouterPath.notFound, - element: , - }, - { - path: RouterPath.camera, - element: , + path: RouterPath.token, + element: , + children: [ + { + path: RouterPath.root, + element: , + }, + { + path: RouterPath.login, + element: , + }, + { + path: RouterPath.signup, + element: , + }, + { + path: RouterPath.main, + element: , + }, + { + path: RouterPath.book, + element: , + }, + { + path: RouterPath.loading, + element: , + }, + { + path: RouterPath.analysis, + element: , + }, + { + path: RouterPath.notFound, + element: , + }, + { + path: RouterPath.camera, + element: , + }, + ], }, ], { diff --git a/src/routes/TokenRouter.tsx b/src/routes/TokenRouter.tsx new file mode 100644 index 0000000..b86a264 --- /dev/null +++ b/src/routes/TokenRouter.tsx @@ -0,0 +1,24 @@ +import {Outlet, useLocation, useNavigate} from "react-router-dom"; + +import { useEffect } from "react"; +const TokenRouter = () => { + +const location = useLocation(); + const navigate = useNavigate(); + const isAuthPath = location.pathname === "/login" || location.pathname === "/signup"; + + + useEffect(() => { + if(!localStorage.getItem("accessToken") && !isAuthPath){ + navigate("/login"); + } + + }, [location.pathname, isAuthPath, navigate]); + return ( +
+ +
+ ); +}; + +export default TokenRouter; diff --git a/src/routes/path.ts b/src/routes/path.ts index c45c3fa..5454b2e 100644 --- a/src/routes/path.ts +++ b/src/routes/path.ts @@ -1,4 +1,5 @@ export const RouterPath = { + token: "", root: "/", start: "/", login: "/login",