'use client'; import Link from 'next/link'; import { FormEvent, useState } from 'react'; import { useRouter } from 'next/navigation'; import { ChevronLeft, DoorOpen, UserRound } from 'lucide-react'; import { BrandLogo } from '@/components/id/brand-logo'; import { useAuth } from '@/components/id/auth-provider'; import { useToast } from '@/components/id/toast-provider'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { PinInput } from '@/components/ui/pin-input'; import { isPinInputComplete } from '@/lib/pin-input'; import { OtpInput } from '@/components/ui/otp-input'; import { PhoneInput, phoneCountries, toE164 } from '@/components/ui/phone-input'; import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; type Step = 'contact' | 'otp' | 'pin'; export default function RegisterPage() { const router = useRouter(); const { sendLoginOtp, verifyLoginOtp, completePin, logout } = useAuth(); const { showToast } = useToast(); const [authTab, setAuthTab] = useState<'email' | 'phone'>('email'); const [email, setEmail] = useState(''); const [phoneNumber, setPhoneNumber] = useState(''); const [country, setCountry] = useState(phoneCountries[0]); const [recipient, setRecipient] = useState(''); const [maskedTarget, setMaskedTarget] = useState(''); const [otp, setOtp] = useState(''); const [pin, setPin] = useState(''); const [pendingSessionId, setPendingSessionId] = useState(null); const [step, setStep] = useState('contact'); const [isSubmitting, setIsSubmitting] = useState(false); function getIdentifier() { return authTab === 'email' ? email.trim() : toE164(country, phoneNumber); } function finishRegistration(pinVerified: boolean, sessionId: string) { if (!pinVerified) { setPendingSessionId(sessionId); setStep('pin'); return; } showToast('Добро пожаловать! ID успешно создан.'); router.push('/'); } async function handleSendCode(event: FormEvent) { event.preventDefault(); setIsSubmitting(true); try { const identifier = getIdentifier(); setRecipient(identifier); const response = await sendLoginOtp(identifier); setMaskedTarget(response.maskedTarget); setOtp(''); setStep('otp'); showToast('Код отправлен. Введите его ниже.'); } catch (error) { showToast(error instanceof Error ? error.message : 'Не удалось отправить код'); } finally { setIsSubmitting(false); } } async function verifyOtp(code: string) { setIsSubmitting(true); try { const response = await verifyLoginOtp(recipient, code); if (response.auth) { finishRegistration(response.auth.pinVerified, response.auth.sessionId); } else { showToast('Не удалось завершить регистрацию'); } } catch (error) { setOtp(''); showToast(error instanceof Error ? error.message : 'Неверный код'); } finally { setIsSubmitting(false); } } async function handlePinSubmit(event: FormEvent) { event.preventDefault(); if (!pendingSessionId) return; setIsSubmitting(true); try { await completePin(pendingSessionId, pin); showToast('Добро пожаловать! ID успешно создан.'); router.push('/'); } catch (error) { showToast(error instanceof Error ? error.message : 'Не удалось проверить PIN-код'); } finally { setIsSubmitting(false); } } function resetToContact() { setStep('contact'); setOtp(''); setPin(''); setPendingSessionId(null); } return (

Создайте ID

{step === 'contact' ? 'Введите почту или телефон. Пароль не нужен.' : 'Подтвердите код — аккаунт создастся автоматически.'}

{step !== 'contact' ? ( ) : null} {step === 'contact' ? (
setAuthTab(value as 'email' | 'phone')} className="w-full"> Почта Телефон setEmail(event.target.value)} required={authTab === 'email'} />
) : null} {step === 'otp' ? (

Код отправлен на

{maskedTarget || recipient}

{isSubmitting ?

Создаём ваш ID...

: null}
) : null} {step === 'pin' && pendingSessionId ? (

Установите PIN для завершения регистрации

) : null} {step === 'contact' ? ( ) : null}
); }