fix xhr object auth

Signed-off-by: vladmandic <mandic00@live.com>
This commit is contained in:
vladmandic
2025-12-02 10:59:18 +01:00
parent 39bced0987
commit 44b461cdf7
3 changed files with 19 additions and 8 deletions
+11 -4
View File
@@ -8,18 +8,25 @@ async function getToken() {
const data = await res.json();
user = data.user;
token = data.token;
log('getToken', user);
}
}
return { user, token };
}
async function authFetch(url, options = {}) {
const { localUser, localToken } = await getToken();
if (localUser && localToken) {
await getToken();
if (user && token) {
if (!options.headers) options.headers = {};
const encoded = btoa(`${localUser}:${localToken}`);
const encoded = btoa(`${user}:${token}`);
options.headers.Authorization = `Basic ${encoded}`;
}
const res = await fetch(url, options);
let res;
try {
res = await fetch(url, options);
if (!res.ok) error('fetch', { status: res.status, url, user, token });
} catch (err) {
error('fetch', { status: res.status, url, user, token, error: err });
}
return res;
}
+3 -3
View File
@@ -43,9 +43,9 @@ const xhrInternal = async (xhrObj, data, handler = undefined, errorHandler = und
}
};
const { localUser, localToken } = await getToken();
if (localUser && localToken) {
const encoded = btoa(`${localUser}:${localToken}`);
const { user, token } = await getToken();
if (user && token) {
const encoded = btoa(`${user}:${token}`);
xhrObj.setRequestHeader('Authorization', `Basic ${encoded}`);
}