|
1 | 1 | import React from 'react'; |
2 | | -import { BrowserRouter, Route, Routes } from 'react-router-dom'; |
| 2 | +import { BrowserRouter, Route, Routes, Navigate } from 'react-router-dom'; |
3 | 3 | import Home from './pages/Home'; |
4 | | -import MyPage from './pages/Mypage'; |
5 | | -import MyPost from './pages/MyPost'; |
| 4 | +import Login from './pages/Login'; |
| 5 | +import SignUp from './pages/SignUp'; |
| 6 | +import LoginComplete from './pages/Login/components/LoginComplete'; |
| 7 | +import TermsAgreement from './pages/TermsAgreement'; |
| 8 | + |
| 9 | +import MyPage from './pages/MyPage'; |
6 | 10 | import ProfileEdit from './pages/ProfileEdit'; |
7 | 11 | import AccountSetting from './pages/AccountSetting'; |
8 | 12 | import AccountEdit from './pages/AccountEdit'; |
9 | 13 | import AccountCancel from './pages/AccountCancel'; |
10 | 14 | import Verification from './pages/verification'; |
11 | | -import Login from './pages/Login'; |
12 | | -import SignUp from './pages/SignUp'; |
| 15 | + |
13 | 16 | import ProfileViewer from './pages/ProfileViewer'; |
| 17 | + |
| 18 | +import Post from './pages/Post'; |
| 19 | +import MyPost from './pages/MyPost'; |
| 20 | +import PostUpload from './pages/PostUpload'; |
| 21 | +import PostImageSelect from './pages/PostImageSelect'; |
| 22 | +import PostInstaConnect from './pages/PostInstaConnect'; |
| 23 | +import PostInstaFeedSelect from './pages/PostInstaFeedSelect'; |
| 24 | + |
14 | 25 | import Chats from './pages/Chats'; |
15 | 26 | import ChatRoom from './pages/Chats/ChatRoom'; |
16 | | -import Upload from './pages/Upload'; |
17 | | -import Post from './pages/Post'; |
18 | | -import KakaoCallback from './pages/Login/components/Kakao/KakaoCallback'; |
19 | | -import NaverCallback from './pages/Login/components/Naver/NaverCallback'; |
20 | | -import GoogleCallback from './pages/Login/components/Google/GoogleCallback'; |
| 27 | + |
| 28 | +import NotFound from './pages/NotFound'; |
| 29 | + |
| 30 | +const ProtectedRoute = ({ children }: { children: JSX.Element }) => { |
| 31 | + const isAuthenticated = Boolean(localStorage.getItem('new_jwt_token')); |
| 32 | + return isAuthenticated ? children : <Navigate to="/login" />; |
| 33 | +}; |
| 34 | + |
| 35 | +// 인증이 필요한 페이지 배열 |
| 36 | +const protectedRoutes = [ |
| 37 | + { path: '/', element: <Home /> }, |
| 38 | + |
| 39 | + // 사용자 프로필 및 계정 관리 |
| 40 | + { path: '/mypage', element: <MyPage /> }, |
| 41 | + { path: '/profile/edit', element: <ProfileEdit /> }, |
| 42 | + { path: '/account-setting', element: <AccountSetting /> }, |
| 43 | + { path: '/account-edit', element: <AccountEdit /> }, |
| 44 | + { path: '/account-cancel', element: <AccountCancel /> }, |
| 45 | + { path: '/verification', element: <Verification /> }, |
| 46 | + { path: '/users/:userId', element: <ProfileViewer /> }, |
| 47 | + |
| 48 | + { path: '/post/:postId', element: <Post /> }, |
| 49 | + { path: '/my-post/:postId', element: <MyPost /> }, |
| 50 | + { path: '/upload', element: <PostUpload /> }, |
| 51 | + { path: '/image-select', element: <PostImageSelect /> }, |
| 52 | + { path: '/insta-connect', element: <PostInstaConnect /> }, |
| 53 | + { path: '/insta-feed-select', element: <PostInstaFeedSelect /> }, |
| 54 | + |
| 55 | + // 메시지/채팅 |
| 56 | + { path: '/chats', element: <Chats /> }, |
| 57 | + { path: '/chats/:chatRoomId', element: <ChatRoom /> }, |
| 58 | +]; |
| 59 | + |
| 60 | +// 인증이 필요 없는 페이지 배열 |
| 61 | +const publicRoutes = [ |
| 62 | + { path: '/login', element: <Login /> }, |
| 63 | + { path: '/signup', element: <SignUp /> }, |
| 64 | + { path: '/login/complete', element: <LoginComplete /> }, |
| 65 | + { path: '/terms-agreement', element: <TermsAgreement /> }, |
| 66 | +]; |
| 67 | + |
21 | 68 | const App: React.FC = () => { |
22 | 69 | return ( |
23 | 70 | <BrowserRouter> |
24 | 71 | <Routes> |
25 | | - <Route path="/" element={<Home />} /> |
26 | | - <Route path="/chats/:roomId" element={<ChatRoom />} /> |
27 | | - <Route path="/mypage" element={<MyPage />} /> |
28 | | - <Route path="/my-post/:postId" element={<MyPost />} /> |
29 | | - <Route path="/profile/edit" element={<ProfileEdit />} /> |
30 | | - <Route path="/account-setting" element={<AccountSetting />} /> |
31 | | - |
32 | | - <Route path="/account-edit" element={<AccountEdit />} /> |
33 | | - <Route path="/account-cancel" element={<AccountCancel />} /> |
34 | | - <Route path="/Verification" element={<Verification />} /> |
35 | | - <Route path="/login" element={<Login />} /> |
36 | | - <Route path="/chats" element={<Chats />} /> |
37 | | - <Route path="/upload" element={<Upload />} /> |
38 | | - <Route path="/post/:postId" element={<Post />} /> |
39 | | - <Route path="/users/:userId" element={<ProfileViewer />} /> |
40 | | - <Route path="signup" element={<SignUp />} /> |
41 | | - |
42 | | - <Route path="/auth/kakao/callback" element={<KakaoCallback />}></Route> |
43 | | - <Route path="/auth/naver/callback" element={<NaverCallback />}></Route> |
44 | | - <Route path="/auth/google/callback" element={<GoogleCallback />}></Route> |
| 72 | + {/* 인증이 필요한 페이지 */} |
| 73 | + {protectedRoutes.map(({ path, element }) => ( |
| 74 | + <Route key={path} path={path} element={<ProtectedRoute>{element}</ProtectedRoute>} /> |
| 75 | + ))} |
| 76 | + |
| 77 | + {/* 인증이 필요 없는 페이지 */} |
| 78 | + {publicRoutes.map(({ path, element }) => ( |
| 79 | + <Route key={path} path={path} element={element} /> |
| 80 | + ))} |
| 81 | + |
| 82 | + {/* 없는 페이지에 대한 처리 */} |
| 83 | + <Route path="*" element={<NotFound />} /> |
45 | 84 | </Routes> |
46 | 85 | </BrowserRouter> |
47 | 86 | ); |
|
0 commit comments