File tree Expand file tree Collapse file tree
frontend_tailwind/src/app Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import React , { useEffect } from "react" ;
2+ import { Navigate , useLocation } from "react-router-dom" ;
3+ import { getCookie } from "../utils/CookieService" ;
4+ import { toast } from "sonner" ;
5+
6+ export default function ProtectedRoute ( { children, routeType } ) {
7+ const location = useLocation ( ) ;
8+ const admin_uid = getCookie ( "admin_uid" ) ;
9+
10+ const isAdmin = ! ! admin_uid ;
11+
12+ useEffect ( ( ) => {
13+ if ( ! isAdmin && ( routeType === "admin" ) ) {
14+ toast ( "This page is restricted to admins only" ) ;
15+ }
16+ } , [ routeType , isAdmin ] ) ;
17+
18+ if ( routeType === "login" && isAdmin ) {
19+ return < Navigate to = "/admin" replace /> ;
20+ }
21+
22+ if ( routeType === "admin" && ! isAdmin ) {
23+ return < Navigate to = "/" state = { { from : location } } replace /> ;
24+ }
25+
26+ return children ;
27+ }
Original file line number Diff line number Diff line change @@ -11,14 +11,32 @@ import AdminSheets from "@/pages/Admin/Sheets/Sheets";
1111import AddBranch from "@/pages/Admin/AddBranch/AddBranch" ;
1212import { AuthProvider } from "@/contexts/AuthContext" ;
1313import { ApiProvider } from "@/contexts/ApiContext" ;
14+ import ProtectedRoute from "./ProtectedRoute" ; // 👈 import it
1415
1516export default function AppRoutes ( ) {
1617 return (
1718 < AuthProvider >
1819 < ApiProvider >
1920 < Routes >
20- < Route path = "*" element = { < Login /> } />
21- < Route path = "/admin" element = { < SidebarDemo /> } >
21+ { /* Login route */ }
22+ < Route
23+ path = "/"
24+ element = {
25+ < ProtectedRoute routeType = "login" >
26+ < Login />
27+ </ ProtectedRoute >
28+ }
29+ />
30+
31+ { /* All /admin routes protected */ }
32+ < Route
33+ path = "/admin"
34+ element = {
35+ < ProtectedRoute routeType = "admin" >
36+ < SidebarDemo />
37+ </ ProtectedRoute >
38+ }
39+ >
2240 < Route index element = { < Dashboard /> } />
2341 < Route path = "add-user" element = { < AddUser /> } />
2442 < Route path = "add-task" element = { < AddTask /> } />
@@ -28,6 +46,9 @@ export default function AppRoutes() {
2846 < Route path = "profile" element = { < AdminProfile /> } />
2947 < Route path = "users/sheets/:userId" element = { < AdminSheets /> } />
3048 </ Route >
49+
50+ { /* Catch-all fallback */ }
51+ < Route path = "*" element = { < Login /> } />
3152 </ Routes >
3253 </ ApiProvider >
3354 </ AuthProvider >
You can’t perform that action at this time.
0 commit comments