"use client";

import { getToken } from "@/lib/token";
import { useRouter } from "next/navigation";
import { useEffect, useState } from "react";

export default function Guard({ children }: { children: React.ReactNode }) {
  const r = useRouter();
  const [ready, setReady] = useState(false);

  useEffect(() => {
    const t = getToken();
    if (!t) r.replace("/login");
    else setReady(true);
  }, [r]);

  if (!ready) return null;
  return <>{children}</>;
}
