mitra-interactions-sdk 1.0.60-beta.0 → 1.0.60-beta.2

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 CHANGED
@@ -259,6 +259,10 @@ function buildEndpoint(authUrl, path) {
259
259
  const base = authUrl.replace(/\/+$/, "");
260
260
  return `${base}/iam/api/v1/auth${path}`;
261
261
  }
262
+ function buildBffAuthEndpoint(gateway, path) {
263
+ const base = gateway.replace(/\/+$/, "");
264
+ return `${base}/auth${path}`;
265
+ }
262
266
  function resolveLoginOptions2(options) {
263
267
  var _a;
264
268
  const config = isConfigured() ? getConfig() : null;
@@ -302,6 +306,26 @@ async function postNewAuth(url, body) {
302
306
  }
303
307
  return response.json();
304
308
  }
309
+ async function postBffAuth(url, body) {
310
+ const fetchFn = getFetch();
311
+ const response = await fetchFn(url, {
312
+ method: "POST",
313
+ headers: { "Content-Type": "application/json" },
314
+ body: JSON.stringify(body)
315
+ });
316
+ if (!response.ok) {
317
+ const text2 = await response.text().catch(() => "");
318
+ let parsed = null;
319
+ try {
320
+ parsed = text2 ? JSON.parse(text2) : null;
321
+ } catch (e) {
322
+ }
323
+ const msg = (parsed == null ? void 0 : parsed.message) || (parsed == null ? void 0 : parsed.error) || `HTTP ${response.status}`;
324
+ throw { message: msg, status: response.status, details: parsed };
325
+ }
326
+ const text = await response.text().catch(() => "");
327
+ return text ? JSON.parse(text) : null;
328
+ }
305
329
  function normalizeResponse(tokens, authUrl) {
306
330
  return {
307
331
  token: tokens.accessToken,
@@ -317,12 +341,13 @@ var OAUTH_RESULT_TYPE = "mitra-oauth-result";
317
341
  var POPUP_W = 480;
318
342
  var POPUP_H = 600;
319
343
  var POPUP_TIMEOUT_MS = 5 * 60 * 1e3;
320
- var PASSTHROUGH_PATH = "/sdk-auth/oauth";
321
- function buildPassthroughBaseUrl(baseURL) {
322
- return baseURL.replace(/\/+$/, "") + PASSTHROUGH_PATH;
344
+ var SSO_AUTH_PAGE_URL = "https://d2by6yrxhbu2rl.cloudfront.net/sdk-auth.html";
345
+ async function fetchSsoAuthPageUrl(_baseURL) {
346
+ return SSO_AUTH_PAGE_URL;
323
347
  }
324
- function buildStartUrl(baseURL, provider, state, appId) {
325
- return buildPassthroughBaseUrl(baseURL) + "?" + new URLSearchParams({ provider, state, appId }).toString();
348
+ function buildStartUrl(authPageUrl, provider, state, appId, apiUrl) {
349
+ const sep = authPageUrl.includes("?") ? "&" : "?";
350
+ return authPageUrl + sep + new URLSearchParams({ provider, state, appId, apiUrl }).toString();
326
351
  }
327
352
  function getOriginFromUrl2(url) {
328
353
  try {
@@ -408,10 +433,11 @@ async function loginWithProvider(provider, options) {
408
433
  const { authUrl, projectId } = resolveLoginOptions2(options);
409
434
  const state = generateState();
410
435
  const appId = String(projectId);
411
- const expectedOrigin = getOriginFromUrl2(authUrl);
412
- const startUrl = buildStartUrl(authUrl, provider, state, appId);
413
- const redirectUri = buildPassthroughBaseUrl(authUrl);
414
- const { code } = await openOAuthPopup(startUrl, expectedOrigin, state);
436
+ const authPageUrl = await fetchSsoAuthPageUrl(authUrl);
437
+ const pageOrigin = getOriginFromUrl2(authPageUrl);
438
+ const redirectUri = authPageUrl;
439
+ const startUrl = buildStartUrl(authPageUrl, provider, state, appId, authUrl);
440
+ const { code } = await openOAuthPopup(startUrl, pageOrigin, state);
415
441
  const tokens = await postNewAuth(buildEndpoint(authUrl, `/${provider}`), {
416
442
  code,
417
443
  redirectUri,
@@ -442,25 +468,27 @@ async function loginMitra2(method, options) {
442
468
  }
443
469
  async function emailLoginMitra2(options) {
444
470
  const { authUrl, projectId } = resolveLoginOptions2(options);
445
- const tokens = await postNewAuth(buildEndpoint(authUrl, "/login"), {
471
+ const data = await postBffAuth(buildBffAuthEndpoint(authUrl, "/email/login"), {
472
+ projectId: String(projectId),
446
473
  email: options.email,
447
- password: options.password,
448
- appId: String(projectId)
474
+ password: options.password
449
475
  });
450
- const response = normalizeResponse(tokens, authUrl);
476
+ const response = {
477
+ token: data.token,
478
+ baseURL: data.baseURL || authUrl,
479
+ ...data.integrationURL ? { integrationURL: data.integrationURL } : {}
480
+ };
451
481
  autoConfigureFromLogin2(response, authUrl, projectId);
452
482
  return response;
453
483
  }
454
484
  async function emailSignupMitra2(options) {
455
485
  const { authUrl, projectId } = resolveLoginOptions2(options);
456
- const tokens = await postNewAuth(buildEndpoint(authUrl, "/register"), {
486
+ await postBffAuth(buildBffAuthEndpoint(authUrl, "/email/signup"), {
487
+ projectId: String(projectId),
457
488
  name: options.name,
458
489
  email: options.email,
459
- password: options.password,
460
- appId: String(projectId)
490
+ password: options.password
461
491
  });
462
- const response = normalizeResponse(tokens, authUrl);
463
- autoConfigureFromLogin2(response, authUrl, projectId);
464
492
  }
465
493
  async function emailVerifyCodeMitra2(_options) {
466
494
  throw new Error(NOT_SUPPORTED_VERIFY);