64 lines
2.1 KiB
JavaScript
64 lines
2.1 KiB
JavaScript
// Обработка формы регистрации
|
||
const registerForm = document.getElementById("registerForm");
|
||
const errorMessage = document.getElementById("errorMessage");
|
||
|
||
if (registerForm) {
|
||
registerForm.addEventListener("submit", async (e) => {
|
||
e.preventDefault();
|
||
|
||
const username = document.getElementById("username").value.trim();
|
||
const password = document.getElementById("password").value;
|
||
const confirmPassword = document.getElementById("confirmPassword").value;
|
||
|
||
// Клиентская валидация
|
||
if (!username || !password || !confirmPassword) {
|
||
errorMessage.textContent = "Все поля обязательны";
|
||
errorMessage.style.display = "block";
|
||
return;
|
||
}
|
||
|
||
if (username.length < 3) {
|
||
errorMessage.textContent = "Логин должен быть не менее 3 символов";
|
||
errorMessage.style.display = "block";
|
||
return;
|
||
}
|
||
|
||
if (password.length < 6) {
|
||
errorMessage.textContent = "Пароль должен быть не менее 6 символов";
|
||
errorMessage.style.display = "block";
|
||
return;
|
||
}
|
||
|
||
if (password !== confirmPassword) {
|
||
errorMessage.textContent = "Пароли не совпадают";
|
||
errorMessage.style.display = "block";
|
||
return;
|
||
}
|
||
|
||
try {
|
||
const response = await fetch("/api/register", {
|
||
method: "POST",
|
||
headers: {
|
||
"Content-Type": "application/json",
|
||
},
|
||
body: JSON.stringify({ username, password, confirmPassword }),
|
||
});
|
||
|
||
const data = await response.json();
|
||
|
||
if (response.ok) {
|
||
// Успешная регистрация
|
||
window.location.href = "/notes";
|
||
} else {
|
||
// Ошибка регистрации
|
||
errorMessage.textContent = data.error || "Ошибка регистрации";
|
||
errorMessage.style.display = "block";
|
||
}
|
||
} catch (err) {
|
||
console.error("Ошибка:", err);
|
||
errorMessage.textContent = "Ошибка соединения с сервером";
|
||
errorMessage.style.display = "block";
|
||
}
|
||
});
|
||
}
|