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",