"use client"; import { useState, useEffect, useRef } from "react"; import Modal from "./Modal"; import Button from "./Button"; import { copyToClipboard } from "@/shared/utils/clipboard"; type KiroSocialOAuthModalProps = { isOpen: boolean; provider: "google" | "github"; targetProvider?: string; providerLabel?: string; onSuccess?: () => void; onClose: () => void; }; export default function KiroSocialOAuthModal({ isOpen, provider, targetProvider, providerLabel = "Kiro", onSuccess, onClose, }: KiroSocialOAuthModalProps) { const [step, setStep] = useState<"loading" | "polling" | "success" | "error">("loading"); const [error, setError] = useState(null); const [userCode, setUserCode] = useState(""); const [authUrl, setAuthUrl] = useState(""); const pollRef = useRef | null>(null); useEffect(() => { if (!isOpen || !provider) return; const initAuth = async () => { try { setError(null); setStep("loading"); const res = await fetch(`/api/oauth/kiro/social-authorize?provider=${provider}`); const data = await res.json(); if (!res.ok) { throw new Error(data.error || "Failed to start authorization"); } setUserCode(data.userCode || ""); setAuthUrl(data.authUrl || ""); setStep("polling"); const interval = (data.interval || 5) * 1000; pollRef.current = setInterval(async () => { try { const pollRes = await fetch("/api/oauth/kiro/social-exchange", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ deviceCode: data.deviceCode, provider, targetProvider }), }); const pollData = await pollRes.json(); if (pollData.success) { if (pollRef.current) clearInterval(pollRef.current); pollRef.current = null; setStep("success"); onSuccess?.(); } } catch { // Network error, keep polling } }, interval); } catch (err: any) { setError(err.message); setStep("error"); } }; initAuth(); return () => { if (pollRef.current) { clearInterval(pollRef.current); pollRef.current = null; } }; }, [isOpen, provider]); const handleClose = () => { if (pollRef.current) { clearInterval(pollRef.current); pollRef.current = null; } onClose(); }; const providerName = provider === "google" ? "Google" : "GitHub"; return (
{step === "loading" && (
progress_activity

Initializing...

Setting up {providerName} authentication

)} {step === "polling" && (
open_in_browser

Open this link in an Incognito window

Use an Incognito/Private window to avoid session conflicts with existing accounts.

{authUrl && (
{authUrl.length > 80 ? authUrl.slice(0, 80) + "..." : authUrl}
)} {userCode && (

Verification code

{userCode}

)}
progress_activity Waiting for authorization...
)} {step === "success" && (
check_circle

Connected Successfully!

Your {providerLabel} account via {providerName} has been connected.

)} {step === "error" && (
error

Connection Failed

{error}

)}
); }