import { RouterProvider, createBrowserRouter } from "react-router-dom"; import { useAuth } from "../provider/authProvider"; import { ProtectedRoute } from "./ProtectedRoute"; import Logout from "../pages/Logout"; import Login from "../pages/Login"; const Routes = () => { const { token } = useAuth(); // Define public routes accessible to all users const routesForPublic = [ { path: "/service", element:
Service Page
, }, { path: "/about-us", element:
About Us
, }, ]; // Define routes accessible only to authenticated users const routesForAuthenticatedOnly = [ { path: "/", element: , // Wrap the component in ProtectedRoute children: [ { path: "/", element:
User Home Page
, }, { path: "/profile", element:
User Profile
, }, { path: "/logout", element: , }, ], }, ]; // Define routes accessible only to non-authenticated users const routesForNotAuthenticatedOnly = [ { path: "/", element:
Home Page
, }, { path: "/login", element: , }, ]; // Combine and conditionally include routes based on authentication status const router = createBrowserRouter([ ...routesForPublic, ...(!token ? routesForNotAuthenticatedOnly : []), ...routesForAuthenticatedOnly, ]); // Provide the router configuration using RouterProvider return ; }; export default Routes;