diff --git a/React-frontend/src/components/routes/AdminRoute.js b/React-frontend/src/components/routes/AdminRoute.js new file mode 100644 index 00000000..a2052ac0 --- /dev/null +++ b/React-frontend/src/components/routes/AdminRoute.js @@ -0,0 +1,23 @@ +import React from 'react'; +import { useSelector } from 'react-redux'; +import { Route, Redirect } from 'react-router-dom'; + +const AdminRoute = ({ component: Component, ...rest }) => { + const { isAuthenticated, isLoading, user } = useSelector(state => state.auth); + + return ( + + (!isLoading && !isAuthenticated) || + (!isLoading && isAuthenticated && user.role !== 'admin') ? ( + + ) : ( + + ) + } + /> + ); +}; + +export default AdminRoute;