mitra-interactions-sdk 1.0.60-beta.1 → 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 +101 -67
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +101 -67
- 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,25 +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
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
method: "GET",
|
|
324
|
-
headers: { accept: "application/json" }
|
|
325
|
-
});
|
|
326
|
-
if (!response.ok) {
|
|
327
|
-
throw new Error(`SSO discovery falhou (HTTP ${response.status}).`);
|
|
328
|
-
}
|
|
329
|
-
const data = await response.json().catch(() => null);
|
|
330
|
-
const url = data && typeof data.authPageUrl === "string" ? data.authPageUrl : "";
|
|
331
|
-
if (!url) {
|
|
332
|
-
throw new Error("SSO discovery n\xE3o retornou authPageUrl.");
|
|
333
|
-
}
|
|
334
|
-
return url;
|
|
321
|
+
var SSO_AUTH_PAGE_URL = "https://d2by6yrxhbu2rl.cloudfront.net/sdk-auth.html";
|
|
322
|
+
async function fetchSsoAuthPageUrl(_baseURL) {
|
|
323
|
+
return SSO_AUTH_PAGE_URL;
|
|
335
324
|
}
|
|
336
325
|
function buildStartUrl(authPageUrl, provider, state, appId, apiUrl) {
|
|
337
326
|
const sep = authPageUrl.includes("?") ? "&" : "?";
|
|
338
|
-
return authPageUrl + sep + new URLSearchParams({ provider, state, appId, apiUrl }).toString();
|
|
327
|
+
return authPageUrl + sep + new URLSearchParams({ provider, state, appId, apiUrl, origin: window.location.origin }).toString();
|
|
339
328
|
}
|
|
340
329
|
function getOriginFromUrl2(url) {
|
|
341
330
|
try {
|
|
@@ -392,11 +381,11 @@ function openOAuthPopup(providerUrl, expectedOrigin, expectedState) {
|
|
|
392
381
|
reject(new Error(data.error || "Erro na autentica\xE7\xE3o OAuth."));
|
|
393
382
|
return;
|
|
394
383
|
}
|
|
395
|
-
if (!data.
|
|
396
|
-
reject(new Error("Resposta OAuth sem
|
|
384
|
+
if (!data.token) {
|
|
385
|
+
reject(new Error("Resposta OAuth sem token."));
|
|
397
386
|
return;
|
|
398
387
|
}
|
|
399
|
-
resolve({
|
|
388
|
+
resolve({ token: data.token });
|
|
400
389
|
}
|
|
401
390
|
function cleanup() {
|
|
402
391
|
clearTimeout(timer);
|
|
@@ -423,15 +412,9 @@ async function loginWithProvider(provider, options) {
|
|
|
423
412
|
const appId = String(projectId);
|
|
424
413
|
const authPageUrl = await fetchSsoAuthPageUrl(authUrl);
|
|
425
414
|
const pageOrigin = getOriginFromUrl2(authPageUrl);
|
|
426
|
-
const redirectUri = authPageUrl;
|
|
427
415
|
const startUrl = buildStartUrl(authPageUrl, provider, state, appId, authUrl);
|
|
428
|
-
const {
|
|
429
|
-
const
|
|
430
|
-
code,
|
|
431
|
-
redirectUri,
|
|
432
|
-
appId
|
|
433
|
-
});
|
|
434
|
-
const response = normalizeResponse(tokens, authUrl);
|
|
416
|
+
const { token } = await openOAuthPopup(startUrl, pageOrigin, state);
|
|
417
|
+
const response = normalizeResponse(token, authUrl);
|
|
435
418
|
autoConfigureFromLogin2(response, authUrl, projectId);
|
|
436
419
|
return response;
|
|
437
420
|
} finally {
|
|
@@ -456,25 +439,30 @@ async function loginMitra2(method, options) {
|
|
|
456
439
|
}
|
|
457
440
|
async function emailLoginMitra2(options) {
|
|
458
441
|
const { authUrl, projectId } = resolveLoginOptions2(options);
|
|
459
|
-
const
|
|
442
|
+
const data = await postBffAuth(buildBffAuthEndpoint(authUrl, "/email/login"), {
|
|
443
|
+
projectId: String(projectId),
|
|
460
444
|
email: options.email,
|
|
461
|
-
password: options.password
|
|
462
|
-
appId: String(projectId)
|
|
445
|
+
password: options.password
|
|
463
446
|
});
|
|
464
|
-
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
|
+
};
|
|
465
455
|
autoConfigureFromLogin2(response, authUrl, projectId);
|
|
466
456
|
return response;
|
|
467
457
|
}
|
|
468
458
|
async function emailSignupMitra2(options) {
|
|
469
459
|
const { authUrl, projectId } = resolveLoginOptions2(options);
|
|
470
|
-
|
|
460
|
+
await postBffAuth(buildBffAuthEndpoint(authUrl, "/email/signup"), {
|
|
461
|
+
projectId: String(projectId),
|
|
471
462
|
name: options.name,
|
|
472
463
|
email: options.email,
|
|
473
|
-
password: options.password
|
|
474
|
-
appId: String(projectId)
|
|
464
|
+
password: options.password
|
|
475
465
|
});
|
|
476
|
-
const response = normalizeResponse(tokens, authUrl);
|
|
477
|
-
autoConfigureFromLogin2(response, authUrl, projectId);
|
|
478
466
|
}
|
|
479
467
|
async function emailVerifyCodeMitra2(_options) {
|
|
480
468
|
throw new Error(NOT_SUPPORTED_VERIFY);
|
|
@@ -490,10 +478,15 @@ async function refreshTokenSilently2(authUrl, _projectId) {
|
|
|
490
478
|
const config = isConfigured() ? getConfig() : null;
|
|
491
479
|
const refreshToken = config == null ? void 0 : config.refreshToken;
|
|
492
480
|
if (!refreshToken) {
|
|
493
|
-
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.");
|
|
494
482
|
}
|
|
495
|
-
const
|
|
496
|
-
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
|
+
};
|
|
497
490
|
} finally {
|
|
498
491
|
_refreshPromise2 = null;
|
|
499
492
|
}
|
|
@@ -713,28 +706,30 @@ function buildUrl(endpoint, params) {
|
|
|
713
706
|
async function tryRefreshToken() {
|
|
714
707
|
var _a, _b;
|
|
715
708
|
const config = getConfig();
|
|
716
|
-
if (!config.token || config.projectId == null) return false;
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
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
|
+
}
|
|
735
730
|
}
|
|
731
|
+
} catch (e) {
|
|
736
732
|
}
|
|
737
|
-
} catch (e) {
|
|
738
733
|
}
|
|
739
734
|
if (!config.authUrl || typeof window === "undefined") return false;
|
|
740
735
|
try {
|
|
@@ -763,7 +758,8 @@ async function handleResponse(response) {
|
|
|
763
758
|
async function fetchWithRefresh(url, init) {
|
|
764
759
|
const fetchFn = getFetch3();
|
|
765
760
|
const response = await fetchFn(url, init);
|
|
766
|
-
|
|
761
|
+
const expiredStatus = isNewWorld() ? 401 : 403;
|
|
762
|
+
if (response.status === expiredStatus) {
|
|
767
763
|
const refreshed = await tryRefreshToken();
|
|
768
764
|
if (refreshed) {
|
|
769
765
|
const newHeaders = { ...buildHeaders() };
|
|
@@ -2359,6 +2355,37 @@ function stripBearer(token) {
|
|
|
2359
2355
|
}
|
|
2360
2356
|
return t;
|
|
2361
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
|
+
}
|
|
2362
2389
|
function readInjectedEnv() {
|
|
2363
2390
|
if (typeof window === "undefined") return {};
|
|
2364
2391
|
const env = window.__mitraEnv;
|
|
@@ -2376,6 +2403,12 @@ function configureSdkMitra(config = {}) {
|
|
|
2376
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).");
|
|
2377
2404
|
}
|
|
2378
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
|
+
}
|
|
2379
2412
|
globalConfig = {
|
|
2380
2413
|
...merged,
|
|
2381
2414
|
baseURL: merged.baseURL.replace(/\/+$/, "")
|
|
@@ -2402,6 +2435,7 @@ function updateGlobalToken(token, refreshToken) {
|
|
|
2402
2435
|
token: stripBearer(token),
|
|
2403
2436
|
...refreshToken !== void 0 ? { refreshToken } : {}
|
|
2404
2437
|
};
|
|
2438
|
+
if (refreshToken) persistRefreshToken(refreshToken);
|
|
2405
2439
|
}
|
|
2406
2440
|
}
|
|
2407
2441
|
function resolveProjectId2(projectId) {
|