Files
WhatIDo/public/js/login.js
T

50 lines
1.9 KiB
JavaScript

const API = location.origin;
async function doLogin() {
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/auth/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password, website: document.getElementById('hpWebsite').value }),
});
const data = await res.json().catch(() => ({}));
if (res.ok && data.token) {
sessionStorage.setItem('authToken', data.token);
location.href = 'dashboard.html';
} else {
err.textContent = data.error || 'Неверный логин или пароль';
err.style.display = 'block';
}
} catch {
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);
(async () => {
try {
const res = await fetch(`${API}/api/public-settings`);
if (!res.ok) return;
const s = await res.json();
const title = document.getElementById('loginTitle');
if (title && (s.system_name || '').trim()) title.textContent = s.system_name;
const logo = document.getElementById('loginLogo');
if (logo && s.system_logo) {
const img = document.createElement('img');
img.src = s.system_logo;
img.alt = '';
logo.appendChild(img);
logo.style.display = 'flex';
}
document.title = title ? title.textContent + ' — Вход' : document.title;
} catch { /* ignore */ }
})();