import { useState } from "react"
import { Navigate, useLocation } from "react-router-dom"
import { isAuthed } from "@/api/token"

export default function AuthGuard({ children }: { children: React.ReactNode }) {
  const location = useLocation()
  const [isValid] = useState(isAuthed)

  if (!isValid) {
    return (
      <Navigate
        to={`/login?redirect=${encodeURIComponent(location.pathname)}`}
        replace
      />
    )
  }

  return <>{children}</>
}
