99 lines
3.0 KiB
HTML
99 lines
3.0 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="ru">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Авторизация</title>
|
|
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
|
|
<style>
|
|
body {
|
|
height: 100vh;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
background: #f8f9fa;
|
|
}
|
|
|
|
.login-card {
|
|
width: 100%;
|
|
max-width: 360px;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div class="card login-card shadow">
|
|
<div class="card-body">
|
|
<h4 class="text-center mb-4">Вход</h4>
|
|
|
|
<!-- ВАЖНО: form -->
|
|
<form id="loginForm">
|
|
<div class="mb-3">
|
|
<input type="password" class="form-control" id="password" placeholder="Введите пароль" autofocus>
|
|
</div>
|
|
|
|
<!-- Кнопка может остаться, но теперь не обязательна -->
|
|
<button type="submit" class="btn btn-success w-100">
|
|
Войти
|
|
</button>
|
|
</form>
|
|
|
|
<div id="error" class="alert alert-danger mt-3 d-none"></div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
document.getElementById("loginForm").addEventListener("submit", function (e) {
|
|
e.preventDefault(); // НЕ перезагружать страницу
|
|
login();
|
|
});
|
|
|
|
async function login() {
|
|
const password = document.getElementById("password").value;
|
|
const errorBox = document.getElementById("error");
|
|
|
|
errorBox.classList.add("d-none");
|
|
|
|
if (!password) {
|
|
errorBox.textContent = "Введите пароль";
|
|
errorBox.classList.remove("d-none");
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const response = await fetch("/login", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: JSON.stringify({ password })
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (data.status === "ok" && data.token) {
|
|
setCookie("auth_token", data.token, 365);
|
|
window.location.href = "/";
|
|
} else {
|
|
errorBox.textContent = data.errorMessage || "Неверный пароль";
|
|
errorBox.classList.remove("d-none");
|
|
}
|
|
} catch (e) {
|
|
errorBox.textContent = "Ошибка соединения";
|
|
errorBox.classList.remove("d-none");
|
|
}
|
|
}
|
|
|
|
function setCookie(name, value, days) {
|
|
const date = new Date();
|
|
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
|
|
document.cookie = `${name}=${value}; expires=${date.toUTCString()}; path=/`;
|
|
}
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html> |