Files
minicrm/public/auth.js
2026-01-26 17:44:28 +05:00

97 lines
2.8 KiB
JavaScript

// auth.js - Аутентификация и авторизация
let currentUser = null;
async function checkAuth() {
try {
const response = await fetch('/api/user');
if (response.ok) {
const data = await response.json();
currentUser = data.user;
showMainInterface();
} else {
showLoginInterface();
}
} catch (error) {
showLoginInterface();
}
}
function showLoginInterface() {
document.getElementById('login-modal').style.display = 'block';
document.querySelector('.container').style.display = 'none';
}
function showMainInterface() {
document.getElementById('login-modal').style.display = 'none';
document.querySelector('.container').style.display = 'block';
let userInfo = `Вы вошли как: ${currentUser.name}`;
if (currentUser.auth_type === 'ldap') {
userInfo += ` (LDAP)`;
}
if (currentUser.groups && currentUser.groups.length > 0) {
userInfo += ` | Группы: ${currentUser.groups.join(', ')}`;
}
document.getElementById('current-user').textContent = userInfo;
document.getElementById('tasks-controls').style.display = 'block';
const showDeletedLabel = document.querySelector('.show-deleted-label');
if (showDeletedLabel) {
if (currentUser.role === 'admin') {
showDeletedLabel.style.display = 'flex';
} else {
showDeletedLabel.style.display = 'none';
}
}
loadUsers();
loadTasks();
loadActivityLogs();
showSection('tasks');
loadKanbanTasks();
showingTasksWithoutDate = false;
const btn = document.getElementById('tasks-no-date-btn');
if (btn) btn.classList.remove('active');
}
async function login(event) {
event.preventDefault();
const login = document.getElementById('login').value;
const password = document.getElementById('password').value;
try {
const response = await fetch('/api/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ login, password })
});
if (response.ok) {
const data = await response.json();
currentUser = data.user;
showMainInterface();
} else {
const error = await response.json();
alert(error.error || 'Ошибка входа');
}
} catch (error) {
console.error('Ошибка:', error);
alert('Ошибка подключения к серверу');
}
}
async function logout() {
try {
await fetch('/api/logout', { method: 'POST' });
currentUser = null;
showLoginInterface();
} catch (error) {
console.error('Ошибка выхода:', error);
}
}