$(document).ready(() => { const token = localStorage.getItem("access_token"); if (!token) { window.location.href = "/auth"; return; } loadProfile(); function loadProfile() { Promise.all([ Api.get("/api/auth/me"), Api.get("/api/auth/roles").catch(() => ({ roles: [] })), ]) .then(async ([user, rolesData]) => { document.title = `LiB - ${user.full_name || user.username}`; await renderProfileHeader(user); renderInfo(user); renderRoles(user.roles || [], rolesData.roles || []); $("#account-section, #roles-section").removeClass("hidden"); }) .catch((error) => { console.error(error); Utils.showToast("Ошибка загрузки профиля", "error"); }); } async function renderProfileHeader(user) { const avatarUrl = await Utils.getGravatarUrl(user.email); const displayName = Utils.escapeHtml(user.full_name || user.username); $("#profile-card").html(`
${user.is_verified ? '
' : ""}

${displayName}

@${Utils.escapeHtml(user.username)}

${user.is_active ? "Активен" : "Заблокирован"}
`); } function renderInfo(user) { const fields = [ { label: "ID пользователя", value: user.id }, { label: "Email", value: user.email }, { label: "Полное имя", value: user.full_name || "Не указано" }, ]; const html = fields .map( (f) => `
${f.label} ${Utils.escapeHtml(String(f.value))}
`, ) .join(""); $("#account-info").html(html); } function renderRoles(userRoles, allRoles) { const $container = $("#roles-container"); if (userRoles.length === 0) { $container.html('

Нет ролей

'); return; } const roleMap = {}; allRoles.forEach((r) => (roleMap[r.name] = r.description)); const html = userRoles .map( (role) => `
${Utils.escapeHtml(role)}
${Utils.escapeHtml(roleMap[role] || "")}
`, ) .join(""); $container.html(html); } $("#submit-password-btn").on("click", async function () { const $btn = $(this); const newPass = $("#new-password").val(); const confirm = $("#confirm-password").val(); if (newPass !== confirm) { Utils.showToast("Пароли не совпадают", "error"); return; } if (newPass.length < 4) { Utils.showToast("Пароль слишком короткий", "error"); return; } $btn.prop("disabled", true).text("Меняем..."); try { await Api.put("/api/auth/me", { password: newPass, }); Utils.showToast("Пароль успешно изменен", "success"); window.dispatchEvent(new CustomEvent("close-modal")); $("#change-password-form")[0].reset(); } catch (error) { console.error(error); Utils.showToast(error.message || "Ошибка смены пароля", "error"); } finally { $btn.prop("disabled", false).text("Сменить"); } }); });