import { useState } from 'react' import type { DeviceCodeInfo } from '../types/ipc' import { useLanguage } from '../contexts/LanguageContext' import Header from '../components/Header' interface AuthPageProps { onSuccess: (username: string) => void } type AuthView = 'default' | 'oauth-pending' | 'token-input' export default function AuthPage({ onSuccess }: AuthPageProps) { const { t } = useLanguage() const [view, setView] = useState('default') const [deviceCode, setDeviceCode] = useState(null) const [loading, setLoading] = useState(false) const [tokenInput, setTokenInput] = useState('') const [error, setError] = useState('') const [polling, setPolling] = useState(false) const [copied, setCopied] = useState(false) const handleOAuth = async () => { setLoading(true) setError('') try { const code = await window.electronAPI.getDeviceCode() setDeviceCode(code) setView('oauth-pending') setPolling(true) const unsubscribe = window.electronAPI.onAuthSuccess((result) => { unsubscribe() setPolling(false) if (result.success && result.username) { onSuccess(result.username) } else { setError(result.error ?? t('auth.authFailed')) setView('default') } }) } catch (err) { setError((err as Error).message) } finally { setLoading(false) } } const handleOpenDeviceUrl = () => { if (deviceCode) window.electronAPI.openUrl(deviceCode.verification_uri) } const handleCopyCode = () => { if (!deviceCode) return navigator.clipboard.writeText(deviceCode.user_code).then(() => { setCopied(true) setTimeout(() => setCopied(false), 1500) }) } const handleSaveToken = async () => { if (!tokenInput.trim()) return setLoading(true) setError('') try { const result = await window.electronAPI.saveToken(tokenInput.trim()) if (result.success && result.username) { onSuccess(result.username) } else { setError(result.error ?? t('auth.tokenInvalid')) } } catch (err) { setError((err as Error).message) } finally { setLoading(false) } } const handleBack = () => { setView('default') setDeviceCode(null) setError('') setPolling(false) setTokenInput('') } return (
{/* Logo and title */}
CA

Copilot API

{t('auth.subtitle')}

{/* Default state: two actions */} {view === 'default' && (
)} {/* OAuth pending state */} {view === 'oauth-pending' && deviceCode && (

{t('auth.deviceCode')}

{deviceCode.user_code}

{t('auth.deviceCodeUrl')}

{polling && (

{t('auth.waitingAuth')}

)}
)} {/* Expanded token input state */} {view === 'token-input' && (