export const validate = ( username: string, email: string, password: string, passwordConfirm: string, ) => { const newErrors: any = {}; if (!username) { newErrors.username = 'Введите никнейм'; } else if (username.length < 3) { newErrors.username = 'Минимум 3 символа.'; } else if (username.length > 32) { newErrors.username = 'Максимум 32 символа'; } if (!email) { newErrors.email = 'Введите email.'; } else { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(email)) newErrors.email = 'Некорректный email'; if (email.length > 254) newErrors.email = 'Слишком длинный email.'; } if (!password) { newErrors.password = 'Введите пароль.'; } else if (password.length < 8) { newErrors.password = 'Необходимо минимум 8 символов.'; } else { const hasLetter = /[A-Za-z]/.test(password); const hasNumber = /\d/.test(password); const hasSymbol = /[^\w\s]/.test(password); if (!(hasLetter && hasNumber && hasSymbol)) { newErrors.password = 'Попробуйте сочетание букв, цифр и символов.'; } } if (password !== passwordConfirm) { newErrors.passwordConfirm = 'пароли не совпадают.'; } return newErrors; }; export const registerUser = async ( username: string, email: string, password: string, ) => { try { const res = await fetch('http://localhost:8000/api/auth/register', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, email, password }), }); const data = await res.json(); if (!res.ok) { const { field, message } = data.detail; return { error: { [field]: message } }; } return { data }; } catch (err: any) { return { error: { general: err.message } }; } };