/* eslint-disable react-hooks/rules-of-hooks */
/* eslint-disable react/display-name */
// components/ProtectedRoute.js
import { useRouter } from "next/router";
import { useEffect } from "react";
import { getSession } from "next-auth/react";

export const ProtectedRoute = (WrappedComponent: any) => {
  return (props: JSX.IntrinsicAttributes) => {
    const router = useRouter();
    useEffect(() => {
      const checkAuthentication = async () => {
        const session = await getSession();
        if (session) {
          // User is not authenticated, redirect to login page
          router.push("/dashboard");
        }
      };
      checkAuthentication();
    }, []);

    return <WrappedComponent {...props} />;
  };
};

export default ProtectedRoute;
