I am using React-Router-Dom for page routes. I need to separate admin routes from public ones.Can anyone tell me how do i do it.
App.js
import { createBrowserRouter,RouterProvider } from "react-router-dom";
import HomePage from "./Pages/home";
import Contact from './Pages/contact';
import AboutPage from "./Pages/About";
import RootLayout from "./Pages/Root";
import ErrorPage from "./Pages/error";
import ProductPage from "./Pages/Product";
import ProductDetails from "./Pages/ProductDetails";
const router = createBrowserRouter([
{
path: '/',
elements: <RootLayout />,
errorElement: <ErrorPage />,
children: [
{path:'/', element:<HomePage />},
{ path: 'contact', element: <Contact /> },
{ path: 'about', element: <AboutPage /> },
{ path: 'product', element: <ProductPage /> },
{ path: 'product/:productId', element: <ProductDetails /> },
],
},
]);
function App() {
return (
<RouterProvider router={router} />
);
}
export default App;
I want admin Routes that are only accessible to admins only.
2
Answers
Here is an example
App.js
PrivateRoute.js
Reference article : https://coderomeos.org/create-private-authenticated-routes-in-react
Here is an example how you can do it: