"use client"; import { useState, useEffect } from "react"; import PropTypes from "prop-types"; import { Modal, Button, Input } from "@/shared/components"; /** * Cursor Auth Modal * Auto-detect and import token from Cursor IDE's local SQLite database */ export default function CursorAuthModal({ isOpen, onSuccess, onClose }) { const [accessToken, setAccessToken] = useState(""); const [machineId, setMachineId] = useState(""); const [error, setError] = useState(null); const [importing, setImporting] = useState(false); const [autoDetecting, setAutoDetecting] = useState(false); const [autoDetected, setAutoDetected] = useState(false); const [windowsManual, setWindowsManual] = useState(false); const runAutoDetect = async () => { setAutoDetecting(true); setError(null); setAutoDetected(false); setWindowsManual(false); try { const res = await fetch("/api/oauth/cursor/auto-import"); const data = await res.json(); if (data.found) { setAccessToken(data.accessToken); setMachineId(data.machineId); setAutoDetected(true); } else if (data.windowsManual) { setWindowsManual(true); } else { setError(data.error || "Could not auto-detect tokens"); } } catch (err) { setError("Failed to auto-detect tokens"); } finally { setAutoDetecting(false); } }; // Auto-detect tokens when modal opens useEffect(() => { if (!isOpen) return; runAutoDetect(); }, [isOpen]); const handleImportToken = async () => { if (!accessToken.trim()) { setError("Please enter an access token"); return; } if (!machineId.trim()) { setError("Please enter a machine ID"); return; } setImporting(true); setError(null); try { const res = await fetch("/api/oauth/cursor/import", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ accessToken: accessToken.trim(), machineId: machineId.trim(), }), }); const data = await res.json(); if (!res.ok) { throw new Error(data.error || "Import failed"); } onSuccess?.(); onClose(); } catch (err) { setError(err.message); } finally { setImporting(false); } }; return (
{/* Auto-detecting state */} {autoDetecting && (
progress_activity

Auto-detecting tokens...

Reading from Cursor IDE database

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

Tokens auto-detected from Cursor IDE successfully!

)} {/* Windows manual instructions */} {windowsManual && (
info

Could not read Cursor database automatically.

Make sure Cursor IDE has been opened at least once, then click Retry. If the problem persists, paste your tokens manually below.

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

Cursor IDE not detected. Please paste your tokens manually.

)} {/* Access Token Input */}