59 lines
2.1 KiB
JavaScript
59 lines
2.1 KiB
JavaScript
// login.js
|
|
|
|
document.getElementById('loginForm').addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
|
|
const username = document.getElementById('username').value;
|
|
const password = document.getElementById('password').value;
|
|
const loginBtn = document.getElementById('loginBtn');
|
|
|
|
// UI Feedback
|
|
loginBtn.classList.add('btn-disabled');
|
|
loginBtn.innerHTML = '<i class="fas fa-sync-alt fa-spin"></i> Авторизация...';
|
|
|
|
try {
|
|
const response = await fetch('/api/v1/auth/login', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ username, password })
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
// Сохраняем токен
|
|
localStorage.setItem('admin_token', data.data.token);
|
|
|
|
showStatus('Успешно! Вход в систему...', 'success');
|
|
setTimeout(() => {
|
|
window.location.href = '/admin';
|
|
}, 800);
|
|
} else {
|
|
showStatus(data.error || 'Ошибка входа', 'error');
|
|
resetButton(loginBtn);
|
|
}
|
|
} catch (err) {
|
|
showStatus('Ошибка сети или сервера', 'error');
|
|
resetButton(loginBtn);
|
|
}
|
|
});
|
|
|
|
function resetButton(btn) {
|
|
btn.classList.remove('btn-disabled');
|
|
btn.innerHTML = '<i class="fas fa-sign-in-alt"></i> Войти в панель';
|
|
}
|
|
|
|
function showStatus(message, type) {
|
|
const notification = document.getElementById('notification');
|
|
notification.className = 'notification';
|
|
if (type === 'error') {
|
|
notification.classList.add('notification-error');
|
|
notification.innerHTML = `<h3><i class="fas fa-exclamation-circle"></i> Ошибка:</h3><div class="notification-message">${message}</div>`;
|
|
} else {
|
|
notification.classList.add('notification-success');
|
|
notification.innerHTML = `<h3><i class="fas fa-check-circle"></i> Успешно:</h3><div class="notification-message">${message}</div>`;
|
|
}
|
|
notification.style.display = 'block';
|
|
notification.style.opacity = '1';
|
|
}
|