"use client"; import { useEffect, useState } from "react"; import { auth } from "@/lib/firebase"; import { onAuthStateChanged } from "firebase/auth"; import { useRouter, usePathname } from "next/navigation"; import { motion, AnimatePresence } from "framer-motion"; export default function AuthGuard({ children }: { children: React.ReactNode }) { const [loading, setLoading] = useState(true); const [user, setUser] = useState(null); const router = useRouter(); const pathname = usePathname(); useEffect(() => { const unsubscribe = onAuthStateChanged(auth, (user) => { setUser(user); setLoading(false); if (!user && pathname !== "/login") { router.push("/login"); } }); return () => unsubscribe(); }, [pathname, router]); if (loading) { return (
); } // If not logged in and not on login page, don't show children (redirecting) if (!user && pathname !== "/login") { return null; } return <>{children}; }