35 lines
1.0 KiB
TypeScript
35 lines
1.0 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 { status } = useSession();
|
|
const pathname = usePathname();
|
|
const router = useRouter();
|
|
|
|
useEffect(() => {
|
|
// Simple redirect to login page if not authenticated
|
|
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}</>;
|
|
}
|