import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'
import { getAuth } from '@/core/utils/auth'

const IsAuth = ({ children }) => {
  const router = useRouter()
  const [response, setResponse] = useState(<></>)

  useEffect(() => {
    if (!getAuth() && router.pathname != '/login') {
      router.replace(`/login?next=${router.asPath}`)
    } else {
      setResponse(children)
    }
  }, [children, router])

  return response
}

export default IsAuth
