Add Protected Routes
This commit is contained in:
12
src/components/Routes/ProtectedRoute.jsx
Normal file
12
src/components/Routes/ProtectedRoute.jsx
Normal file
@@ -0,0 +1,12 @@
|
||||
import React from 'react';
|
||||
import { Redirect, Route } from 'react-router-dom';
|
||||
import { useUserContext } from '../Contexts/UserContext';
|
||||
|
||||
export const ProtectedRoute = ({ render, ...others }) => {
|
||||
const { user, setError } = useUserContext();
|
||||
if (!user || !user.sub || user.sub.length === 0) {
|
||||
setError('Please sign in to access');
|
||||
return <Redirect to="/" />;
|
||||
}
|
||||
return <Route render {...others} />;
|
||||
}
|
||||
Reference in New Issue
Block a user