Neah/components/auth/auth-check.tsx
2025-05-03 12:58:38 +02:00

38 lines
1.1 KiB
TypeScript

"use client";
import { useSession } from "next-auth/react";
import { usePathname, useRouter } from "next/navigation";
import { useEffect } from "react";
export function AuthCheck({ children }: { children: React.ReactNode }) {
const session = useSession();
const pathname = usePathname();
const router = useRouter();
// Safely extract status with a fallback
const status = session?.status || "loading";
useEffect(() => {
// Only redirect if we're certain the user is unauthenticated
if (status === "unauthenticated" && !pathname?.includes("/signin")) {
router?.push("/signin");
}
}, [status, router, pathname]);
// Simple loading state
if (status === "loading") {
return (
<div className="flex justify-center items-center min-h-screen">
<div className="animate-spin h-10 w-10 border-4 border-blue-500 rounded-full border-t-transparent"></div>
</div>
);
}
// Don't render on unauthenticated
if (status === "unauthenticated" && !pathname?.includes("/signin")) {
return null;
}
// Render children if authenticated
return <>{children}</>;
}