fastcheck/src/components/auth/ProtectedRoute.tsx
2026-04-08 13:58:46 -04:00

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;