"use client"; import React, { useState } from "react"; import { useRouter } from "next/navigation"; const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; const API_BASE = process.env.NEXT_PUBLIC_API_BASE_URL; const RegPage = () => { const router = useRouter(); const [firstName, setFirstName] = useState(""); // имя const [lastName, setLastName] = useState(""); // фамилия const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [rememberMe, setRememberMe] = useState(false); const [checkboxError, setCheckboxError] = useState(false); const [authError, setAuthError] = useState(""); const [isSubmitting, setIsSubmitting] = useState(false); const isEmailValid = emailRegex.test(email); const isFormValid = isEmailValid && password.length > 0 && firstName.trim().length > 0 && lastName.trim().length > 0; const handleSubmit = async (e) => { e.preventDefault(); if (!rememberMe) { setCheckboxError(true); return; } setCheckboxError(false); if (!isFormValid || !API_BASE) return; try { setAuthError(""); setIsSubmitting(true); const res = await fetch(`${API_BASE}/auth/register`, { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/json", }, body: JSON.stringify({ email, password, first_name: firstName, last_name: lastName, // остальные поля можно не отправлять, если не обязательные }), }); if (!res.ok) { let msg = "Ошибка регистрации"; try { const data = await res.json(); if (data.error) msg = data.error; } catch { const text = await res.text(); if (text) msg = text; } setAuthError(msg); setIsSubmitting(false); return; } const data = await res.json(); console.log("Регистрация успешна, ответ API:", data); router.push("/regCode"); } catch (err) { setAuthError(err.message || "Ошибка сети"); setIsSubmitting(false); } }; return (