21 lines
526 B
TypeScript
21 lines
526 B
TypeScript
import React from 'react';
|
|
import { Navigate, useLocation } from 'react-router-dom';
|
|
import { useAuth } from '../hooks/useAuth';
|
|
|
|
const RequireAuth: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
|
const { isAuthenticated, isLoading } = useAuth();
|
|
const location = useLocation();
|
|
|
|
if (isLoading) {
|
|
return <div>Loading...</div>; // Or a spinner
|
|
}
|
|
|
|
if (!isAuthenticated) {
|
|
return <Navigate to="/login" state={{ from: location }} replace />;
|
|
}
|
|
|
|
return children;
|
|
};
|
|
|
|
export default RequireAuth;
|