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