mitra-interactions-sdk 1.0.60-beta.0 → 1.0.60-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +106 -58
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +106 -58
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -255,9 +255,9 @@ function getFetch() {
|
|
|
255
255
|
if (typeof globalThis.fetch === "function") return globalThis.fetch;
|
|
256
256
|
throw new Error("fetch n\xE3o dispon\xEDvel. Use Node.js 18+ ou instale node-fetch.");
|
|
257
257
|
}
|
|
258
|
-
function
|
|
259
|
-
const base =
|
|
260
|
-
return `${base}/
|
|
258
|
+
function buildBffAuthEndpoint(gateway, path) {
|
|
259
|
+
const base = gateway.replace(/\/+$/, "");
|
|
260
|
+
return `${base}/auth${path}`;
|
|
261
261
|
}
|
|
262
262
|
function resolveLoginOptions2(options) {
|
|
263
263
|
var _a;
|
|
@@ -283,7 +283,7 @@ function autoConfigureFromLogin2(response, authUrl, projectId) {
|
|
|
283
283
|
...response.integrationURL ? { integrationURL: response.integrationURL } : {}
|
|
284
284
|
});
|
|
285
285
|
}
|
|
286
|
-
async function
|
|
286
|
+
async function postBffAuth(url, body) {
|
|
287
287
|
const fetchFn = getFetch();
|
|
288
288
|
const response = await fetchFn(url, {
|
|
289
289
|
method: "POST",
|
|
@@ -291,16 +291,17 @@ async function postNewAuth(url, body) {
|
|
|
291
291
|
body: JSON.stringify(body)
|
|
292
292
|
});
|
|
293
293
|
if (!response.ok) {
|
|
294
|
-
const
|
|
294
|
+
const text2 = await response.text().catch(() => "");
|
|
295
295
|
let parsed = null;
|
|
296
296
|
try {
|
|
297
|
-
parsed =
|
|
297
|
+
parsed = text2 ? JSON.parse(text2) : null;
|
|
298
298
|
} catch (e) {
|
|
299
299
|
}
|
|
300
300
|
const msg = (parsed == null ? void 0 : parsed.message) || (parsed == null ? void 0 : parsed.error) || `HTTP ${response.status}`;
|
|
301
301
|
throw { message: msg, status: response.status, details: parsed };
|
|
302
302
|
}
|
|
303
|
-
|
|
303
|
+
const text = await response.text().catch(() => "");
|
|
304
|
+
return text ? JSON.parse(text) : null;
|
|
304
305
|
}
|
|
305
306
|
function normalizeResponse(tokens, authUrl) {
|
|
306
307
|
return {
|
|
@@ -317,12 +318,13 @@ var OAUTH_RESULT_TYPE = "mitra-oauth-result";
|
|
|
317
318
|
var POPUP_W = 480;
|
|
318
319
|
var POPUP_H = 600;
|
|
319
320
|
var POPUP_TIMEOUT_MS = 5 * 60 * 1e3;
|
|
320
|
-
var
|
|
321
|
-
function
|
|
322
|
-
return
|
|
321
|
+
var SSO_AUTH_PAGE_URL = "https://d2by6yrxhbu2rl.cloudfront.net/sdk-auth.html";
|
|
322
|
+
async function fetchSsoAuthPageUrl(_baseURL) {
|
|
323
|
+
return SSO_AUTH_PAGE_URL;
|
|
323
324
|
}
|
|
324
|
-
function buildStartUrl(
|
|
325
|
-
|
|
325
|
+
function buildStartUrl(authPageUrl, provider, state, appId, apiUrl) {
|
|
326
|
+
const sep = authPageUrl.includes("?") ? "&" : "?";
|
|
327
|
+
return authPageUrl + sep + new URLSearchParams({ provider, state, appId, apiUrl, origin: window.location.origin }).toString();
|
|
326
328
|
}
|
|
327
329
|
function getOriginFromUrl2(url) {
|
|
328
330
|
try {
|
|
@@ -379,11 +381,11 @@ function openOAuthPopup(providerUrl, expectedOrigin, expectedState) {
|
|
|
379
381
|
reject(new Error(data.error || "Erro na autentica\xE7\xE3o OAuth."));
|
|
380
382
|
return;
|
|
381
383
|
}
|
|
382
|
-
if (!data.
|
|
383
|
-
reject(new Error("Resposta OAuth sem
|
|
384
|
+
if (!data.token) {
|
|
385
|
+
reject(new Error("Resposta OAuth sem token."));
|
|
384
386
|
return;
|
|
385
387
|
}
|
|
386
|
-
resolve({
|
|
388
|
+
resolve({ token: data.token });
|
|
387
389
|
}
|
|
388
390
|
function cleanup() {
|
|
389
391
|
clearTimeout(timer);
|
|
@@ -408,16 +410,11 @@ async function loginWithProvider(provider, options) {
|
|
|
408
410
|
const { authUrl, projectId } = resolveLoginOptions2(options);
|
|
409
411
|
const state = generateState();
|
|
410
412
|
const appId = String(projectId);
|
|
411
|
-
const
|
|
412
|
-
const
|
|
413
|
-
const
|
|
414
|
-
const {
|
|
415
|
-
const
|
|
416
|
-
code,
|
|
417
|
-
redirectUri,
|
|
418
|
-
appId
|
|
419
|
-
});
|
|
420
|
-
const response = normalizeResponse(tokens, authUrl);
|
|
413
|
+
const authPageUrl = await fetchSsoAuthPageUrl(authUrl);
|
|
414
|
+
const pageOrigin = getOriginFromUrl2(authPageUrl);
|
|
415
|
+
const startUrl = buildStartUrl(authPageUrl, provider, state, appId, authUrl);
|
|
416
|
+
const { token } = await openOAuthPopup(startUrl, pageOrigin, state);
|
|
417
|
+
const response = normalizeResponse(token, authUrl);
|
|
421
418
|
autoConfigureFromLogin2(response, authUrl, projectId);
|
|
422
419
|
return response;
|
|
423
420
|
} finally {
|
|
@@ -442,25 +439,30 @@ async function loginMitra2(method, options) {
|
|
|
442
439
|
}
|
|
443
440
|
async function emailLoginMitra2(options) {
|
|
444
441
|
const { authUrl, projectId } = resolveLoginOptions2(options);
|
|
445
|
-
const
|
|
442
|
+
const data = await postBffAuth(buildBffAuthEndpoint(authUrl, "/email/login"), {
|
|
443
|
+
projectId: String(projectId),
|
|
446
444
|
email: options.email,
|
|
447
|
-
password: options.password
|
|
448
|
-
appId: String(projectId)
|
|
445
|
+
password: options.password
|
|
449
446
|
});
|
|
450
|
-
const response =
|
|
447
|
+
const response = {
|
|
448
|
+
token: data.token,
|
|
449
|
+
// baseURL = authUrl (o gateway configurado). O BFF não devolve baseURL/integrationURL;
|
|
450
|
+
// a base é sempre o gateway, resolvido no lado do SDK.
|
|
451
|
+
baseURL: authUrl,
|
|
452
|
+
// Guarda o refreshToken do login — é ele que vai no body do refresh depois.
|
|
453
|
+
...data.refreshToken ? { refreshToken: data.refreshToken } : {}
|
|
454
|
+
};
|
|
451
455
|
autoConfigureFromLogin2(response, authUrl, projectId);
|
|
452
456
|
return response;
|
|
453
457
|
}
|
|
454
458
|
async function emailSignupMitra2(options) {
|
|
455
459
|
const { authUrl, projectId } = resolveLoginOptions2(options);
|
|
456
|
-
|
|
460
|
+
await postBffAuth(buildBffAuthEndpoint(authUrl, "/email/signup"), {
|
|
461
|
+
projectId: String(projectId),
|
|
457
462
|
name: options.name,
|
|
458
463
|
email: options.email,
|
|
459
|
-
password: options.password
|
|
460
|
-
appId: String(projectId)
|
|
464
|
+
password: options.password
|
|
461
465
|
});
|
|
462
|
-
const response = normalizeResponse(tokens, authUrl);
|
|
463
|
-
autoConfigureFromLogin2(response, authUrl, projectId);
|
|
464
466
|
}
|
|
465
467
|
async function emailVerifyCodeMitra2(_options) {
|
|
466
468
|
throw new Error(NOT_SUPPORTED_VERIFY);
|
|
@@ -476,10 +478,15 @@ async function refreshTokenSilently2(authUrl, _projectId) {
|
|
|
476
478
|
const config = isConfigured() ? getConfig() : null;
|
|
477
479
|
const refreshToken = config == null ? void 0 : config.refreshToken;
|
|
478
480
|
if (!refreshToken) {
|
|
479
|
-
throw new Error("refreshToken n\xE3o dispon\xEDvel para renova\xE7\xE3o
|
|
481
|
+
throw new Error("refreshToken n\xE3o dispon\xEDvel para renova\xE7\xE3o. Fa\xE7a login novamente.");
|
|
480
482
|
}
|
|
481
|
-
const
|
|
482
|
-
return
|
|
483
|
+
const data = await postBffAuth(buildBffAuthEndpoint(authUrl, "/refresh-token"), { refreshToken });
|
|
484
|
+
return {
|
|
485
|
+
token: data.token,
|
|
486
|
+
// Refresh não muda a base — mantém o authUrl (gateway).
|
|
487
|
+
baseURL: authUrl,
|
|
488
|
+
...data.refreshToken ? { refreshToken: data.refreshToken } : {}
|
|
489
|
+
};
|
|
483
490
|
} finally {
|
|
484
491
|
_refreshPromise2 = null;
|
|
485
492
|
}
|
|
@@ -699,28 +706,30 @@ function buildUrl(endpoint, params) {
|
|
|
699
706
|
async function tryRefreshToken() {
|
|
700
707
|
var _a, _b;
|
|
701
708
|
const config = getConfig();
|
|
702
|
-
if (!config.token || config.projectId == null) return false;
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
709
|
+
if (!config.token || config.projectId == null || !config.baseURL) return false;
|
|
710
|
+
if (!isNewWorld()) {
|
|
711
|
+
try {
|
|
712
|
+
const baseURL = config.baseURL.replace(/\/+$/, "");
|
|
713
|
+
const resp = await getFetch3()(`${baseURL}/mitraspace/project/refreshedToken/${config.projectId}`, {
|
|
714
|
+
method: "GET",
|
|
715
|
+
headers: { "Authorization": formatToken(config.token) }
|
|
716
|
+
});
|
|
717
|
+
if (resp.ok) {
|
|
718
|
+
const data = await resp.json();
|
|
719
|
+
if (data.token) {
|
|
720
|
+
const newToken = `Bearer ${stripBearer(data.token)}`;
|
|
721
|
+
const session = {
|
|
722
|
+
token: newToken,
|
|
723
|
+
baseURL: ((_a = data.merge) == null ? void 0 : _a.backURL) || config.baseURL,
|
|
724
|
+
...((_b = data.merge) == null ? void 0 : _b.integrationURL) ? { integrationURL: data.merge.integrationURL } : {}
|
|
725
|
+
};
|
|
726
|
+
updateGlobalToken(session.token);
|
|
727
|
+
if (config.onTokenRefresh) config.onTokenRefresh(session);
|
|
728
|
+
return true;
|
|
729
|
+
}
|
|
721
730
|
}
|
|
731
|
+
} catch (e) {
|
|
722
732
|
}
|
|
723
|
-
} catch (e) {
|
|
724
733
|
}
|
|
725
734
|
if (!config.authUrl || typeof window === "undefined") return false;
|
|
726
735
|
try {
|
|
@@ -749,7 +758,8 @@ async function handleResponse(response) {
|
|
|
749
758
|
async function fetchWithRefresh(url, init) {
|
|
750
759
|
const fetchFn = getFetch3();
|
|
751
760
|
const response = await fetchFn(url, init);
|
|
752
|
-
|
|
761
|
+
const expiredStatus = isNewWorld() ? 401 : 403;
|
|
762
|
+
if (response.status === expiredStatus) {
|
|
753
763
|
const refreshed = await tryRefreshToken();
|
|
754
764
|
if (refreshed) {
|
|
755
765
|
const newHeaders = { ...buildHeaders() };
|
|
@@ -2345,6 +2355,37 @@ function stripBearer(token) {
|
|
|
2345
2355
|
}
|
|
2346
2356
|
return t;
|
|
2347
2357
|
}
|
|
2358
|
+
var SESSION_STORE_KEY = "mitra-session";
|
|
2359
|
+
function safeLocalStorage() {
|
|
2360
|
+
try {
|
|
2361
|
+
return typeof localStorage !== "undefined" ? localStorage : null;
|
|
2362
|
+
} catch (e) {
|
|
2363
|
+
return null;
|
|
2364
|
+
}
|
|
2365
|
+
}
|
|
2366
|
+
function persistRefreshToken(refreshToken) {
|
|
2367
|
+
const ls = safeLocalStorage();
|
|
2368
|
+
if (!ls) return;
|
|
2369
|
+
try {
|
|
2370
|
+
const raw = ls.getItem(SESSION_STORE_KEY);
|
|
2371
|
+
const obj = raw ? JSON.parse(raw) : {};
|
|
2372
|
+
obj.refreshToken = refreshToken;
|
|
2373
|
+
ls.setItem(SESSION_STORE_KEY, JSON.stringify(obj));
|
|
2374
|
+
} catch (e) {
|
|
2375
|
+
}
|
|
2376
|
+
}
|
|
2377
|
+
function readPersistedRefreshToken() {
|
|
2378
|
+
const ls = safeLocalStorage();
|
|
2379
|
+
if (!ls) return void 0;
|
|
2380
|
+
try {
|
|
2381
|
+
const raw = ls.getItem(SESSION_STORE_KEY);
|
|
2382
|
+
if (!raw) return void 0;
|
|
2383
|
+
const obj = JSON.parse(raw);
|
|
2384
|
+
return typeof (obj == null ? void 0 : obj.refreshToken) === "string" && obj.refreshToken ? obj.refreshToken : void 0;
|
|
2385
|
+
} catch (e) {
|
|
2386
|
+
return void 0;
|
|
2387
|
+
}
|
|
2388
|
+
}
|
|
2348
2389
|
function readInjectedEnv() {
|
|
2349
2390
|
if (typeof window === "undefined") return {};
|
|
2350
2391
|
const env = window.__mitraEnv;
|
|
@@ -2362,6 +2403,12 @@ function configureSdkMitra(config = {}) {
|
|
|
2362
2403
|
throw new Error("baseURL \xE9 obrigat\xF3rio. Passe em configureSdkMitra({ baseURL }), fa\xE7a login para receber via sdk-auth, ou rode em uma app publicada pelo Mitra (window.__mitraEnv).");
|
|
2363
2404
|
}
|
|
2364
2405
|
if (merged.token != null) merged.token = stripBearer(merged.token);
|
|
2406
|
+
if (!merged.refreshToken) {
|
|
2407
|
+
const persisted = readPersistedRefreshToken();
|
|
2408
|
+
if (persisted) merged.refreshToken = persisted;
|
|
2409
|
+
} else {
|
|
2410
|
+
persistRefreshToken(merged.refreshToken);
|
|
2411
|
+
}
|
|
2365
2412
|
globalConfig = {
|
|
2366
2413
|
...merged,
|
|
2367
2414
|
baseURL: merged.baseURL.replace(/\/+$/, "")
|
|
@@ -2388,6 +2435,7 @@ function updateGlobalToken(token, refreshToken) {
|
|
|
2388
2435
|
token: stripBearer(token),
|
|
2389
2436
|
...refreshToken !== void 0 ? { refreshToken } : {}
|
|
2390
2437
|
};
|
|
2438
|
+
if (refreshToken) persistRefreshToken(refreshToken);
|
|
2391
2439
|
}
|
|
2392
2440
|
}
|
|
2393
2441
|
function resolveProjectId2(projectId) {
|