fix progress auth

Signed-off-by: vladmandic <mandic00@live.com>
This commit is contained in:
vladmandic
2025-12-02 10:38:17 +01:00
parent 7aa1bfdc70
commit 175201a72f
8 changed files with 33 additions and 18 deletions
+9 -4
View File
@@ -1,8 +1,8 @@
let user = null;
let token = null;
async function authFetch(url, options = {}) {
if (!token) {
async function getToken() {
if (!token || !user) {
const res = await fetch(`${window.subpath}/token`);
if (res.ok) {
const data = await res.json();
@@ -10,9 +10,14 @@ async function authFetch(url, options = {}) {
token = data.token;
}
}
if (user && token) {
return { user, token };
}
async function authFetch(url, options = {}) {
const { localUser, localToken } = await getToken();
if (localUser && localToken) {
if (!options.headers) options.headers = {};
const encoded = btoa(`${user}:${token}`);
const encoded = btoa(`${localUser}:${localToken}`);
options.headers.Authorization = `Basic ${encoded}`;
}
const res = await fetch(url, options);
+7 -1
View File
@@ -35,7 +35,7 @@ const error = async (...msg) => {
// if (!txt.includes('asctime') && !txt.includes('xhr.')) xhrPost('/sdapi/v1/log', { error: txt }); // eslint-disable-line no-use-before-define
};
const xhrInternal = (xhrObj, data, handler = undefined, errorHandler = undefined, ignore = false, serverTimeout = opts.ui_request_timeout || 30000) => {
const xhrInternal = async (xhrObj, data, handler = undefined, errorHandler = undefined, ignore = false, serverTimeout = opts.ui_request_timeout || 30000) => {
const err = (msg) => {
if (!ignore) {
error(`${msg}: state=${xhrObj.readyState} status=${xhrObj.status} response=${xhrObj.responseText}`);
@@ -43,6 +43,12 @@ const xhrInternal = (xhrObj, data, handler = undefined, errorHandler = undefined
}
};
const { localUser, localToken } = await getToken();
if (localUser && localToken) {
const encoded = btoa(`${localUser}:${localToken}`);
xhrObj.setRequestHeader('Authorization', `Basic ${encoded}`);
}
xhrObj.setRequestHeader('Content-Type', 'application/json');
xhrObj.timeout = opts.ui_request_timeout || 30000;
xhrObj.ontimeout = () => err('xhr.ontimeout');