Skip to content

Commit

Permalink
PrivateRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
wmazoni committed May 1, 2024
1 parent f50e562 commit 36c634c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
25 changes: 25 additions & 0 deletions frontend/src/components/PrivateRoute/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Redirect, Route } from 'react-router-dom';
import { isAuthenticated } from 'util/requests';


type Props = {
children: React.ReactNode;
path: string;
};


const PrivateRoute = ({ children, path }: Props) => {


return (
<Route
path={path}
render={() =>
isAuthenticated() ? children : <Redirect to="/admin/auth/login" />
}
/>
);
};


export default PrivateRoute;
15 changes: 8 additions & 7 deletions frontend/src/pages/Admin/index.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { Route, Switch } from "react-router-dom";
import { Switch } from "react-router-dom";
import Navbar from "./Navbar";
import './styles.css'
import User from "./User";
import PrivateRoute from "components/PrivateRoute";

const Admin = () => {
return (
<div className="admin-container">
<Navbar />
<div className="admin-content">
<Switch>
<Route path="/admin/products">
<PrivateRoute path="/admin/products">
<h1>Product CRUD</h1>
</Route>
<Route path="/admin/categories">
</PrivateRoute>
<PrivateRoute path="/admin/categories">
<h1>Product CRUD</h1>
</Route>
<Route path="/admin/users">
</PrivateRoute>
<PrivateRoute path="/admin/users">
<User />
</Route>
</PrivateRoute>
</Switch>
</div>
</div>
Expand Down

0 comments on commit 36c634c

Please sign in to comment.