"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 (
{(checkboxError || authError) && (
{checkboxError ? "Вы не согласны с условиями использования" : authError}
)}
Регистрация
{/* Имя */}
setFirstName(e.target.value)} className="w-full rounded-full bg-white px-4 py-2 text-sm font-montserrat text-black outline-none focus:ring-2 focus:ring-blue-200" /> {firstName.trim().length === 0 && (

Введите имя

)}
{/* Фамилия */}
setLastName(e.target.value)} className="w-full rounded-full bg-white px-4 py-2 text-sm font-montserrat text-black outline-none focus:ring-2 focus:ring-blue-200" /> {lastName.trim().length === 0 && (

Введите фамилию

)}
{/* Почта */}
setEmail(e.target.value)} className="w-full rounded-full bg-white px-4 py-2 text-sm font-montserrat text-black outline-none focus:ring-2 focus:ring-blue-200" /> {!isEmailValid && email.length > 0 && (

Введите корректный email

)}
{/* Пароль */}
setPassword(e.target.value)} className="w-full rounded-full bg-white px-4 py-2 text-sm font-montserrat text-black outline-none focus:ring-2 focus:ring-blue-200" />
{/* Чекбокс */}

Подтверждаю, что я прочитал условия использования данного приложения

{/* Кнопка Регистрация */}
); }; export default RegPage;