import { useState, useEffect } from 'react' import { getAuthStatus, getAuthKey, signIn, setTokenManually } from '../services/api' import type { RequestError } from '../services/api' import { isCadesPluginAvailable, listCertificates, signWithCadesPlugin } from '../services/cadesplugin' import type { CertInfo } from '../services/cadesplugin' interface Props { onAuth: () => void } export function AuthPage({ onAuth }: Props) { const [status, setStatus] = useState<{ authenticated: boolean; tokenExpiresAt: string | null } | null>(null) const [hasPlugin, setHasPlugin] = useState(null) const [certs, setCerts] = useState([]) const [selectedThumbprint, setSelectedThumbprint] = useState('') const [inn, setInn] = useState('') const [loading, setLoading] = useState(false) const [error, setError] = useState('') const [errorDebug, setErrorDebug] = useState(null) const [debugExpanded, setDebugExpanded] = useState(false) const [manualToken, setManualToken] = useState('') const [manualLoading, setManualLoading] = useState(false) useEffect(() => { getAuthStatus().then(setStatus).catch(() => {}) checkPlugin() }, []) async function checkPlugin() { const available = await isCadesPluginAvailable() setHasPlugin(available) if (available) { try { const certList = await listCertificates() setCerts(certList) } catch {} } } async function handleSetToken() { setManualLoading(true) setError('') setErrorDebug(null) setDebugExpanded(false) try { await setTokenManually(manualToken) const s = await getAuthStatus() setStatus(s) if (s.authenticated) onAuth() } catch (err) { const re = err as RequestError setError(re.message) if (re.debugInfo) { setErrorDebug(JSON.stringify(re.debugInfo, null, 2)) } } finally { setManualLoading(false) } } async function handleSignIn() { setLoading(true) setError('') setErrorDebug(null) setDebugExpanded(false) try { const { uuid, data } = await getAuthKey() const signature = await signWithCadesPlugin(data, selectedThumbprint || undefined) await signIn(uuid, signature, inn || undefined) const s = await getAuthStatus() setStatus(s) if (s.authenticated) onAuth() } catch (err) { const re = err as RequestError setError(re.message) if (re.debugInfo) { setErrorDebug(JSON.stringify(re.debugInfo, null, 2)) } } finally { setLoading(false) } } return (

Авторизация в True API

{hasPlugin === false && (
⚠️ КриптоПро Browser Plugin не обнаружен.
Установите расширение для браузера
)} {status?.authenticated && (
✅ Авторизован
Токен истекает: {new Date(status.tokenExpiresAt!).toLocaleString()}
)}
setInn(e.target.value)} placeholder="Введите ИНН для однозначной авторизации" style={inputStyle} />
{hasPlugin && certs.length > 0 && (
)} {certs.length === 0 && hasPlugin && (
Нет доступных сертификатов. Установите УКЭП в систему.
)}