@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.
@@ -73,7 +73,7 @@ const DEFAULT_ALLOWED_DOWNLOAD_HOST_PATTERNS = [
73
73
  /\.cloudfront\.net$/i,
74
74
  ];
75
75
  const API_URL = 'https://api.sparkvault.com';
76
- const IDENTITY_URL = 'https://api.sparkvault.com/v1/apps/identity';
76
+ const IDENTITY_URL = 'https://api.sparkvault.com/v1/products/identity';
77
77
  function normalizeApiBaseUrl(url) {
78
78
  const trimmed = url.replace(/\/+$/, '');
79
79
  return trimmed.endsWith('/v1') ? trimmed.slice(0, -3) : trimmed;
@@ -148,6 +148,14 @@ function defaultIsRetryable(error) {
148
148
  }
149
149
  return false;
150
150
  }
151
+ /**
152
+ * Return a random unit interval value using Web Crypto.
153
+ */
154
+ function randomUnit() {
155
+ const value = new Uint32Array(1);
156
+ crypto.getRandomValues(value);
157
+ return value[0] / 0x100000000;
158
+ }
151
159
  /**
152
160
  * Calculate delay with exponential backoff and jitter
153
161
  */
@@ -156,7 +164,7 @@ function calculateDelay(attempt, baseDelayMs, maxDelayMs, jitterFactor) {
156
164
  const exponentialDelay = baseDelayMs * Math.pow(2, attempt);
157
165
  const cappedDelay = Math.min(exponentialDelay, maxDelayMs);
158
166
  // Add jitter: random value within jitter range
159
- const jitter = cappedDelay * jitterFactor * Math.random();
167
+ const jitter = cappedDelay * jitterFactor * randomUnit();
160
168
  return cappedDelay + jitter;
161
169
  }
162
170
  /**
@@ -890,7 +898,7 @@ function generateState() {
890
898
  /**
891
899
  * Identity API Client
892
900
  *
893
- * Handles all HTTP communication with the Identity App endpoints.
901
+ * Handles all HTTP communication with the Identity Product endpoints.
894
902
  * Single responsibility: API calls only.
895
903
  */
896
904
  class IdentityApi extends SparkVaultApi {
@@ -966,8 +974,6 @@ class IdentityApi extends SparkVaultApi {
966
974
  * (email: MX lookup; phone: E.164 format)
967
975
  * - has_passkey: whether any passkeys are registered for this identity
968
976
  * (cross-tenant — single global RP)
969
- * - email_valid: legacy alias for identity_valid, present for backward
970
- * compatibility with older SDK versions
971
977
  */
972
978
  async checkPasskey(identity, identityType = 'email') {
973
979
  const body = identityType === 'phone' ? { phone: identity } : { email: identity };
@@ -1018,8 +1024,10 @@ class IdentityApi extends SparkVaultApi {
1018
1024
  /**
1019
1025
  * Start passkey registration for an identity (email or phone).
1020
1026
  */
1021
- async startPasskeyRegister(identity, identityType = 'email') {
1022
- const body = identityType === 'phone' ? { phone: identity } : { email: identity };
1027
+ async startPasskeyRegister(identity, identityType = 'email', registrationToken) {
1028
+ const body = identityType === 'phone'
1029
+ ? { phone: identity, registration_token: registrationToken }
1030
+ : { email: identity, registration_token: registrationToken };
1023
1031
  // Backend returns { options: PublicKeyCredentialCreationOptions, session: {...} }
1024
1032
  // Extract and flatten to match PasskeyChallengeResponse
1025
1033
  const response = await this.request('POST', '/passkey/register', body);
@@ -1467,10 +1475,8 @@ class PasskeyHandler {
1467
1475
  try {
1468
1476
  const response = await this.api.checkPasskey(this.state.recipient, this.state.identityType);
1469
1477
  this.state.setPasskeyStatus(response.has_passkey);
1470
- // Prefer identity_valid; fall back to email_valid for older backends.
1471
- const valid = response.identity_valid ?? response.email_valid;
1472
1478
  return {
1473
- emailValid: valid,
1479
+ emailValid: response.identity_valid,
1474
1480
  hasPasskey: response.has_passkey,
1475
1481
  };
1476
1482
  }
@@ -1483,8 +1489,8 @@ class PasskeyHandler {
1483
1489
  /**
1484
1490
  * Register a new passkey for the user via popup
1485
1491
  */
1486
- async register() {
1487
- const result = await this.openPasskeyPopup('register');
1492
+ async register(registrationToken) {
1493
+ const result = await this.openPasskeyPopup('register', registrationToken);
1488
1494
  if (result.success) {
1489
1495
  // Update state - user now has a passkey
1490
1496
  this.state.setPasskeyStatus(true);
@@ -1500,7 +1506,7 @@ class PasskeyHandler {
1500
1506
  /**
1501
1507
  * Open passkey popup and wait for result
1502
1508
  */
1503
- async openPasskeyPopup(mode) {
1509
+ async openPasskeyPopup(mode, registrationToken) {
1504
1510
  return new Promise((resolve) => {
1505
1511
  // Build popup URL. Pass identity as `email` or `phone` (mutually
1506
1512
  // exclusive) so the popup can render and POST the correct field.
@@ -1523,7 +1529,10 @@ class PasskeyHandler {
1523
1529
  session_id: authContext.simpleMode.sessionId,
1524
1530
  }));
1525
1531
  }
1526
- const popupUrl = `${this.baseUrl}/${this.accountId}/passkey/popup?${params}`;
1532
+ const hash = mode === 'register' && registrationToken
1533
+ ? `#${new URLSearchParams({ registration_token: registrationToken }).toString()}`
1534
+ : '';
1535
+ const popupUrl = `${this.baseUrl}/${this.accountId}/passkey/popup?${params}${hash}`;
1527
1536
  // Calculate popup position (centered)
1528
1537
  const left = Math.round((window.screen.width - POPUP_WIDTH) / 2);
1529
1538
  const top = Math.round((window.screen.height - POPUP_HEIGHT) / 2);
@@ -1568,7 +1577,7 @@ class PasskeyHandler {
1568
1577
  });
1569
1578
  }
1570
1579
  else {
1571
- resolve(this.handlePopupError(data.error, data.message));
1580
+ resolve(this.handlePopupError(data.error, data.message, data.errorCode));
1572
1581
  }
1573
1582
  };
1574
1583
  window.addEventListener('message', handleMessage);
@@ -1619,7 +1628,7 @@ class PasskeyHandler {
1619
1628
  /**
1620
1629
  * Handle popup error response
1621
1630
  */
1622
- handlePopupError(error, message) {
1631
+ handlePopupError(error, message, errorCode) {
1623
1632
  if (error === 'cancelled') {
1624
1633
  return {
1625
1634
  success: false,
@@ -1627,6 +1636,14 @@ class PasskeyHandler {
1627
1636
  errorType: 'cancelled',
1628
1637
  };
1629
1638
  }
1639
+ if (errorCode === 'identity_locked' || error === 'identity_locked') {
1640
+ return {
1641
+ success: false,
1642
+ error: message || 'This SparkVault Identity is locked. Visit auth.sv to manage it.',
1643
+ errorType: 'unknown',
1644
+ errorCode: 'identity_locked',
1645
+ };
1646
+ }
1630
1647
  if (message?.includes('No passkey') || message?.includes('not found')) {
1631
1648
  return {
1632
1649
  success: false,
@@ -1638,6 +1655,7 @@ class PasskeyHandler {
1638
1655
  success: false,
1639
1656
  error: message || error || 'Passkey authentication failed',
1640
1657
  errorType: 'unknown',
1658
+ errorCode,
1641
1659
  };
1642
1660
  }
1643
1661
  }
@@ -1759,9 +1777,11 @@ class TotpHandler {
1759
1777
  }
1760
1778
  catch (error) {
1761
1779
  const message = error instanceof Error ? error.message : 'Verification failed';
1780
+ const errorCode = (error instanceof SparkVaultError) ? error.code : undefined;
1762
1781
  return {
1763
1782
  success: false,
1764
1783
  error: message,
1784
+ errorCode,
1765
1785
  };
1766
1786
  }
1767
1787
  }
@@ -2702,7 +2722,7 @@ function getStyles(options) {
2702
2722
 
2703
2723
  .sv-not-activated-badge-svg {
2704
2724
  display: block;
2705
- filter: drop-shadow(0 6px 14px rgba(245, 158, 11, 0.18));
2725
+ filter: drop-shadow(0 8px 18px rgba(15, 23, 42, 0.08));
2706
2726
  }
2707
2727
 
2708
2728
  .sv-not-activated-title {
@@ -4859,38 +4879,46 @@ class ErrorView {
4859
4879
  // Content section
4860
4880
  const contentSection = div();
4861
4881
  contentSection.className = 'sv-error-content';
4882
+ // A locked-down identity is a dedicated, non-error state: a specific
4883
+ // title, no debug error-code badge, and no "Try Again" (retrying the
4884
+ // login cannot clear a lockdown — only managing it at auth.sv can).
4885
+ const isLocked = this.props.code === 'identity_locked';
4862
4886
  const title = document.createElement('h3');
4863
4887
  title.className = 'sv-error-title';
4864
- title.textContent = 'Something went wrong';
4888
+ title.textContent = isLocked ? 'This SparkVault Identity is locked' : 'Something went wrong';
4865
4889
  const message = document.createElement('p');
4866
4890
  message.className = 'sv-error-message';
4867
4891
  message.textContent = this.props.message;
4868
- // Error code badge
4869
- const codeBadge = document.createElement('div');
4870
- codeBadge.className = 'sv-error-code-badge';
4871
- const codeLabel = document.createElement('span');
4872
- codeLabel.className = 'sv-error-code-label';
4873
- codeLabel.textContent = 'Error code';
4874
- const codeValue = document.createElement('code');
4875
- codeValue.className = 'sv-error-code-value';
4876
- codeValue.textContent = this.props.code;
4877
- codeBadge.appendChild(codeLabel);
4878
- codeBadge.appendChild(codeValue);
4879
4892
  contentSection.appendChild(title);
4880
4893
  contentSection.appendChild(message);
4881
- contentSection.appendChild(codeBadge);
4894
+ if (!isLocked) {
4895
+ // Error code badge — useful for debugging genuine errors only.
4896
+ const codeBadge = document.createElement('div');
4897
+ codeBadge.className = 'sv-error-code-badge';
4898
+ const codeLabel = document.createElement('span');
4899
+ codeLabel.className = 'sv-error-code-label';
4900
+ codeLabel.textContent = 'Error code';
4901
+ const codeValue = document.createElement('code');
4902
+ codeValue.className = 'sv-error-code-value';
4903
+ codeValue.textContent = this.props.code;
4904
+ codeBadge.appendChild(codeLabel);
4905
+ codeBadge.appendChild(codeValue);
4906
+ contentSection.appendChild(codeBadge);
4907
+ }
4882
4908
  // Action buttons with proper styling
4883
4909
  const buttonContainer = div();
4884
4910
  buttonContainer.className = 'sv-error-actions';
4885
- this.retryButton = document.createElement('button');
4886
- this.retryButton.className = 'sv-btn sv-btn-error-primary';
4887
- this.retryButton.textContent = 'Try Again';
4888
- this.retryButton.addEventListener('click', this.boundHandleRetry);
4911
+ if (!isLocked) {
4912
+ this.retryButton = document.createElement('button');
4913
+ this.retryButton.className = 'sv-btn sv-btn-error-primary';
4914
+ this.retryButton.textContent = 'Try Again';
4915
+ this.retryButton.addEventListener('click', this.boundHandleRetry);
4916
+ buttonContainer.appendChild(this.retryButton);
4917
+ }
4889
4918
  this.closeButton = document.createElement('button');
4890
4919
  this.closeButton.className = 'sv-btn sv-btn-error-secondary';
4891
4920
  this.closeButton.textContent = 'Close';
4892
4921
  this.closeButton.addEventListener('click', this.boundHandleClose);
4893
- buttonContainer.appendChild(this.retryButton);
4894
4922
  buttonContainer.appendChild(this.closeButton);
4895
4923
  container.appendChild(iconContainer);
4896
4924
  container.appendChild(contentSection);
@@ -5009,53 +5037,66 @@ class ServiceUnavailableView {
5009
5037
  }
5010
5038
 
5011
5039
  /**
5012
- * App Not Activated View
5040
+ * Product Not Configured View
5013
5041
  *
5014
- * Full-dialog terminal screen shown when the SparkVault Identity app has not
5015
- * been activated for the account that the JS SDK is configured against. The
5042
+ * Full-dialog terminal screen shown when the SparkVault Identity Product has
5043
+ * not been configured for the account that the JS SDK is configured against. The
5016
5044
  * audience is the website owner / developer — this is a misconfiguration
5017
5045
  * surface, not an end-user error — so the dialog drops customer branding and
5018
5046
  * presents itself as an unmistakable SparkVault notice.
5019
5047
  */
5020
- const ACTIVATION_URL = 'https://app.sparkvault.com/apps';
5048
+ const ACTIVATION_URL = 'https://app.sparkvault.com/products/identity';
5021
5049
  function createBadge() {
5022
5050
  const badge = div('sv-not-activated-badge');
5023
5051
  const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
5024
- svg.setAttribute('viewBox', '0 0 56 56');
5052
+ svg.setAttribute('viewBox', '0 0 64 64');
5025
5053
  svg.setAttribute('width', '72');
5026
5054
  svg.setAttribute('height', '72');
5027
5055
  svg.setAttribute('fill', 'none');
5056
+ svg.setAttribute('aria-hidden', 'true');
5028
5057
  svg.classList.add('sv-not-activated-badge-svg');
5029
- // Soft outer ring
5030
- const ring = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
5031
- ring.setAttribute('cx', '28');
5032
- ring.setAttribute('cy', '28');
5033
- ring.setAttribute('r', '27');
5034
- ring.setAttribute('fill', '#FFFBEB');
5035
- ring.setAttribute('stroke', '#FDE68A');
5036
- ring.setAttribute('stroke-width', '1');
5037
- svg.appendChild(ring);
5038
- // Shield body
5039
- const shield = document.createElementNS('http://www.w3.org/2000/svg', 'path');
5040
- 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');
5041
- shield.setAttribute('fill', '#F59E0B');
5042
- svg.appendChild(shield);
5043
- // Exclamation bar
5044
- const bar = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
5045
- bar.setAttribute('x', '26.6');
5046
- bar.setAttribute('y', '20');
5047
- bar.setAttribute('width', '2.8');
5048
- bar.setAttribute('height', '10');
5049
- bar.setAttribute('rx', '1.4');
5050
- bar.setAttribute('fill', '#FFFFFF');
5051
- svg.appendChild(bar);
5052
- // Exclamation dot
5053
- const dot = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
5054
- dot.setAttribute('cx', '28');
5055
- dot.setAttribute('cy', '34');
5056
- dot.setAttribute('r', '1.6');
5057
- dot.setAttribute('fill', '#FFFFFF');
5058
- svg.appendChild(dot);
5058
+ const surface = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
5059
+ surface.setAttribute('cx', '32');
5060
+ surface.setAttribute('cy', '32');
5061
+ surface.setAttribute('r', '29.5');
5062
+ surface.setAttribute('fill', '#F8FAFC');
5063
+ surface.setAttribute('stroke', '#E2E8F0');
5064
+ surface.setAttribute('stroke-width', '1');
5065
+ svg.appendChild(surface);
5066
+ const panel = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
5067
+ panel.setAttribute('x', '18.5');
5068
+ panel.setAttribute('y', '18.5');
5069
+ panel.setAttribute('width', '27');
5070
+ panel.setAttribute('height', '27');
5071
+ panel.setAttribute('rx', '7');
5072
+ panel.setAttribute('fill', '#FFFFFF');
5073
+ panel.setAttribute('stroke', '#CBD5E1');
5074
+ panel.setAttribute('stroke-width', '1');
5075
+ svg.appendChild(panel);
5076
+ const createLine = (y, x2) => {
5077
+ const line = document.createElementNS('http://www.w3.org/2000/svg', 'path');
5078
+ line.setAttribute('d', `M24 ${y}H${x2}`);
5079
+ line.setAttribute('stroke', '#475569');
5080
+ line.setAttribute('stroke-width', '2');
5081
+ line.setAttribute('stroke-linecap', 'round');
5082
+ svg.appendChild(line);
5083
+ };
5084
+ createLine('26', '40');
5085
+ createLine('32', '40');
5086
+ createLine('38', '35');
5087
+ const createKnob = (cx, cy) => {
5088
+ const knob = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
5089
+ knob.setAttribute('cx', cx);
5090
+ knob.setAttribute('cy', cy);
5091
+ knob.setAttribute('r', '2.5');
5092
+ knob.setAttribute('fill', '#4F46E5');
5093
+ knob.setAttribute('stroke', '#FFFFFF');
5094
+ knob.setAttribute('stroke-width', '1.5');
5095
+ svg.appendChild(knob);
5096
+ };
5097
+ createKnob('30', '26');
5098
+ createKnob('36', '32');
5099
+ createKnob('28', '38');
5059
5100
  badge.appendChild(svg);
5060
5101
  return badge;
5061
5102
  }
@@ -5092,7 +5133,7 @@ function createSparkVaultMark() {
5092
5133
  mark.appendChild(label);
5093
5134
  return mark;
5094
5135
  }
5095
- class AppNotActivatedView {
5136
+ class ProductNotConfiguredView {
5096
5137
  constructor(props) {
5097
5138
  this.dismissButton = null;
5098
5139
  this.props = props;
@@ -5116,12 +5157,12 @@ class AppNotActivatedView {
5116
5157
  const title = document.createElement('h2');
5117
5158
  title.className = 'sv-not-activated-title';
5118
5159
  title.id = 'sv-modal-title';
5119
- title.textContent = 'Identity app not activated';
5160
+ title.textContent = 'Identity Product not configured';
5120
5161
  container.appendChild(title);
5121
5162
  const message = document.createElement('p');
5122
5163
  message.className = 'sv-not-activated-message';
5123
5164
  message.textContent =
5124
- 'Sign-in is unavailable because the site owner hasn’t activated SparkVault Identity for this account. Activate the app to enable passwordless authentication.';
5165
+ 'Sign-in is unavailable because the site owner has not configured SparkVault Identity for this account.';
5125
5166
  container.appendChild(message);
5126
5167
  const ownerCard = div('sv-not-activated-owner');
5127
5168
  const ownerLabel = document.createElement('span');
@@ -5130,7 +5171,7 @@ class AppNotActivatedView {
5130
5171
  const ownerHint = document.createElement('span');
5131
5172
  ownerHint.className = 'sv-not-activated-owner-hint';
5132
5173
  ownerHint.textContent =
5133
- 'Sign in to your SparkVault account and install the Identity app to start accepting sign-ins.';
5174
+ 'Sign in to SparkVault and configure the Identity Product to start accepting sign-ins.';
5134
5175
  ownerCard.appendChild(ownerLabel);
5135
5176
  ownerCard.appendChild(ownerHint);
5136
5177
  container.appendChild(ownerCard);
@@ -5141,7 +5182,7 @@ class AppNotActivatedView {
5141
5182
  cta.target = '_blank';
5142
5183
  cta.rel = 'noopener noreferrer';
5143
5184
  const ctaLabel = document.createElement('span');
5144
- ctaLabel.textContent = 'Activate on SparkVault';
5185
+ ctaLabel.textContent = 'Configure in SparkVault';
5145
5186
  cta.appendChild(ctaLabel);
5146
5187
  cta.appendChild(createExternalLinkIcon());
5147
5188
  actions.appendChild(cta);
@@ -5196,6 +5237,20 @@ class IdentityRenderer {
5196
5237
  this.verificationState.setAuthContext(options.authContext);
5197
5238
  }
5198
5239
  }
5240
+ /**
5241
+ * Fire the host's onSuccess callback, swallowing any exception so a
5242
+ * misbehaving host handler can't block the post-success redirect the
5243
+ * caller is depending on. The verify flow continues regardless.
5244
+ */
5245
+ safeOnSuccess(result) {
5246
+ try {
5247
+ this.callbacks.onSuccess(result);
5248
+ }
5249
+ catch (err) {
5250
+ // eslint-disable-next-line no-console
5251
+ console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
5252
+ }
5253
+ }
5199
5254
  // Convenience accessors for state
5200
5255
  get recipient() {
5201
5256
  return this.verificationState.recipient;
@@ -5218,7 +5273,7 @@ class IdentityRenderer {
5218
5273
  if (this.api.isConfigPreloaded()) {
5219
5274
  // Config fetch already started - race it against a 50ms timeout
5220
5275
  // If config is ready (or almost ready), we skip the loading flash.
5221
- // A rejected preload (e.g. app_not_installed) must not abort start() —
5276
+ // A rejected preload (e.g. product_not_configured) must not abort start() —
5222
5277
  // swallow here and let the in-modal try/catch below route to the
5223
5278
  // correct error view.
5224
5279
  const timeoutPromise = new Promise((resolve) => setTimeout(() => resolve(null), 50));
@@ -5272,13 +5327,13 @@ class IdentityRenderer {
5272
5327
  }
5273
5328
  /**
5274
5329
  * Map a config-fetch error to the appropriate terminal view.
5275
- * `app_not_installed` is a misconfiguration (account hasn't installed the
5276
- * Identity app) — show a developer-targeted "Not Activated" screen with a
5330
+ * `product_not_configured` is a misconfiguration (the account has not
5331
+ * configured Identity) — show a developer-targeted terminal screen with a
5277
5332
  * link to SparkVault.com instead of the generic service-unavailable view.
5278
5333
  */
5279
5334
  viewStateForConfigError(error) {
5280
- if (error instanceof IdentityApiError && error.code === 'app_not_installed') {
5281
- return { view: 'app-not-activated' };
5335
+ if (error instanceof IdentityApiError && error.code === 'product_not_configured') {
5336
+ return { view: 'product-not-configured' };
5282
5337
  }
5283
5338
  return { view: 'service-unavailable' };
5284
5339
  }
@@ -5366,7 +5421,7 @@ class IdentityRenderer {
5366
5421
  // Full-dialog terminal screens own the entire modal; toggle the container
5367
5422
  // chrome accordingly so the customer header doesn't bleed into a
5368
5423
  // SparkVault-owned message.
5369
- this.container.setMinimalChrome(this.viewState.view === 'app-not-activated');
5424
+ this.container.setMinimalChrome(this.viewState.view === 'product-not-configured');
5370
5425
  const view = this.createViewForState(this.viewState);
5371
5426
  this.currentView = view;
5372
5427
  clearChildren(body);
@@ -5468,8 +5523,8 @@ class IdentityRenderer {
5468
5523
  onRetry: () => this.retryConfigLoad(),
5469
5524
  onClose: () => this.handleClose(),
5470
5525
  });
5471
- case 'app-not-activated':
5472
- return new AppNotActivatedView({
5526
+ case 'product-not-configured':
5527
+ return new ProductNotConfiguredView({
5473
5528
  onClose: () => this.handleClose(),
5474
5529
  });
5475
5530
  }
@@ -5666,17 +5721,20 @@ class IdentityRenderer {
5666
5721
  this.close();
5667
5722
  // Hand the full result (including redirect) to the host first so the
5668
5723
  // page-level handler can act before we navigate away.
5669
- try {
5670
- this.callbacks.onSuccess(result.result);
5671
- }
5672
- catch (err) {
5673
- // eslint-disable-next-line no-console
5674
- console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
5675
- }
5724
+ this.safeOnSuccess(result.result);
5676
5725
  if (typeof result.result.redirect === 'string' && result.result.redirect) {
5677
5726
  window.location.href = result.result.redirect;
5678
5727
  }
5679
5728
  }
5729
+ else if (result.errorCode === 'identity_locked') {
5730
+ // The identity is locked down — a dedicated, actionable state, not a
5731
+ // generic "invalid code" retry. The person must manage it at auth.sv.
5732
+ this.setState({
5733
+ view: 'error',
5734
+ code: 'identity_locked',
5735
+ message: 'This SparkVault Identity is locked. Visit auth.sv to manage it.',
5736
+ });
5737
+ }
5680
5738
  else {
5681
5739
  // Check if error is expiry - auto-resend if so
5682
5740
  const errorMsg = result.error?.toLowerCase() ?? '';
@@ -5739,17 +5797,19 @@ class IdentityRenderer {
5739
5797
  : await this.passkeyHandler.verify();
5740
5798
  if (result.success && result.result) {
5741
5799
  this.close();
5742
- try {
5743
- this.callbacks.onSuccess(result.result);
5744
- }
5745
- catch (err) {
5746
- // eslint-disable-next-line no-console
5747
- console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
5748
- }
5800
+ this.safeOnSuccess(result.result);
5749
5801
  if (typeof result.result.redirect === 'string' && result.result.redirect) {
5750
5802
  window.location.href = result.result.redirect;
5751
5803
  }
5752
5804
  }
5805
+ else if (result.errorCode === 'identity_locked') {
5806
+ // Dedicated state — retrying cannot unlock the identity.
5807
+ this.setState({
5808
+ view: 'error',
5809
+ code: 'identity_locked',
5810
+ message: 'This SparkVault Identity is locked. Visit auth.sv to manage it.',
5811
+ });
5812
+ }
5753
5813
  else {
5754
5814
  // Handle different error types
5755
5815
  if (result.errorType === 'not_found') {
@@ -5787,13 +5847,7 @@ class IdentityRenderer {
5787
5847
  if (pendingResult) {
5788
5848
  this.verificationState.setPendingResult(null);
5789
5849
  this.close();
5790
- try {
5791
- this.callbacks.onSuccess(pendingResult);
5792
- }
5793
- catch (err) {
5794
- // eslint-disable-next-line no-console
5795
- console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
5796
- }
5850
+ this.safeOnSuccess(pendingResult);
5797
5851
  if (typeof pendingResult.redirect === 'string' && pendingResult.redirect) {
5798
5852
  window.location.href = pendingResult.redirect;
5799
5853
  }
@@ -5876,17 +5930,11 @@ class IdentityRenderer {
5876
5930
  // Show loading while browser passkey dialog is active
5877
5931
  this.setState({ view: 'loading' });
5878
5932
  // Directly trigger passkey registration
5879
- const result = await this.passkeyHandler.register();
5933
+ const result = await this.passkeyHandler.register(pendingResult.token);
5880
5934
  if (result.success && result.result) {
5881
5935
  // Passkey created successfully - use the new token
5882
5936
  this.close();
5883
- try {
5884
- this.callbacks.onSuccess(result.result);
5885
- }
5886
- catch (err) {
5887
- // eslint-disable-next-line no-console
5888
- console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
5889
- }
5937
+ this.safeOnSuccess(result.result);
5890
5938
  if (typeof result.result.redirect === 'string' && result.result.redirect) {
5891
5939
  window.location.href = result.result.redirect;
5892
5940
  }
@@ -5918,13 +5966,7 @@ class IdentityRenderer {
5918
5966
  this.verificationState.dismissPasskeyPrompt();
5919
5967
  // Complete the verification
5920
5968
  this.close();
5921
- try {
5922
- this.callbacks.onSuccess(pendingResult);
5923
- }
5924
- catch (err) {
5925
- // eslint-disable-next-line no-console
5926
- console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
5927
- }
5969
+ this.safeOnSuccess(pendingResult);
5928
5970
  if (typeof pendingResult.redirect === 'string' && pendingResult.redirect) {
5929
5971
  window.location.href = pendingResult.redirect;
5930
5972
  }
@@ -6029,19 +6071,6 @@ class IdentityRenderer {
6029
6071
  catch {
6030
6072
  return;
6031
6073
  }
6032
- // Diagnostic: dump the parsed message so we can see on the client
6033
- // EXACTLY what arrives. Keys + redirect prefix tell us whether the
6034
- // field made it across API Gateway. Remove once we've confirmed.
6035
- // eslint-disable-next-line no-console
6036
- console.info('[SparkVault SDK] sparklink WS message', {
6037
- type: data.type,
6038
- keys: Object.keys(data),
6039
- hasRedirect: typeof data.redirect === 'string',
6040
- redirectPrefix: typeof data.redirect === 'string'
6041
- ? data.redirect.slice(0, 80)
6042
- : null,
6043
- rawLength: event.data.length,
6044
- });
6045
6074
  switch (data.type) {
6046
6075
  case 'sparklink_verified':
6047
6076
  this.handleSparkLinkVerified({
@@ -6103,13 +6132,7 @@ class IdentityRenderer {
6103
6132
  // Hand the full result (including redirect) to the host first so the
6104
6133
  // page-level handler can do app-specific work (logging, navigation,
6105
6134
  // etc.) before we steal the browsing context with a navigation.
6106
- try {
6107
- this.callbacks.onSuccess(result);
6108
- }
6109
- catch (err) {
6110
- // eslint-disable-next-line no-console
6111
- console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
6112
- }
6135
+ this.safeOnSuccess(result);
6113
6136
  // SDK-side fallback navigation. If the host already navigated this is
6114
6137
  // a no-op (the document is already unloading); if not, this rescues
6115
6138
  // simple-mode flows whose host forgot to act on result.redirect.
@@ -8624,7 +8647,9 @@ function hexToBase64url(hex) {
8624
8647
  */
8625
8648
  function generateRequestId() {
8626
8649
  const timestamp = Date.now().toString(16);
8627
- const random = Math.random().toString(16).substring(2, 10);
8650
+ const randomBytes = new Uint8Array(4);
8651
+ crypto.getRandomValues(randomBytes);
8652
+ const random = Array.from(randomBytes, byte => byte.toString(16).padStart(2, '0')).join('');
8628
8653
  return `${timestamp}${random}`;
8629
8654
  }
8630
8655
  /**