Fix CORS preflight hang: GET токен через ?_t query (без Authorization)

This commit is contained in:
root 2026-08-20 21:44:59 +00:00
parent 9a9d8a7761
commit 4af525c7b8

View File

@ -43,9 +43,16 @@ const Store = {
async function api(path, opts = {}, _retry = 0) { async function api(path, opts = {}, _retry = 0) {
// rebuild opts on every retry (body may be already stringified) // rebuild opts on every retry (body may be already stringified)
const o = { ...opts, headers: { ...(opts.headers || {}) } }; const o = { ...opts, headers: { ...(opts.headers || {}) } };
if (Store.access && !o.headers.Authorization) { // для GET — передаём токен через query (избегаем CORS preflight на Authorization)
// для POST/PUT/DELETE — оставляем в headers
const isGet = !o.method || o.method === 'GET' || o.method === 'HEAD';
if (Store.access && !o.headers.Authorization && !isGet) {
o.headers.Authorization = `Bearer ${Store.access}`; o.headers.Authorization = `Bearer ${Store.access}`;
} }
if (Store.access && isGet) {
const sep = path.includes('?') ? '&' : '?';
path = path + sep + '_t=' + encodeURIComponent(Store.access);
}
if (o.body && typeof o.body === 'object' && !(o.body instanceof FormData)) { if (o.body && typeof o.body === 'object' && !(o.body instanceof FormData)) {
o.headers['Content-Type'] = 'application/json'; o.headers['Content-Type'] = 'application/json';
o.body = JSON.stringify(o.body); o.body = JSON.stringify(o.body);