added ProtectedRoute for /users

This commit is contained in:
Davor 2022-05-19 18:38:32 +02:00
parent f0439dba40
commit eff6ce851f

View file

@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import { Helmet } from 'react-helmet'; import { Helmet } from 'react-helmet';
import { Routes, Route, Navigate } from 'react-router-dom'; import { Routes, Route, Navigate, Outlet } from 'react-router-dom';
import { Toaster } from 'react-hot-toast'; import { Toaster } from 'react-hot-toast';
import { useAuth } from 'src/services/auth'; import { useAuth } from 'src/services/auth';
@ -10,10 +10,14 @@ import { LoginCallback } from './modules/login/LoginCallback';
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
function App() { function App() {
const { authToken, currentUser } = useAuth(); const { authToken, currentUser, isAdmin } = useAuth();
const redirectToLogin = !authToken || !currentUser?.app_roles; const redirectToLogin = !authToken || !currentUser?.app_roles;
const ProtectedRoute = () => {
return isAdmin ? <Outlet /> : <Navigate to="/dashboard" />;
};
return ( return (
<> <>
<Helmet> <Helmet>
@ -38,7 +42,9 @@ function App() {
<Layout> <Layout>
<Routes> <Routes>
<Route path="/dashboard" element={<Dashboard />} /> <Route path="/dashboard" element={<Dashboard />} />
<Route path="/users" element={<Users />} /> <Route path="/users" element={<ProtectedRoute />}>
<Route path="/users" element={<Users />} />
</Route>
<Route path="*" element={<Navigate to="/dashboard" />} /> <Route path="*" element={<Navigate to="/dashboard" />} />
</Routes> </Routes>
</Layout> </Layout>