@@ -2,16 +2,24 @@ import { useCallback, useEffect, useState } from "react";
22import { AuthContext } from "./auth/AuthContext.jsx" ;
33import { FaqPage } from "./components/FaqPage.jsx" ;
44import { Hero } from "./components/Hero.jsx" ;
5+ import { LoginPage } from "./components/LoginPage.jsx" ;
56import { MainPage } from "./components/MainPage.jsx" ;
67import { ProjectsPage } from "./components/ProjectsPage.jsx" ;
78import { ShopPage } from "./components/ShopPage.jsx" ;
89import { TestPage } from "./components/TestPage.jsx" ;
10+ import { AdminPage } from "./components/AdminPage.jsx" ;
11+ import { AdminShopPage } from "./components/AdminShopPage.jsx" ;
12+ import { AdminShopOrdersPage } from "./components/AdminShopOrdersPage.jsx" ;
913import { UserAreaPage } from "./components/UserAreaPage.jsx" ;
1014
11- const PROTECTED = new Set ( [ "/main" , "/shop" , "/projects" , "/faq" , "/user" , "/test" ] ) ;
15+ const PROTECTED = new Set ( [ "/main" , "/shop" , "/projects" , "/faq" , "/user" , "/test" , "/admin" ] ) ;
16+ const ADMIN_ONLY = new Set ( [ "/admin" ] ) ;
1217
1318export default function App ( ) {
1419 const [ auth , setAuth ] = useState ( { status : "loading" , user : null } ) ;
20+ const pathname = window . location . pathname . replace ( / \/ + $ / , "" ) || "/" ;
21+ const isLocalhost = [ "localhost" , "127.0.0.1" ] . includes ( window . location . hostname ) ;
22+ const isAdminPath = pathname === "/admin" || pathname . startsWith ( "/admin/" ) ;
1523
1624 const loadMe = useCallback ( async ( ) => {
1725 try {
@@ -27,8 +35,6 @@ export default function App() {
2735 loadMe ( ) ;
2836 } , [ loadMe ] ) ;
2937
30- const pathname = window . location . pathname . replace ( / \/ + $ / , "" ) || "/" ;
31-
3238 if ( auth . status === "loading" ) {
3339 return (
3440 < div className = "app-auth-loading" aria-busy = "true" >
@@ -37,9 +43,14 @@ export default function App() {
3743 ) ;
3844 }
3945
40- if ( PROTECTED . has ( pathname ) && ! auth . user ) {
46+ if ( ( PROTECTED . has ( pathname ) || isAdminPath ) && ! auth . user ) {
4147 const returnTo = `${ pathname } ${ window . location . search } ${ window . location . hash } ` ;
42- window . location . replace ( `/api/auth/hackclub/login?returnTo=${ encodeURIComponent ( returnTo ) } ` ) ;
48+ window . location . replace ( `/login?returnTo=${ encodeURIComponent ( returnTo ) } ` ) ;
49+ return null ;
50+ }
51+
52+ if ( ( ADMIN_ONLY . has ( pathname ) || isAdminPath ) && auth . user ?. role !== "admin" ) {
53+ window . location . replace ( "/main" ) ;
4354 return null ;
4455 }
4556
@@ -48,6 +59,11 @@ export default function App() {
4859 return null ;
4960 }
5061
62+ if ( pathname === "/login" && auth . user ) {
63+ window . location . replace ( "/main" ) ;
64+ return null ;
65+ }
66+
5167 const contextValue = {
5268 user : auth . user ,
5369 status : auth . status ,
@@ -59,8 +75,11 @@ export default function App() {
5975 case "/main" :
6076 page = < MainPage /> ;
6177 break ;
78+ case "/login" :
79+ page = < LoginPage /> ;
80+ break ;
6281 case "/test" :
63- page = < TestPage /> ;
82+ page = isLocalhost ? < TestPage /> : < AdminPage /> ;
6483 break ;
6584 case "/projects" :
6685 page = < ProjectsPage /> ;
@@ -74,6 +93,15 @@ export default function App() {
7493 case "/user" :
7594 page = < UserAreaPage /> ;
7695 break ;
96+ case "/admin" :
97+ page = < AdminPage /> ;
98+ break ;
99+ case "/admin/shop" :
100+ page = < AdminShopPage /> ;
101+ break ;
102+ case "/admin/shop/orders" :
103+ page = < AdminShopOrdersPage /> ;
104+ break ;
77105 default :
78106 page = < Hero /> ;
79107 }
0 commit comments