27 lines
752 B
TypeScript
27 lines
752 B
TypeScript
import { Navigate, Outlet, useLocation } from 'react-router-dom';
|
|
import { useAuth } from '../../contexts/AuthContext';
|
|
import PageLoader from '../common/PageLoader';
|
|
import DashboardLayout from '../layouts/DashboardLayout';
|
|
|
|
const ProtectedRoute = () => {
|
|
const { isAuthenticated, loading } = useAuth();
|
|
const location = useLocation();
|
|
|
|
if (loading) {
|
|
return <PageLoader />;
|
|
}
|
|
|
|
if (!isAuthenticated) {
|
|
// Redirect to login page but save the page they tried to access
|
|
return <Navigate to="/login" state={{ from: location }} replace />;
|
|
}
|
|
|
|
// User is authenticated, render the protected route with dashboard layout
|
|
return (
|
|
<DashboardLayout>
|
|
<Outlet />
|
|
</DashboardLayout>
|
|
);
|
|
};
|
|
|
|
export default ProtectedRoute; |