EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
PrivateRoute.jsx
Go to the documentation of this file.
1import { Navigate, useLocation } from 'react-router-dom';
2import { isTokenExpired } from '../lib/jwt';
3import { apiFetch } from '../lib/api';
4
5function PrivateRoute({ children }) {
6 const token = localStorage.getItem('token');
7 const location = useLocation();
8
9 if (!token || isTokenExpired(token)) {
10 localStorage.removeItem('token');
11 // Redirect to login page but save the attempted URL
12 return <Navigate to="/login" state={{ from: location }} replace />;
13 }
14
15 return children;
16}
17
18export default PrivateRoute;