22 lines
611 B
TypeScript
22 lines
611 B
TypeScript
import React from 'react';
|
|
import { Navigate, useLocation } from 'react-router-dom';
|
|
import { useAuth } from '../hooks/useAuth';
|
|
import { LoadingOverlay } from './LoadingStates';
|
|
|
|
const RequireAuth: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
|
const { isAuthenticated, isLoading } = useAuth();
|
|
const location = useLocation();
|
|
|
|
if (isLoading) {
|
|
return <LoadingOverlay message="Authenticating..." />; // Consistent loading UX
|
|
}
|
|
|
|
if (!isAuthenticated) {
|
|
return <Navigate to="/login" state={{ from: location }} replace />;
|
|
}
|
|
|
|
return children;
|
|
};
|
|
|
|
export default RequireAuth;
|