import { auth } from "@/auth";
import { redirect } from "next/navigation";
import AutoLogin from "./AutoLogin";

export default async function LoginPage({
  searchParams,
}: {
  searchParams: Promise<{ callbackUrl?: string }>;
}) {
  const { callbackUrl } = await searchParams;
  const destination = callbackUrl || "/";

  if (process.env.SKIP_AUTH === "true") {
    redirect(destination);
  }

  const session = await auth();
  const hasError = (session as unknown as { error?: string } | null)?.error;
  if (session?.user && !hasError) {
    redirect(destination);
  }

  return <AutoLogin callbackUrl={destination} />;
}
