import { useEffect, useState } from 'react' import { Alert, Button, Card, Form, Input, Typography } from 'antd' import { MailOutlined, SafetyOutlined } from '@ant-design/icons' import { loginByEmailCode, requestCode, AuthError } from '@/auth/authStore' const RESEND_COOLDOWN_SECONDS = 60 /** * Вход по email-коду: код приходит на почту (в dev без SMTP бэкенд пишет его в лог). */ export function LoginScreen() { const [step, setStep] = useState<'email' | 'code'>('email') const [email, setEmail] = useState('') const [error, setError] = useState(null) const [busy, setBusy] = useState(false) const [cooldown, setCooldown] = useState(0) useEffect(() => { if (cooldown <= 0) return const timer = setInterval(() => setCooldown((c) => c - 1), 1000) return () => clearInterval(timer) }, [cooldown]) const submitEmail = async (values: { email: string }) => { setBusy(true) setError(null) try { await requestCode(values.email) setEmail(values.email) setStep('code') setCooldown(RESEND_COOLDOWN_SECONDS) } catch (e) { setError(describe(e)) } finally { setBusy(false) } } const submitCode = async (values: { code: string }) => { setBusy(true) setError(null) try { await loginByEmailCode(email, values.code) // Сессия появится в AuthContext через подписку authStore } catch (e) { setError(describe(e)) } finally { setBusy(false) } } return (
Городар · Админ-панель {step === 'email' ? 'Введите рабочий email — пришлём код подтверждения' : `Код отправлен на ${email}`} {error && ( )} {step === 'email' ? (
} placeholder="email@example.com" size="large" autoFocus />
) : (
} placeholder="000000" size="large" autoFocus maxLength={6} inputMode="numeric" />
)}
) } function describe(e: unknown): string { if (e instanceof AuthError) return e.description if (e instanceof Error) return e.message return 'Ошибка авторизации' }