import useAuth from '../hooks/useAuth';
import { redirectToLogin } from '../utils';
import Loading from './Loading';

export default function RequireAuth({ children }: { children: JSX.Element }) {
  const auth = useAuth();

  if (auth.isLoading === false && !auth.userInfo) {
    redirectToLogin();
    return null;
  }

  if (auth.isLoading) {
    return <Loading />;
  }

  return children;
}
