- Реализовано хранение сессий в базе данных SQLite с помощью connect-sqlite3 - Добавлены API для проверки статуса аутентификации - Обновлены клиентские скрипты для управления состоянием аутентификации - Добавлены проверки аутентификации на страницах входа и профиля - Улучшено управление состоянием аутентификации в localStorage
71 lines
2.4 KiB
JavaScript
71 lines
2.4 KiB
JavaScript
// Проверяем, не авторизован ли уже пользователь
|
||
if (localStorage.getItem('isAuthenticated') === 'true') {
|
||
window.location.href = "/notes";
|
||
}
|
||
|
||
// Обработка формы регистрации
|
||
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) {
|
||
// Успешная регистрация - сохраняем состояние аутентификации
|
||
localStorage.setItem('isAuthenticated', 'true');
|
||
localStorage.setItem('username', username);
|
||
window.location.href = "/notes";
|
||
} else {
|
||
// Ошибка регистрации
|
||
errorMessage.textContent = data.error || "Ошибка регистрации";
|
||
errorMessage.style.display = "block";
|
||
}
|
||
} catch (err) {
|
||
console.error("Ошибка:", err);
|
||
errorMessage.textContent = "Ошибка соединения с сервером";
|
||
errorMessage.style.display = "block";
|
||
}
|
||
});
|
||
}
|