"use client"; import { useEffect, useState } from "react"; import { useRouter, useSearchParams } from "next/navigation"; import { apiFetch } from "@/lib/api"; export default function GoogleCallbackPage() { const router = useRouter(); const searchParams = useSearchParams(); const [status, setStatus] = useState<"loading" | "success" | "error">("loading"); const [message, setMessage] = useState(""); useEffect(() => { const code = searchParams.get("code"); const error = searchParams.get("error"); if (error) { setStatus("error"); setMessage(error === "access_denied" ? "You declined Google access." : `Google returned an error: ${error}`); return; } if (!code) { setStatus("error"); setMessage("No authorization code received from Google."); return; } apiFetch<{ connected: boolean; googleEmail: string }>("/api/google/exchange", { method: "POST", body: JSON.stringify({ code }), }).then((res) => { if (res.error) { setStatus("error"); setMessage(res.error.message ?? "Failed to connect Google account."); } else { setStatus("success"); setMessage(`Connected as ${res.data?.googleEmail ?? "your Google account"}.`); setTimeout(() => router.replace("/exports"), 2000); } }); }, [searchParams, router]); return (
{status === "loading" && ( <>

Connecting your Google account...

)} {status === "success" && ( <>

Google Connected!

{message}

Redirecting to Exports...

)} {status === "error" && ( <>

Connection Failed

{message}

)}
); }