"use client"; import { useState, useEffect } from "react"; import PropTypes from "prop-types"; import { Modal, Button, Input } from "@/shared/components"; /** * Kiro Auth Method Selection Modal * Auto-detects token from AWS SSO cache or allows manual import */ export default function KiroAuthModal({ isOpen, onMethodSelect, onClose }) { 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"); 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(); }, [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", { 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 - notify parent to refresh connections onMethodSelect("import"); } 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 - HIDDEN */} {/* GitHub Social Login - HIDDEN */} {/* 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 from AWS SSO cache

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

Token auto-detected from Kiro IDE successfully!

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

Kiro IDE not detected. Please paste your refresh token manually.

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

{error}

)}
)}
)}
); } KiroAuthModal.propTypes = { isOpen: PropTypes.bool.isRequired, onMethodSelect: PropTypes.func.isRequired, onClose: PropTypes.func.isRequired, };