"use client"; import { useEffect } from "react"; import { usePathname, useRouter } from "next/navigation"; import { useSession } from "@/lib/store"; import { Skeleton } from "@/components/ui"; /** * Gate for player-only screens. Guests are sent to /login?next=; * while the session is still hydrating, a skeleton keeps the layout stable. */ export function RequireAuth({ children, fallback }: { children: React.ReactNode; fallback?: React.ReactNode }) { const status = useSession((s) => s.status); const router = useRouter(); const pathname = usePathname(); useEffect(() => { if (status === "guest") router.replace(`/login?next=${encodeURIComponent(pathname)}`); }, [status, router, pathname]); if (status !== "authenticated") { return ( <> {fallback ?? (
)} ); } return <>{children}; }