mirror of
https://github.com/vladmandic/automatic
synced 2026-09-06 13:00:44 +02:00
@@ -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);
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user