"use client"; import { useState, useEffect } from "react"; import Modal from "./Modal"; import Button from "./Button"; import Input from "./Input"; type KiroAuthModalProps = { isOpen: boolean; providerId?: string; providerLabel?: string; onMethodSelect: (method: string, config?: Record) => void; onClose: () => void; }; /** * Kiro Auth Method Selection Modal * Auto-detects token from AWS SSO cache or allows manual import */ export default function KiroAuthModal({ isOpen, providerId = "kiro", providerLabel = "Kiro", onMethodSelect, onClose, }: KiroAuthModalProps) { const [selectedMethod, setSelectedMethod] = useState(null); const [idcStartUrl, setIdcStartUrl] = useState(""); const [idcRegion, setIdcRegion] = useState("us-east-1"); const [refreshToken, setRefreshToken] = useState(""); const [error, setError] = useState(null); const [importing, setImporting] = useState(false); const [autoDetecting, setAutoDetecting] = useState(false); const [autoDetected, setAutoDetected] = useState(false); // Auto-detect token when import method is selected useEffect(() => { if (selectedMethod !== "import" || !isOpen) return; const autoDetect = async () => { setAutoDetecting(true); setError(null); setAutoDetected(false); try { const res = await fetch( `/api/oauth/kiro/auto-import?targetProvider=${encodeURIComponent(providerId)}` ); const data = await res.json(); if (data.found) { setRefreshToken(data.refreshToken); setAutoDetected(true); } else { setError(data.error || "Could not auto-detect token"); } } catch (err) { setError("Failed to auto-detect token"); } finally { setAutoDetecting(false); } }; autoDetect(); }, [providerId, selectedMethod, isOpen]); const handleMethodSelect = (method) => { setSelectedMethod(method); setError(null); }; const handleBack = () => { setSelectedMethod(null); setError(null); }; const handleImportToken = async () => { if (!refreshToken.trim()) { setError("Please enter a refresh token"); return; } setImporting(true); setError(null); try { const res = await fetch( `/api/oauth/kiro/import?targetProvider=${encodeURIComponent(providerId)}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ refreshToken: refreshToken.trim() }), } ); const data = await res.json(); if (!res.ok) { throw new Error(data.error || "Import failed"); } // Success - close modal onClose(); } catch (err) { setError(err.message); } finally { setImporting(false); } }; const handleIdcContinue = () => { if (!idcStartUrl.trim()) { setError("Please enter your IDC start URL"); return; } onMethodSelect("idc", { startUrl: idcStartUrl.trim(), region: idcRegion }); }; const handleSocialLogin = (provider) => { onMethodSelect("social", { provider }); }; return (
{/* Method Selection */} {!selectedMethod && (

Choose your authentication method:

{/* AWS Builder ID */} {/* AWS IAM Identity Center (IDC) */} {/* Google Social Login */} {/* GitHub Social Login */} {/* Import Token */}
)} {/* IDC Configuration */} {selectedMethod === "idc" && (
setIdcStartUrl(e.target.value)} placeholder="https://your-org.awsapps.com/start" className="font-mono text-sm" />

Your organization's AWS IAM Identity Center URL

setIdcRegion(e.target.value)} placeholder="us-east-1" className="font-mono text-sm" />

AWS region for your Identity Center (default: us-east-1)

{error &&

{error}

}
)} {/* Social Login Info (Google) */} {selectedMethod === "social-google" && (
info

Manual Callback Required

After login, you'll need to copy the callback URL from your browser and paste it back here.

)} {/* Social Login Info (GitHub) */} {selectedMethod === "social-github" && (
info

Manual Callback Required

After login, you'll need to copy the callback URL from your browser and paste it back here.

)} {/* Import Token */} {selectedMethod === "import" && (
{/* Auto-detecting state */} {autoDetecting && (
progress_activity

Auto-detecting token...

Reading {providerLabel} credentials from AWS SSO cache

)} {/* Form (shown after auto-detect completes) */} {!autoDetecting && ( <> {/* Success message if auto-detected */} {autoDetected && (
check_circle

Token auto-detected from {providerLabel} successfully!

)} {/* Info message if not auto-detected */} {!autoDetected && !error && (
info

{providerLabel} token was not auto-detected. Please paste your refresh token manually.

)}
setRefreshToken(e.target.value)} placeholder="Token will be auto-filled..." className="font-mono text-sm" />
{error && (

{error}

)}
)}
)}
); }