@sparkvault/sdk-js 2.1.1 → 2.1.3

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.
@@ -6522,6 +6522,13 @@ class IdentityRenderer {
6522
6522
  }
6523
6523
  handleSocialLogin(provider) {
6524
6524
  this.setState({ view: 'oauth-pending', provider });
6525
+ // When embedded, social must originate from the host: the OAuth state and
6526
+ // redirect_uri belong to the customer's top origin, not this iframe (whose
6527
+ // sessionStorage and URL the return would never match). Hand it up.
6528
+ if (this.options.onSocial) {
6529
+ this.options.onSocial(provider);
6530
+ return;
6531
+ }
6525
6532
  const state = generateState();
6526
6533
  const redirectUri = window.location.origin + window.location.pathname;
6527
6534
  sessionStorage.setItem('sv_oauth_state', state);
@@ -6904,8 +6911,21 @@ class IdentityRenderer {
6904
6911
  */
6905
6912
  /** Result envelope posted by the widget page. */
6906
6913
  const RESULT_TYPE = 'sparkvault-identity-result';
6907
- /** Top-window navigation request posted by the widget (social / OIDC). */
6914
+ /** Top-window navigation request posted by the widget (OIDC / generic). */
6908
6915
  const REDIRECT_TYPE = 'sparkvault-identity-redirect';
6916
+ /** Social-login start request posted by the widget. */
6917
+ const SOCIAL_TYPE = 'sparkvault-identity-social';
6918
+ /** Sent by the widget once its modal has mounted — proof the frame is alive. */
6919
+ const READY_TYPE = 'sparkvault-identity-ready';
6920
+ /**
6921
+ * How long to wait for the widget frame to signal it has mounted before giving
6922
+ * up and recovering in-page. The frame paints its own backdrop + spinner the
6923
+ * instant it loads and posts `ready` the moment its modal mounts; a frame that
6924
+ * is blocked from framing, 404s, or whose SDK never loads will never post — so
6925
+ * this bounds the "scroll locked, nothing on screen" failure to a few seconds
6926
+ * instead of leaving the host stuck until a manual refresh.
6927
+ */
6928
+ const FRAME_READY_TIMEOUT_MS = 8000;
6909
6929
  class WidgetEmbedder {
6910
6930
  constructor(config, options, callbacks) {
6911
6931
  this.config = config;
@@ -6913,6 +6933,9 @@ class WidgetEmbedder {
6913
6933
  this.callbacks = callbacks;
6914
6934
  this.iframe = null;
6915
6935
  this.settled = false;
6936
+ this.ready = false;
6937
+ this.failed = false;
6938
+ this.readyTimer = null;
6916
6939
  this.previousBodyOverflow = '';
6917
6940
  // Only ever trust messages from the Identity origin the SDK is configured
6918
6941
  // against — never the broader sparkvault.com family.
@@ -6937,14 +6960,20 @@ class WidgetEmbedder {
6937
6960
  'background:transparent',
6938
6961
  'z-index:2147483647',
6939
6962
  ].join(';');
6963
+ // A network-level load failure (DNS, connection reset) fires onerror; an
6964
+ // HTTP error body or a frame blocked from framing does not, which is what
6965
+ // the readiness timeout below covers.
6966
+ iframe.onerror = () => this.handleFrameUnavailable();
6940
6967
  this.iframe = iframe;
6941
6968
  // Lock host scroll while the modal (rendered inside the iframe) is open.
6942
6969
  this.previousBodyOverflow = document.body.style.overflow;
6943
6970
  document.body.style.overflow = 'hidden';
6944
6971
  window.addEventListener('message', this.boundHandleMessage);
6945
6972
  document.body.appendChild(iframe);
6973
+ this.readyTimer = setTimeout(() => this.handleFrameUnavailable(), FRAME_READY_TIMEOUT_MS);
6946
6974
  }
6947
6975
  close() {
6976
+ this.clearReadyTimer();
6948
6977
  if (this.iframe?.parentNode) {
6949
6978
  this.iframe.parentNode.removeChild(this.iframe);
6950
6979
  }
@@ -6952,6 +6981,62 @@ class WidgetEmbedder {
6952
6981
  window.removeEventListener('message', this.boundHandleMessage);
6953
6982
  document.body.style.overflow = this.previousBodyOverflow;
6954
6983
  }
6984
+ clearReadyTimer() {
6985
+ if (this.readyTimer !== null) {
6986
+ clearTimeout(this.readyTimer);
6987
+ this.readyTimer = null;
6988
+ }
6989
+ }
6990
+ /**
6991
+ * The widget frame never became interactive. Tear down, release the host
6992
+ * scroll lock, and hand control back so the flow can run in-page. Guarded so
6993
+ * a late onerror or timeout firing after a real result (or after the frame
6994
+ * has proven itself with `ready`) is a no-op.
6995
+ */
6996
+ handleFrameUnavailable() {
6997
+ if (this.settled || this.failed || this.ready)
6998
+ return;
6999
+ this.failed = true;
7000
+ this.close();
7001
+ this.callbacks.onFrameUnavailable();
7002
+ }
7003
+ /**
7004
+ * Begin a social-login redirect from the host. State and redirect_uri are
7005
+ * the customer's top origin so `consumeRedirectResult` validates them on
7006
+ * return. Mirrors the SDK's getSocialAuthUrl.
7007
+ */
7008
+ startSocial(provider) {
7009
+ // Provider comes from the trusted widget origin, but never build a URL from
7010
+ // an unexpected value.
7011
+ if (!/^[a-z0-9_]+$/i.test(provider))
7012
+ return;
7013
+ const state = generateState();
7014
+ try {
7015
+ sessionStorage.setItem('sv_oauth_state', state);
7016
+ }
7017
+ catch {
7018
+ // Storage disabled (private mode): proceed; the backend still binds state.
7019
+ }
7020
+ const url = new URL(`${this.config.identityBaseUrl}/${this.config.accountId}/social/${provider}`);
7021
+ url.searchParams.set('redirect_uri', window.location.origin + window.location.pathname);
7022
+ url.searchParams.set('state', state);
7023
+ const authContext = this.options.authContext;
7024
+ if (authContext?.authRequestId) {
7025
+ url.searchParams.set('auth_request_id', authContext.authRequestId);
7026
+ }
7027
+ if (authContext?.simpleMode) {
7028
+ const simpleMode = authContext.simpleMode.sessionId
7029
+ ? { session_id: authContext.simpleMode.sessionId }
7030
+ : {
7031
+ success_url: authContext.simpleMode.successUrl,
7032
+ failure_url: authContext.simpleMode.failureUrl,
7033
+ state: authContext.simpleMode.state,
7034
+ };
7035
+ url.searchParams.set('simple_mode', JSON.stringify(simpleMode));
7036
+ }
7037
+ this.close();
7038
+ window.location.href = url.toString();
7039
+ }
6955
7040
  buildUrl() {
6956
7041
  const url = new URL(`${this.config.identityBaseUrl}/${this.config.accountId}/widget`);
6957
7042
  url.searchParams.set('origin', window.location.origin);
@@ -6983,10 +7068,25 @@ class WidgetEmbedder {
6983
7068
  const data = event.data;
6984
7069
  if (!data || typeof data.type !== 'string')
6985
7070
  return;
6986
- // Social / OIDC redirect: the widget cannot navigate the top window from a
7071
+ // The frame mounted its modal — it is alive and owns the interaction now.
7072
+ // Cancel the readiness timeout so a slow-but-working frame is never torn
7073
+ // down and recovered in-page.
7074
+ if (data.type === READY_TYPE) {
7075
+ this.ready = true;
7076
+ this.clearReadyTimer();
7077
+ return;
7078
+ }
7079
+ // Social login: start it from the host so the OAuth state and redirect_uri
7080
+ // live on the customer's top origin (where the return is consumed).
7081
+ if (data.type === SOCIAL_TYPE && typeof data.provider === 'string') {
7082
+ this.startSocial(data.provider);
7083
+ return;
7084
+ }
7085
+ // OIDC / generic redirect: the widget cannot navigate the top window from a
6987
7086
  // cross-origin frame, so it asks us to. Navigating away tears down the
6988
7087
  // iframe; on return the host SDK consumes the redirect token.
6989
7088
  if (data.type === REDIRECT_TYPE && typeof data.url === 'string') {
7089
+ this.clearReadyTimer();
6990
7090
  window.location.href = data.url;
6991
7091
  return;
6992
7092
  }
@@ -7361,19 +7461,30 @@ class IdentityModule {
7361
7461
  rejectRef(error);
7362
7462
  },
7363
7463
  };
7464
+ const startLocalRender = (container) => {
7465
+ this.renderer = new IdentityRenderer(container, this.api, mergedOptions, hostCallbacks);
7466
+ this.renderer.start().catch((error) => {
7467
+ options.onError?.(error);
7468
+ rejectRef(error);
7469
+ });
7470
+ };
7364
7471
  if (!useLocal) {
7365
- this.embedder = new WidgetEmbedder(this.config, mergedOptions, hostCallbacks);
7472
+ this.embedder = new WidgetEmbedder(this.config, mergedOptions, {
7473
+ ...hostCallbacks,
7474
+ onFrameUnavailable: () => {
7475
+ // The hosted widget iframe could not load or mount (blocked from
7476
+ // framing, network failure, blank render, or its in-frame SDK never
7477
+ // loaded). Recover by running the verification flow in this document
7478
+ // — the same local path inline rendering uses — so login still works
7479
+ // and the host scroll lock the embedder set has already been released.
7480
+ this.embedder = null;
7481
+ startLocalRender(new ModalContainer());
7482
+ },
7483
+ });
7366
7484
  this.embedder.start();
7367
7485
  return settledPromise;
7368
7486
  }
7369
- const container = isInline
7370
- ? this.createInlineContainer(options.target)
7371
- : new ModalContainer();
7372
- this.renderer = new IdentityRenderer(container, this.api, mergedOptions, hostCallbacks);
7373
- this.renderer.start().catch((error) => {
7374
- options.onError?.(error);
7375
- rejectRef(error);
7376
- });
7487
+ startLocalRender(isInline ? this.createInlineContainer(options.target) : new ModalContainer());
7377
7488
  return settledPromise;
7378
7489
  }
7379
7490
  /**