Skip to content

Commit

Permalink
Merge pull request #11 from team-GDGline/feat/error
Browse files Browse the repository at this point in the history
feat: accessToken 없을 시에 loingPage로 navigate 구현
  • Loading branch information
Catleap02 authored Nov 15, 2024
2 parents b0d4ad1 + fc3bba4 commit 4828d9a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 37 deletions.
3 changes: 0 additions & 3 deletions src/api/constant.ts
Original file line number Diff line number Diff line change
@@ -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";
75 changes: 41 additions & 34 deletions src/routes/Routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: <StartPage />,
},
{
path: RouterPath.login,
element: <LoginPage />,
},
{
path: RouterPath.signup,
element: <SignupPage/>,
},
{
path: RouterPath.main,
element: <MainPage />,
},
{
path: RouterPath.book,
element: <BookPage />,
},
{
path: RouterPath.loading,
element: <LoadingPage />,
},
{
path: RouterPath.analysis,
element: <AnalysisPage />,
},
{
path: RouterPath.notFound,
element: <NotFoundPage />,
},
{
path: RouterPath.camera,
element: <CameraPage />,
path: RouterPath.token,
element: <TokenRouter />,
children: [
{
path: RouterPath.root,
element: <StartPage />,
},
{
path: RouterPath.login,
element: <LoginPage />,
},
{
path: RouterPath.signup,
element: <SignupPage />,
},
{
path: RouterPath.main,
element: <MainPage />,
},
{
path: RouterPath.book,
element: <BookPage />,
},
{
path: RouterPath.loading,
element: <LoadingPage />,
},
{
path: RouterPath.analysis,
element: <AnalysisPage />,
},
{
path: RouterPath.notFound,
element: <NotFoundPage />,
},
{
path: RouterPath.camera,
element: <CameraPage />,
},
],
},
],
{
Expand Down
24 changes: 24 additions & 0 deletions src/routes/TokenRouter.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div>
<Outlet />
</div>
);
};

export default TokenRouter;
1 change: 1 addition & 0 deletions src/routes/path.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const RouterPath = {
token: "",
root: "/",
start: "/",
login: "/login",
Expand Down

0 comments on commit 4828d9a

Please sign in to comment.