@@ -5,26 +5,6 @@
|
||||
const TOKEN_KEY = "compton.access_token";
|
||||
const ENTERPRISE_KEY = "compton.enterprise_id";
|
||||
const USER_KEY = "compton.user";
|
||||
const DEBUG_ENDPOINT = "http://127.0.0.1:7898/ingest/d209dffb-c63e-477b-8b08-a57520eaee9b";
|
||||
const SESSION = "785e22";
|
||||
|
||||
function debugLog(hypothesisId, message, data) {
|
||||
// #region agent log
|
||||
fetch(DEBUG_ENDPOINT, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", "X-Debug-Session-Id": SESSION },
|
||||
body: JSON.stringify({
|
||||
sessionId: SESSION,
|
||||
runId: "login-fix",
|
||||
hypothesisId,
|
||||
location: "wesp-orchestrator-auth-bridge.js",
|
||||
message,
|
||||
data,
|
||||
timestamp: Date.now(),
|
||||
}),
|
||||
}).catch(function () {});
|
||||
// #endregion
|
||||
}
|
||||
|
||||
function readJson(key) {
|
||||
try {
|
||||
@@ -42,20 +22,29 @@
|
||||
"";
|
||||
const user = readJson(USER_KEY);
|
||||
|
||||
let resolveReady;
|
||||
const ready = new Promise(function (resolve) {
|
||||
resolveReady = resolve;
|
||||
});
|
||||
|
||||
function markReady() {
|
||||
if (typeof resolveReady === "function") {
|
||||
resolveReady();
|
||||
resolveReady = null;
|
||||
}
|
||||
}
|
||||
|
||||
global.__WESP_ORCH__ = Object.assign({}, global.__WESP_ORCH__ || {}, {
|
||||
apiBase: "/api/v1",
|
||||
accessToken: token,
|
||||
enterpriseId: enterpriseId,
|
||||
userEmail: user && user.email ? user.email : "",
|
||||
ready: ready,
|
||||
});
|
||||
|
||||
debugLog("H1", "auth bridge init", {
|
||||
hasToken: Boolean(token),
|
||||
hasEnterprise: Boolean(enterpriseId),
|
||||
path: global.location.pathname,
|
||||
});
|
||||
|
||||
if (token && !enterpriseId) {
|
||||
if (enterpriseId || !token) {
|
||||
markReady();
|
||||
} else {
|
||||
fetch("/api/v1/enterprise/enterprises", {
|
||||
headers: { Authorization: "Bearer " + token },
|
||||
})
|
||||
@@ -65,16 +54,13 @@
|
||||
.then(function (rows) {
|
||||
const entId = Array.isArray(rows) && rows[0] && rows[0].id ? String(rows[0].id) : "";
|
||||
if (!entId) {
|
||||
debugLog("H1", "enterprise resolve failed", { count: Array.isArray(rows) ? rows.length : -1 });
|
||||
return;
|
||||
}
|
||||
global.localStorage.setItem(ENTERPRISE_KEY, entId);
|
||||
global.__WESP_ORCH__ = Object.assign({}, global.__WESP_ORCH__ || {}, { enterpriseId: entId });
|
||||
debugLog("H1", "enterprise resolved async", { enterpriseId: entId });
|
||||
})
|
||||
.catch(function () {
|
||||
debugLog("H1", "enterprise resolve error", {});
|
||||
});
|
||||
.catch(function () {})
|
||||
.finally(markReady);
|
||||
}
|
||||
|
||||
global.ComptonWespAuth = {
|
||||
|
||||
Reference in New Issue
Block a user