fix: expose dashboard login shell

This commit is contained in:
Курнат Андрей
2026-07-19 22:20:14 +03:00
parent 0ec64645b6
commit 393454c9e0
4 changed files with 39 additions and 16 deletions
+18 -7
View File
@@ -2,7 +2,7 @@
const state = {
snapshot: null,
token: "",
authorization: "",
loading: false,
timer: null,
marketFilter: "",
@@ -32,7 +32,7 @@ document.addEventListener("DOMContentLoaded", () => {
bindNavigation();
bindControls();
selectPage(location.hash.slice(1) || "overview", false);
loadSnapshot();
showAuthDialog();
});
function bindNavigation() {
@@ -76,17 +76,26 @@ function bindControls() {
});
$("#authForm").addEventListener("submit", async (event) => {
event.preventDefault();
state.token = $("#tokenInput").value.trim();
const username = $("#usernameInput").value.trim();
const password = $("#passwordInput").value;
setText("authError", "");
if (!state.token) return;
if (!username || !password) return;
state.authorization = basicAuthorization(username, password);
await loadSnapshot(true, true);
});
}
function basicAuthorization(username, password) {
const bytes = new TextEncoder().encode(`${username}:${password}`);
let binary = "";
bytes.forEach((byte) => { binary += String.fromCharCode(byte); });
return `Basic ${btoa(binary)}`;
}
async function api(path, options = {}) {
const headers = { Accept: "application/json", ...(options.headers || {}) };
if (options.body) headers["Content-Type"] = "application/json";
if (state.token) headers["X-TradeBot-Token"] = state.token;
if (state.authorization) headers.Authorization = state.authorization;
const response = await fetch(path, {
...options,
headers,
@@ -115,11 +124,13 @@ async function loadSnapshot(manual = false, fromAuth = false) {
render(snapshot);
setOffline(false);
if ($("#authDialog").open) $("#authDialog").close();
$("#passwordInput").value = "";
setText("authError", "");
scheduleRefresh(10000);
} catch (error) {
if (error instanceof AuthRequiredError) {
if (fromAuth) setText("authError", "Токен не принят сервером.");
if (fromAuth) setText("authError", "Неверный логин или пароль.");
state.authorization = "";
showAuthDialog();
} else {
setOffline(true, error.message);
@@ -140,7 +151,7 @@ function showAuthDialog() {
const dialog = $("#authDialog");
if (!dialog.open) dialog.showModal();
setText("syncLabel", "Нужна авторизация");
setTimeout(() => $("#tokenInput").focus(), 50);
setTimeout(() => $("#usernameInput").focus(), 50);
}
function setOffline(offline, message = "") {