19 lines
535 B
TypeScript
19 lines
535 B
TypeScript
import React from 'react';
|
|
import { Navigate, useLocation } from 'react-router-dom';
|
|
import { useAuthStore } from '@/store/authStore';
|
|
import { useAuth } from '@/hooks/useAuth';
|
|
|
|
export function ProtectedRoute({ children }: { children: React.ReactNode }) {
|
|
const { isAuthenticated } = useAuthStore();
|
|
const location = useLocation();
|
|
|
|
// Load user data on mount (handles page refresh)
|
|
useAuth();
|
|
|
|
if (!isAuthenticated) {
|
|
return <Navigate to="/login" state={{ from: location }} replace />;
|
|
}
|
|
|
|
return <>{children}</>;
|
|
}
|