@sparkvault/sdk-js 1.26.0 → 1.27.1

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.
@@ -77,7 +77,7 @@ const DEFAULT_ALLOWED_DOWNLOAD_HOST_PATTERNS = [
77
77
  /\.cloudfront\.net$/i,
78
78
  ];
79
79
  const API_URL = 'https://api.sparkvault.com';
80
- const IDENTITY_URL = 'https://api.sparkvault.com/v1/apps/identity';
80
+ const IDENTITY_URL = 'https://api.sparkvault.com/v1/products/identity';
81
81
  function normalizeApiBaseUrl(url) {
82
82
  const trimmed = url.replace(/\/+$/, '');
83
83
  return trimmed.endsWith('/v1') ? trimmed.slice(0, -3) : trimmed;
@@ -152,6 +152,14 @@ function defaultIsRetryable(error) {
152
152
  }
153
153
  return false;
154
154
  }
155
+ /**
156
+ * Return a random unit interval value using Web Crypto.
157
+ */
158
+ function randomUnit() {
159
+ const value = new Uint32Array(1);
160
+ crypto.getRandomValues(value);
161
+ return value[0] / 0x100000000;
162
+ }
155
163
  /**
156
164
  * Calculate delay with exponential backoff and jitter
157
165
  */
@@ -160,7 +168,7 @@ function calculateDelay(attempt, baseDelayMs, maxDelayMs, jitterFactor) {
160
168
  const exponentialDelay = baseDelayMs * Math.pow(2, attempt);
161
169
  const cappedDelay = Math.min(exponentialDelay, maxDelayMs);
162
170
  // Add jitter: random value within jitter range
163
- const jitter = cappedDelay * jitterFactor * Math.random();
171
+ const jitter = cappedDelay * jitterFactor * randomUnit();
164
172
  return cappedDelay + jitter;
165
173
  }
166
174
  /**
@@ -894,7 +902,7 @@ function generateState() {
894
902
  /**
895
903
  * Identity API Client
896
904
  *
897
- * Handles all HTTP communication with the Identity App endpoints.
905
+ * Handles all HTTP communication with the Identity Product endpoints.
898
906
  * Single responsibility: API calls only.
899
907
  */
900
908
  class IdentityApi extends SparkVaultApi {
@@ -970,8 +978,6 @@ class IdentityApi extends SparkVaultApi {
970
978
  * (email: MX lookup; phone: E.164 format)
971
979
  * - has_passkey: whether any passkeys are registered for this identity
972
980
  * (cross-tenant — single global RP)
973
- * - email_valid: legacy alias for identity_valid, present for backward
974
- * compatibility with older SDK versions
975
981
  */
976
982
  async checkPasskey(identity, identityType = 'email') {
977
983
  const body = identityType === 'phone' ? { phone: identity } : { email: identity };
@@ -1022,8 +1028,10 @@ class IdentityApi extends SparkVaultApi {
1022
1028
  /**
1023
1029
  * Start passkey registration for an identity (email or phone).
1024
1030
  */
1025
- async startPasskeyRegister(identity, identityType = 'email') {
1026
- const body = identityType === 'phone' ? { phone: identity } : { email: identity };
1031
+ async startPasskeyRegister(identity, identityType = 'email', registrationToken) {
1032
+ const body = identityType === 'phone'
1033
+ ? { phone: identity, registration_token: registrationToken }
1034
+ : { email: identity, registration_token: registrationToken };
1027
1035
  // Backend returns { options: PublicKeyCredentialCreationOptions, session: {...} }
1028
1036
  // Extract and flatten to match PasskeyChallengeResponse
1029
1037
  const response = await this.request('POST', '/passkey/register', body);
@@ -1471,10 +1479,8 @@ class PasskeyHandler {
1471
1479
  try {
1472
1480
  const response = await this.api.checkPasskey(this.state.recipient, this.state.identityType);
1473
1481
  this.state.setPasskeyStatus(response.has_passkey);
1474
- // Prefer identity_valid; fall back to email_valid for older backends.
1475
- const valid = response.identity_valid ?? response.email_valid;
1476
1482
  return {
1477
- emailValid: valid,
1483
+ emailValid: response.identity_valid,
1478
1484
  hasPasskey: response.has_passkey,
1479
1485
  };
1480
1486
  }
@@ -1487,8 +1493,8 @@ class PasskeyHandler {
1487
1493
  /**
1488
1494
  * Register a new passkey for the user via popup
1489
1495
  */
1490
- async register() {
1491
- const result = await this.openPasskeyPopup('register');
1496
+ async register(registrationToken) {
1497
+ const result = await this.openPasskeyPopup('register', registrationToken);
1492
1498
  if (result.success) {
1493
1499
  // Update state - user now has a passkey
1494
1500
  this.state.setPasskeyStatus(true);
@@ -1504,7 +1510,7 @@ class PasskeyHandler {
1504
1510
  /**
1505
1511
  * Open passkey popup and wait for result
1506
1512
  */
1507
- async openPasskeyPopup(mode) {
1513
+ async openPasskeyPopup(mode, registrationToken) {
1508
1514
  return new Promise((resolve) => {
1509
1515
  // Build popup URL. Pass identity as `email` or `phone` (mutually
1510
1516
  // exclusive) so the popup can render and POST the correct field.
@@ -1527,7 +1533,10 @@ class PasskeyHandler {
1527
1533
  session_id: authContext.simpleMode.sessionId,
1528
1534
  }));
1529
1535
  }
1530
- const popupUrl = `${this.baseUrl}/${this.accountId}/passkey/popup?${params}`;
1536
+ const hash = mode === 'register' && registrationToken
1537
+ ? `#${new URLSearchParams({ registration_token: registrationToken }).toString()}`
1538
+ : '';
1539
+ const popupUrl = `${this.baseUrl}/${this.accountId}/passkey/popup?${params}${hash}`;
1531
1540
  // Calculate popup position (centered)
1532
1541
  const left = Math.round((window.screen.width - POPUP_WIDTH) / 2);
1533
1542
  const top = Math.round((window.screen.height - POPUP_HEIGHT) / 2);
@@ -1572,7 +1581,7 @@ class PasskeyHandler {
1572
1581
  });
1573
1582
  }
1574
1583
  else {
1575
- resolve(this.handlePopupError(data.error, data.message));
1584
+ resolve(this.handlePopupError(data.error, data.message, data.errorCode));
1576
1585
  }
1577
1586
  };
1578
1587
  window.addEventListener('message', handleMessage);
@@ -1623,7 +1632,7 @@ class PasskeyHandler {
1623
1632
  /**
1624
1633
  * Handle popup error response
1625
1634
  */
1626
- handlePopupError(error, message) {
1635
+ handlePopupError(error, message, errorCode) {
1627
1636
  if (error === 'cancelled') {
1628
1637
  return {
1629
1638
  success: false,
@@ -1631,6 +1640,14 @@ class PasskeyHandler {
1631
1640
  errorType: 'cancelled',
1632
1641
  };
1633
1642
  }
1643
+ if (errorCode === 'identity_locked' || error === 'identity_locked') {
1644
+ return {
1645
+ success: false,
1646
+ error: message || 'This SparkVault Identity is locked. Visit auth.sv to manage it.',
1647
+ errorType: 'unknown',
1648
+ errorCode: 'identity_locked',
1649
+ };
1650
+ }
1634
1651
  if (message?.includes('No passkey') || message?.includes('not found')) {
1635
1652
  return {
1636
1653
  success: false,
@@ -1642,6 +1659,7 @@ class PasskeyHandler {
1642
1659
  success: false,
1643
1660
  error: message || error || 'Passkey authentication failed',
1644
1661
  errorType: 'unknown',
1662
+ errorCode,
1645
1663
  };
1646
1664
  }
1647
1665
  }
@@ -1763,9 +1781,11 @@ class TotpHandler {
1763
1781
  }
1764
1782
  catch (error) {
1765
1783
  const message = error instanceof Error ? error.message : 'Verification failed';
1784
+ const errorCode = (error instanceof SparkVaultError) ? error.code : undefined;
1766
1785
  return {
1767
1786
  success: false,
1768
1787
  error: message,
1788
+ errorCode,
1769
1789
  };
1770
1790
  }
1771
1791
  }
@@ -2706,7 +2726,7 @@ function getStyles(options) {
2706
2726
 
2707
2727
  .sv-not-activated-badge-svg {
2708
2728
  display: block;
2709
- filter: drop-shadow(0 6px 14px rgba(245, 158, 11, 0.18));
2729
+ filter: drop-shadow(0 8px 18px rgba(15, 23, 42, 0.08));
2710
2730
  }
2711
2731
 
2712
2732
  .sv-not-activated-title {
@@ -4863,38 +4883,46 @@ class ErrorView {
4863
4883
  // Content section
4864
4884
  const contentSection = div();
4865
4885
  contentSection.className = 'sv-error-content';
4886
+ // A locked-down identity is a dedicated, non-error state: a specific
4887
+ // title, no debug error-code badge, and no "Try Again" (retrying the
4888
+ // login cannot clear a lockdown — only managing it at auth.sv can).
4889
+ const isLocked = this.props.code === 'identity_locked';
4866
4890
  const title = document.createElement('h3');
4867
4891
  title.className = 'sv-error-title';
4868
- title.textContent = 'Something went wrong';
4892
+ title.textContent = isLocked ? 'This SparkVault Identity is locked' : 'Something went wrong';
4869
4893
  const message = document.createElement('p');
4870
4894
  message.className = 'sv-error-message';
4871
4895
  message.textContent = this.props.message;
4872
- // Error code badge
4873
- const codeBadge = document.createElement('div');
4874
- codeBadge.className = 'sv-error-code-badge';
4875
- const codeLabel = document.createElement('span');
4876
- codeLabel.className = 'sv-error-code-label';
4877
- codeLabel.textContent = 'Error code';
4878
- const codeValue = document.createElement('code');
4879
- codeValue.className = 'sv-error-code-value';
4880
- codeValue.textContent = this.props.code;
4881
- codeBadge.appendChild(codeLabel);
4882
- codeBadge.appendChild(codeValue);
4883
4896
  contentSection.appendChild(title);
4884
4897
  contentSection.appendChild(message);
4885
- contentSection.appendChild(codeBadge);
4898
+ if (!isLocked) {
4899
+ // Error code badge — useful for debugging genuine errors only.
4900
+ const codeBadge = document.createElement('div');
4901
+ codeBadge.className = 'sv-error-code-badge';
4902
+ const codeLabel = document.createElement('span');
4903
+ codeLabel.className = 'sv-error-code-label';
4904
+ codeLabel.textContent = 'Error code';
4905
+ const codeValue = document.createElement('code');
4906
+ codeValue.className = 'sv-error-code-value';
4907
+ codeValue.textContent = this.props.code;
4908
+ codeBadge.appendChild(codeLabel);
4909
+ codeBadge.appendChild(codeValue);
4910
+ contentSection.appendChild(codeBadge);
4911
+ }
4886
4912
  // Action buttons with proper styling
4887
4913
  const buttonContainer = div();
4888
4914
  buttonContainer.className = 'sv-error-actions';
4889
- this.retryButton = document.createElement('button');
4890
- this.retryButton.className = 'sv-btn sv-btn-error-primary';
4891
- this.retryButton.textContent = 'Try Again';
4892
- this.retryButton.addEventListener('click', this.boundHandleRetry);
4915
+ if (!isLocked) {
4916
+ this.retryButton = document.createElement('button');
4917
+ this.retryButton.className = 'sv-btn sv-btn-error-primary';
4918
+ this.retryButton.textContent = 'Try Again';
4919
+ this.retryButton.addEventListener('click', this.boundHandleRetry);
4920
+ buttonContainer.appendChild(this.retryButton);
4921
+ }
4893
4922
  this.closeButton = document.createElement('button');
4894
4923
  this.closeButton.className = 'sv-btn sv-btn-error-secondary';
4895
4924
  this.closeButton.textContent = 'Close';
4896
4925
  this.closeButton.addEventListener('click', this.boundHandleClose);
4897
- buttonContainer.appendChild(this.retryButton);
4898
4926
  buttonContainer.appendChild(this.closeButton);
4899
4927
  container.appendChild(iconContainer);
4900
4928
  container.appendChild(contentSection);
@@ -5013,53 +5041,66 @@ class ServiceUnavailableView {
5013
5041
  }
5014
5042
 
5015
5043
  /**
5016
- * App Not Activated View
5044
+ * Product Not Configured View
5017
5045
  *
5018
- * Full-dialog terminal screen shown when the SparkVault Identity app has not
5019
- * been activated for the account that the JS SDK is configured against. The
5046
+ * Full-dialog terminal screen shown when the SparkVault Identity Product has
5047
+ * not been configured for the account that the JS SDK is configured against. The
5020
5048
  * audience is the website owner / developer — this is a misconfiguration
5021
5049
  * surface, not an end-user error — so the dialog drops customer branding and
5022
5050
  * presents itself as an unmistakable SparkVault notice.
5023
5051
  */
5024
- const ACTIVATION_URL = 'https://app.sparkvault.com/apps';
5052
+ const ACTIVATION_URL = 'https://app.sparkvault.com/products/identity';
5025
5053
  function createBadge() {
5026
5054
  const badge = div('sv-not-activated-badge');
5027
5055
  const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
5028
- svg.setAttribute('viewBox', '0 0 56 56');
5056
+ svg.setAttribute('viewBox', '0 0 64 64');
5029
5057
  svg.setAttribute('width', '72');
5030
5058
  svg.setAttribute('height', '72');
5031
5059
  svg.setAttribute('fill', 'none');
5060
+ svg.setAttribute('aria-hidden', 'true');
5032
5061
  svg.classList.add('sv-not-activated-badge-svg');
5033
- // Soft outer ring
5034
- const ring = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
5035
- ring.setAttribute('cx', '28');
5036
- ring.setAttribute('cy', '28');
5037
- ring.setAttribute('r', '27');
5038
- ring.setAttribute('fill', '#FFFBEB');
5039
- ring.setAttribute('stroke', '#FDE68A');
5040
- ring.setAttribute('stroke-width', '1');
5041
- svg.appendChild(ring);
5042
- // Shield body
5043
- const shield = document.createElementNS('http://www.w3.org/2000/svg', 'path');
5044
- shield.setAttribute('d', 'M28 13l-10 4.2v8.6c0 6.7 4.27 12.95 10 14.4 5.73-1.45 10-7.7 10-14.4v-8.6L28 13z');
5045
- shield.setAttribute('fill', '#F59E0B');
5046
- svg.appendChild(shield);
5047
- // Exclamation bar
5048
- const bar = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
5049
- bar.setAttribute('x', '26.6');
5050
- bar.setAttribute('y', '20');
5051
- bar.setAttribute('width', '2.8');
5052
- bar.setAttribute('height', '10');
5053
- bar.setAttribute('rx', '1.4');
5054
- bar.setAttribute('fill', '#FFFFFF');
5055
- svg.appendChild(bar);
5056
- // Exclamation dot
5057
- const dot = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
5058
- dot.setAttribute('cx', '28');
5059
- dot.setAttribute('cy', '34');
5060
- dot.setAttribute('r', '1.6');
5061
- dot.setAttribute('fill', '#FFFFFF');
5062
- svg.appendChild(dot);
5062
+ const surface = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
5063
+ surface.setAttribute('cx', '32');
5064
+ surface.setAttribute('cy', '32');
5065
+ surface.setAttribute('r', '29.5');
5066
+ surface.setAttribute('fill', '#F8FAFC');
5067
+ surface.setAttribute('stroke', '#E2E8F0');
5068
+ surface.setAttribute('stroke-width', '1');
5069
+ svg.appendChild(surface);
5070
+ const panel = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
5071
+ panel.setAttribute('x', '18.5');
5072
+ panel.setAttribute('y', '18.5');
5073
+ panel.setAttribute('width', '27');
5074
+ panel.setAttribute('height', '27');
5075
+ panel.setAttribute('rx', '7');
5076
+ panel.setAttribute('fill', '#FFFFFF');
5077
+ panel.setAttribute('stroke', '#CBD5E1');
5078
+ panel.setAttribute('stroke-width', '1');
5079
+ svg.appendChild(panel);
5080
+ const createLine = (y, x2) => {
5081
+ const line = document.createElementNS('http://www.w3.org/2000/svg', 'path');
5082
+ line.setAttribute('d', `M24 ${y}H${x2}`);
5083
+ line.setAttribute('stroke', '#475569');
5084
+ line.setAttribute('stroke-width', '2');
5085
+ line.setAttribute('stroke-linecap', 'round');
5086
+ svg.appendChild(line);
5087
+ };
5088
+ createLine('26', '40');
5089
+ createLine('32', '40');
5090
+ createLine('38', '35');
5091
+ const createKnob = (cx, cy) => {
5092
+ const knob = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
5093
+ knob.setAttribute('cx', cx);
5094
+ knob.setAttribute('cy', cy);
5095
+ knob.setAttribute('r', '2.5');
5096
+ knob.setAttribute('fill', '#4F46E5');
5097
+ knob.setAttribute('stroke', '#FFFFFF');
5098
+ knob.setAttribute('stroke-width', '1.5');
5099
+ svg.appendChild(knob);
5100
+ };
5101
+ createKnob('30', '26');
5102
+ createKnob('36', '32');
5103
+ createKnob('28', '38');
5063
5104
  badge.appendChild(svg);
5064
5105
  return badge;
5065
5106
  }
@@ -5096,7 +5137,7 @@ function createSparkVaultMark() {
5096
5137
  mark.appendChild(label);
5097
5138
  return mark;
5098
5139
  }
5099
- class AppNotActivatedView {
5140
+ class ProductNotConfiguredView {
5100
5141
  constructor(props) {
5101
5142
  this.dismissButton = null;
5102
5143
  this.props = props;
@@ -5120,12 +5161,12 @@ class AppNotActivatedView {
5120
5161
  const title = document.createElement('h2');
5121
5162
  title.className = 'sv-not-activated-title';
5122
5163
  title.id = 'sv-modal-title';
5123
- title.textContent = 'Identity app not activated';
5164
+ title.textContent = 'Identity Product not configured';
5124
5165
  container.appendChild(title);
5125
5166
  const message = document.createElement('p');
5126
5167
  message.className = 'sv-not-activated-message';
5127
5168
  message.textContent =
5128
- 'Sign-in is unavailable because the site owner hasn’t activated SparkVault Identity for this account. Activate the app to enable passwordless authentication.';
5169
+ 'Sign-in is unavailable because the site owner has not configured SparkVault Identity for this account.';
5129
5170
  container.appendChild(message);
5130
5171
  const ownerCard = div('sv-not-activated-owner');
5131
5172
  const ownerLabel = document.createElement('span');
@@ -5134,7 +5175,7 @@ class AppNotActivatedView {
5134
5175
  const ownerHint = document.createElement('span');
5135
5176
  ownerHint.className = 'sv-not-activated-owner-hint';
5136
5177
  ownerHint.textContent =
5137
- 'Sign in to your SparkVault account and install the Identity app to start accepting sign-ins.';
5178
+ 'Sign in to SparkVault and configure the Identity Product to start accepting sign-ins.';
5138
5179
  ownerCard.appendChild(ownerLabel);
5139
5180
  ownerCard.appendChild(ownerHint);
5140
5181
  container.appendChild(ownerCard);
@@ -5145,7 +5186,7 @@ class AppNotActivatedView {
5145
5186
  cta.target = '_blank';
5146
5187
  cta.rel = 'noopener noreferrer';
5147
5188
  const ctaLabel = document.createElement('span');
5148
- ctaLabel.textContent = 'Activate on SparkVault';
5189
+ ctaLabel.textContent = 'Configure in SparkVault';
5149
5190
  cta.appendChild(ctaLabel);
5150
5191
  cta.appendChild(createExternalLinkIcon());
5151
5192
  actions.appendChild(cta);
@@ -5200,6 +5241,20 @@ class IdentityRenderer {
5200
5241
  this.verificationState.setAuthContext(options.authContext);
5201
5242
  }
5202
5243
  }
5244
+ /**
5245
+ * Fire the host's onSuccess callback, swallowing any exception so a
5246
+ * misbehaving host handler can't block the post-success redirect the
5247
+ * caller is depending on. The verify flow continues regardless.
5248
+ */
5249
+ safeOnSuccess(result) {
5250
+ try {
5251
+ this.callbacks.onSuccess(result);
5252
+ }
5253
+ catch (err) {
5254
+ // eslint-disable-next-line no-console
5255
+ console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
5256
+ }
5257
+ }
5203
5258
  // Convenience accessors for state
5204
5259
  get recipient() {
5205
5260
  return this.verificationState.recipient;
@@ -5222,7 +5277,7 @@ class IdentityRenderer {
5222
5277
  if (this.api.isConfigPreloaded()) {
5223
5278
  // Config fetch already started - race it against a 50ms timeout
5224
5279
  // If config is ready (or almost ready), we skip the loading flash.
5225
- // A rejected preload (e.g. app_not_installed) must not abort start() —
5280
+ // A rejected preload (e.g. product_not_configured) must not abort start() —
5226
5281
  // swallow here and let the in-modal try/catch below route to the
5227
5282
  // correct error view.
5228
5283
  const timeoutPromise = new Promise((resolve) => setTimeout(() => resolve(null), 50));
@@ -5276,13 +5331,13 @@ class IdentityRenderer {
5276
5331
  }
5277
5332
  /**
5278
5333
  * Map a config-fetch error to the appropriate terminal view.
5279
- * `app_not_installed` is a misconfiguration (account hasn't installed the
5280
- * Identity app) — show a developer-targeted "Not Activated" screen with a
5334
+ * `product_not_configured` is a misconfiguration (the account has not
5335
+ * configured Identity) — show a developer-targeted terminal screen with a
5281
5336
  * link to SparkVault.com instead of the generic service-unavailable view.
5282
5337
  */
5283
5338
  viewStateForConfigError(error) {
5284
- if (error instanceof IdentityApiError && error.code === 'app_not_installed') {
5285
- return { view: 'app-not-activated' };
5339
+ if (error instanceof IdentityApiError && error.code === 'product_not_configured') {
5340
+ return { view: 'product-not-configured' };
5286
5341
  }
5287
5342
  return { view: 'service-unavailable' };
5288
5343
  }
@@ -5370,7 +5425,7 @@ class IdentityRenderer {
5370
5425
  // Full-dialog terminal screens own the entire modal; toggle the container
5371
5426
  // chrome accordingly so the customer header doesn't bleed into a
5372
5427
  // SparkVault-owned message.
5373
- this.container.setMinimalChrome(this.viewState.view === 'app-not-activated');
5428
+ this.container.setMinimalChrome(this.viewState.view === 'product-not-configured');
5374
5429
  const view = this.createViewForState(this.viewState);
5375
5430
  this.currentView = view;
5376
5431
  clearChildren(body);
@@ -5472,8 +5527,8 @@ class IdentityRenderer {
5472
5527
  onRetry: () => this.retryConfigLoad(),
5473
5528
  onClose: () => this.handleClose(),
5474
5529
  });
5475
- case 'app-not-activated':
5476
- return new AppNotActivatedView({
5530
+ case 'product-not-configured':
5531
+ return new ProductNotConfiguredView({
5477
5532
  onClose: () => this.handleClose(),
5478
5533
  });
5479
5534
  }
@@ -5670,17 +5725,20 @@ class IdentityRenderer {
5670
5725
  this.close();
5671
5726
  // Hand the full result (including redirect) to the host first so the
5672
5727
  // page-level handler can act before we navigate away.
5673
- try {
5674
- this.callbacks.onSuccess(result.result);
5675
- }
5676
- catch (err) {
5677
- // eslint-disable-next-line no-console
5678
- console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
5679
- }
5728
+ this.safeOnSuccess(result.result);
5680
5729
  if (typeof result.result.redirect === 'string' && result.result.redirect) {
5681
5730
  window.location.href = result.result.redirect;
5682
5731
  }
5683
5732
  }
5733
+ else if (result.errorCode === 'identity_locked') {
5734
+ // The identity is locked down — a dedicated, actionable state, not a
5735
+ // generic "invalid code" retry. The person must manage it at auth.sv.
5736
+ this.setState({
5737
+ view: 'error',
5738
+ code: 'identity_locked',
5739
+ message: 'This SparkVault Identity is locked. Visit auth.sv to manage it.',
5740
+ });
5741
+ }
5684
5742
  else {
5685
5743
  // Check if error is expiry - auto-resend if so
5686
5744
  const errorMsg = result.error?.toLowerCase() ?? '';
@@ -5743,17 +5801,19 @@ class IdentityRenderer {
5743
5801
  : await this.passkeyHandler.verify();
5744
5802
  if (result.success && result.result) {
5745
5803
  this.close();
5746
- try {
5747
- this.callbacks.onSuccess(result.result);
5748
- }
5749
- catch (err) {
5750
- // eslint-disable-next-line no-console
5751
- console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
5752
- }
5804
+ this.safeOnSuccess(result.result);
5753
5805
  if (typeof result.result.redirect === 'string' && result.result.redirect) {
5754
5806
  window.location.href = result.result.redirect;
5755
5807
  }
5756
5808
  }
5809
+ else if (result.errorCode === 'identity_locked') {
5810
+ // Dedicated state — retrying cannot unlock the identity.
5811
+ this.setState({
5812
+ view: 'error',
5813
+ code: 'identity_locked',
5814
+ message: 'This SparkVault Identity is locked. Visit auth.sv to manage it.',
5815
+ });
5816
+ }
5757
5817
  else {
5758
5818
  // Handle different error types
5759
5819
  if (result.errorType === 'not_found') {
@@ -5791,13 +5851,7 @@ class IdentityRenderer {
5791
5851
  if (pendingResult) {
5792
5852
  this.verificationState.setPendingResult(null);
5793
5853
  this.close();
5794
- try {
5795
- this.callbacks.onSuccess(pendingResult);
5796
- }
5797
- catch (err) {
5798
- // eslint-disable-next-line no-console
5799
- console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
5800
- }
5854
+ this.safeOnSuccess(pendingResult);
5801
5855
  if (typeof pendingResult.redirect === 'string' && pendingResult.redirect) {
5802
5856
  window.location.href = pendingResult.redirect;
5803
5857
  }
@@ -5880,17 +5934,11 @@ class IdentityRenderer {
5880
5934
  // Show loading while browser passkey dialog is active
5881
5935
  this.setState({ view: 'loading' });
5882
5936
  // Directly trigger passkey registration
5883
- const result = await this.passkeyHandler.register();
5937
+ const result = await this.passkeyHandler.register(pendingResult.token);
5884
5938
  if (result.success && result.result) {
5885
5939
  // Passkey created successfully - use the new token
5886
5940
  this.close();
5887
- try {
5888
- this.callbacks.onSuccess(result.result);
5889
- }
5890
- catch (err) {
5891
- // eslint-disable-next-line no-console
5892
- console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
5893
- }
5941
+ this.safeOnSuccess(result.result);
5894
5942
  if (typeof result.result.redirect === 'string' && result.result.redirect) {
5895
5943
  window.location.href = result.result.redirect;
5896
5944
  }
@@ -5922,13 +5970,7 @@ class IdentityRenderer {
5922
5970
  this.verificationState.dismissPasskeyPrompt();
5923
5971
  // Complete the verification
5924
5972
  this.close();
5925
- try {
5926
- this.callbacks.onSuccess(pendingResult);
5927
- }
5928
- catch (err) {
5929
- // eslint-disable-next-line no-console
5930
- console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
5931
- }
5973
+ this.safeOnSuccess(pendingResult);
5932
5974
  if (typeof pendingResult.redirect === 'string' && pendingResult.redirect) {
5933
5975
  window.location.href = pendingResult.redirect;
5934
5976
  }
@@ -6033,19 +6075,6 @@ class IdentityRenderer {
6033
6075
  catch {
6034
6076
  return;
6035
6077
  }
6036
- // Diagnostic: dump the parsed message so we can see on the client
6037
- // EXACTLY what arrives. Keys + redirect prefix tell us whether the
6038
- // field made it across API Gateway. Remove once we've confirmed.
6039
- // eslint-disable-next-line no-console
6040
- console.info('[SparkVault SDK] sparklink WS message', {
6041
- type: data.type,
6042
- keys: Object.keys(data),
6043
- hasRedirect: typeof data.redirect === 'string',
6044
- redirectPrefix: typeof data.redirect === 'string'
6045
- ? data.redirect.slice(0, 80)
6046
- : null,
6047
- rawLength: event.data.length,
6048
- });
6049
6078
  switch (data.type) {
6050
6079
  case 'sparklink_verified':
6051
6080
  this.handleSparkLinkVerified({
@@ -6107,13 +6136,7 @@ class IdentityRenderer {
6107
6136
  // Hand the full result (including redirect) to the host first so the
6108
6137
  // page-level handler can do app-specific work (logging, navigation,
6109
6138
  // etc.) before we steal the browsing context with a navigation.
6110
- try {
6111
- this.callbacks.onSuccess(result);
6112
- }
6113
- catch (err) {
6114
- // eslint-disable-next-line no-console
6115
- console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
6116
- }
6139
+ this.safeOnSuccess(result);
6117
6140
  // SDK-side fallback navigation. If the host already navigated this is
6118
6141
  // a no-op (the document is already unloading); if not, this rescues
6119
6142
  // simple-mode flows whose host forgot to act on result.redirect.
@@ -8628,7 +8651,9 @@ function hexToBase64url(hex) {
8628
8651
  */
8629
8652
  function generateRequestId() {
8630
8653
  const timestamp = Date.now().toString(16);
8631
- const random = Math.random().toString(16).substring(2, 10);
8654
+ const randomBytes = new Uint8Array(4);
8655
+ crypto.getRandomValues(randomBytes);
8656
+ const random = Array.from(randomBytes, byte => byte.toString(16).padStart(2, '0')).join('');
8632
8657
  return `${timestamp}${random}`;
8633
8658
  }
8634
8659
  /**