31 lines
1.3 KiB
JavaScript
31 lines
1.3 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); |