'use client'

import { ReactNode, useEffect } from 'react';
import { useRouter } from 'next/router';
import { getToken } from "../../services/global";

// Create the WithAuthRedirect component
type WithAuthRedirectProps = {
    children: ReactNode;
};

export function WithAuthRedirect({ children }: WithAuthRedirectProps) {
    const {push} = useRouter();

    useEffect(() => {
        const token = getToken();

        if (!token) {
            // Redirect to the login page if no token is present
            push('/login');
        }
    }, []);

    return <>{children}</>;
}