51 lines
1.6 KiB
JavaScript
51 lines
1.6 KiB
JavaScript
// Проверяем наличие ошибки в URL
|
||
const urlParams = new URLSearchParams(window.location.search);
|
||
if (urlParams.get("error") === "invalid_password") {
|
||
document.getElementById("errorMessage").style.display = "block";
|
||
document.getElementById("errorMessage").textContent = "Неверный пароль!";
|
||
}
|
||
|
||
// Обработка формы входа
|
||
const loginForm = document.getElementById("loginForm");
|
||
const errorMessage = document.getElementById("errorMessage");
|
||
|
||
if (loginForm) {
|
||
loginForm.addEventListener("submit", async (e) => {
|
||
e.preventDefault();
|
||
|
||
const username = document.getElementById("username").value.trim();
|
||
const password = document.getElementById("password").value;
|
||
|
||
if (!username || !password) {
|
||
errorMessage.textContent = "Логин и пароль обязательны";
|
||
errorMessage.style.display = "block";
|
||
return;
|
||
}
|
||
|
||
try {
|
||
const response = await fetch("/api/login", {
|
||
method: "POST",
|
||
headers: {
|
||
"Content-Type": "application/json",
|
||
},
|
||
body: JSON.stringify({ username, password }),
|
||
});
|
||
|
||
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";
|
||
}
|
||
});
|
||
}
|