Skip to content

Commit

Permalink
fix(*): add private router
Browse files Browse the repository at this point in the history
  • Loading branch information
HoseaCodes committed Feb 12, 2024
1 parent 94e12e8 commit afc2072
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/PrivateRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from "react";
import { Route, Redirect } from "react-router-dom";

const PrivateRoute = ({ type, exact, path, element }) => {
const isLoggedIn = localStorage.getItem("isLoggedIn");
const isAdmin = localStorage.getItem("isAdmin");

if (type === "login") {
return isLoggedIn ? (
<Route path={path} exact={exact} component={element} />
) : (
<Redirect to="/login" />
);
}

if (type === "admin") {
return isAdmin ? (
<Route path={path} exact={exact} component={element} />
) : (
<Redirect to="/" />
);
}

return <Redirect to="/" />;
};

export default PrivateRoute;

0 comments on commit afc2072

Please sign in to comment.