Update project files
This commit is contained in:
+18
-7
@@ -1,20 +1,31 @@
|
||||
const API = location.origin;
|
||||
|
||||
async function doLogin() {
|
||||
const pwd = document.getElementById('pwdInput').value;
|
||||
if (!pwd) return;
|
||||
const username = document.getElementById('userInput').value.trim();
|
||||
const password = document.getElementById('pwdInput').value;
|
||||
if (!username || !password) return;
|
||||
const err = document.getElementById('loginErr');
|
||||
err.style.display = 'none';
|
||||
try {
|
||||
const res = await fetch(`${API}/api/stats`, { headers: { 'X-Admin-Token': pwd } });
|
||||
if (res.ok) {
|
||||
sessionStorage.setItem('adminToken', pwd);
|
||||
const res = await fetch(`${API}/api/auth/login`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username, password }),
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (res.ok && data.token) {
|
||||
sessionStorage.setItem('authToken', data.token);
|
||||
location.href = 'dashboard.html';
|
||||
} else {
|
||||
document.getElementById('loginErr').style.display = 'block';
|
||||
err.textContent = data.error || 'Неверный логин или пароль';
|
||||
err.style.display = 'block';
|
||||
}
|
||||
} catch {
|
||||
document.getElementById('loginErr').style.display = 'block';
|
||||
err.textContent = 'Ошибка соединения';
|
||||
err.style.display = 'block';
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById('pwdInput').addEventListener('keydown', e => { if (e.key === 'Enter') doLogin(); });
|
||||
document.getElementById('userInput').addEventListener('keydown', e => { if (e.key === 'Enter') doLogin(); });
|
||||
document.querySelector('.login button').addEventListener('click', doLogin);
|
||||
Reference in New Issue
Block a user