@sparkvault/sdk-js 1.27.2 → 1.27.4

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.
@@ -61,14 +61,72 @@ class PopupBlockedError extends SparkVaultError {
61
61
  }
62
62
  }
63
63
 
64
+ /**
65
+ * SparkVault-owned domain registry.
66
+ *
67
+ * Keep platform domain decisions centralized: CSP, redirect safety, custom
68
+ * domain reservation, and Identity postMessage trust all depend on the same
69
+ * ownership boundary.
70
+ */
71
+
72
+ const SPARKVAULT_ROOT_DOMAIN = 'sparkvault.com';
73
+
74
+ const SPARKVAULT_PLATFORM_SV_HOSTS = Object.freeze([
75
+ 'auth.sv',
76
+ 'x.sv',
77
+ 'files.sv',
78
+ 'file.sv',
79
+ 'send.sv',
80
+ 'spark.sv',
81
+ 'by.sv',
82
+ 'at.sv',
83
+ 'db.sv'
84
+ ]);
85
+
86
+ const SPARKVAULT_WEBSOCKET_HOSTS = Object.freeze([
87
+ 'ws.sparkvault.com'
88
+ ]);
89
+
90
+ Object.freeze([
91
+ SPARKVAULT_ROOT_DOMAIN,
92
+ 'app.sparkvault.com',
93
+ 'api.sparkvault.com',
94
+ 'cdn.sparkvault.com',
95
+ 'forge.sparkvault.com',
96
+ 'img.sparkvault.com',
97
+ ...SPARKVAULT_WEBSOCKET_HOSTS,
98
+ ...SPARKVAULT_PLATFORM_SV_HOSTS
99
+ ]);
100
+
101
+ function normalizeHostname(hostname) {
102
+ if (typeof hostname !== 'string') return '';
103
+ return hostname.trim().toLowerCase().replace(/\.$/, '');
104
+ }
105
+
106
+ function isSparkVaultRootOrSubdomain(hostname) {
107
+ const normalized = normalizeHostname(hostname);
108
+ return normalized === SPARKVAULT_ROOT_DOMAIN || normalized.endsWith(`.${SPARKVAULT_ROOT_DOMAIN}`);
109
+ }
110
+
111
+ function isSparkVaultPlatformSvHost(hostname) {
112
+ return SPARKVAULT_PLATFORM_SV_HOSTS.includes(normalizeHostname(hostname));
113
+ }
114
+
115
+ function isSparkVaultOwnedHostname(hostname) {
116
+ return isSparkVaultRootOrSubdomain(hostname) || isSparkVaultPlatformSvHost(hostname);
117
+ }
118
+
64
119
  /**
65
120
  * SparkVault SDK Configuration
66
121
  */
122
+ function escapeRegExp(value) {
123
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
124
+ }
125
+ const SPARKVAULT_PLATFORM_SV_DOWNLOAD_PATTERN = new RegExp(`(^|\\.)(${SPARKVAULT_PLATFORM_SV_HOSTS.map(escapeRegExp).join('|')})$`, 'i');
67
126
  // Hosts that are allowed to serve backend-issued ingot download URLs.
68
- // Keep in sync with the sdk-mobile default in packages/sdk-mobile/src/config.ts.
69
127
  const DEFAULT_ALLOWED_DOWNLOAD_HOST_PATTERNS = [
70
- /(^|\.)sparkvault\.com$/i,
71
- /(^|\.)(x|files|file|send|spark|by|at|db|auth)\.sv$/i,
128
+ new RegExp(`(^|\\.)${escapeRegExp(SPARKVAULT_ROOT_DOMAIN)}$`, 'i'),
129
+ SPARKVAULT_PLATFORM_SV_DOWNLOAD_PATTERN,
72
130
  /\.amazonaws\.com$/i,
73
131
  /\.cloudfront\.net$/i,
74
132
  ];
@@ -967,12 +1025,12 @@ class IdentityApi extends SparkVaultApi {
967
1025
  return this.configCache !== null;
968
1026
  }
969
1027
  /**
970
- * Check if an identity (email or phone) has registered passkeys.
1028
+ * Check whether the SVID resolved from an email or phone has registered passkeys.
971
1029
  *
972
1030
  * Returns:
973
1031
  * - identity_valid: identity passes per-type validity check
974
1032
  * (email: MX lookup; phone: E.164 format)
975
- * - has_passkey: whether any passkeys are registered for this identity
1033
+ * - has_passkey: whether any passkeys are registered on the resolved SVID
976
1034
  * (cross-tenant — single global RP)
977
1035
  */
978
1036
  async checkPasskey(identity, identityType = 'email') {
@@ -1022,7 +1080,7 @@ class IdentityApi extends SparkVaultApi {
1022
1080
  });
1023
1081
  }
1024
1082
  /**
1025
- * Start passkey registration for an identity (email or phone).
1083
+ * Start passkey registration from a freshly verified identifier.
1026
1084
  */
1027
1085
  async startPasskeyRegister(identity, identityType = 'email', registrationToken) {
1028
1086
  const body = identityType === 'phone'
@@ -1060,19 +1118,19 @@ class IdentityApi extends SparkVaultApi {
1060
1118
  });
1061
1119
  }
1062
1120
  /**
1063
- * Start passkey verification for an identity (email or phone).
1121
+ * Start discoverable passkey verification. Legacy identity parameters are
1122
+ * intentionally ignored; the credential userHandle routes completion to the
1123
+ * SVID.
1064
1124
  */
1065
- async startPasskeyVerify(identity, identityType = 'email', authContext) {
1066
- const identityField = identityType === 'phone' ? { phone: identity } : { email: identity };
1125
+ async startPasskeyVerify(_identity, _identityType = 'email', authContext) {
1067
1126
  // Backend returns { options: PublicKeyCredentialRequestOptions, session: {...} }
1068
1127
  // Extract and flatten to match PasskeyChallengeResponse
1069
- const response = await this.request('POST', '/passkey/verify', { ...identityField, ...this.buildAuthContextParams(authContext) });
1128
+ const response = await this.request('POST', '/passkey/verify', this.buildAuthContextParams(authContext));
1070
1129
  return {
1071
1130
  challenge: response.options.challenge,
1072
1131
  rpId: response.options.rpId,
1073
1132
  rpName: 'SparkVault Identity', // Not returned by verify endpoint
1074
1133
  timeout: response.options.timeout,
1075
- allowCredentials: response.options.allowCredentials,
1076
1134
  session: response.session,
1077
1135
  };
1078
1136
  }
@@ -1168,7 +1226,6 @@ const METHOD_REGISTRY = {
1168
1226
  passkey: {
1169
1227
  id: 'passkey',
1170
1228
  type: 'passkey',
1171
- identityType: 'email',
1172
1229
  name: 'Passkey',
1173
1230
  description: 'Use biometrics or security key',
1174
1231
  icon: 'passkey',
@@ -1313,19 +1370,20 @@ class VerificationState {
1313
1370
  return this._sdkConfig?.accountId ?? '';
1314
1371
  }
1315
1372
  /**
1316
- * Get enriched methods filtered by identity type
1373
+ * Get enriched methods filtered by the fallback identifier type.
1317
1374
  */
1318
1375
  getAvailableMethods() {
1319
1376
  if (!this._sdkConfig)
1320
1377
  return [];
1321
1378
  const methodIds = this._sdkConfig.methods ?? [];
1322
1379
  let methods = enrichMethods(methodIds);
1323
- // Filter by identity type
1380
+ // Filter by identifier type. Passkeys are SVID-scoped and remain
1381
+ // available regardless of whether the fallback identifier is email or phone.
1324
1382
  if (this._identityType === 'phone') {
1325
- methods = methods.filter((m) => m.type === 'totp' && m.id === 'totp_sms');
1383
+ methods = methods.filter((m) => m.type === 'passkey' || m.identityType === 'phone');
1326
1384
  }
1327
1385
  else {
1328
- methods = methods.filter((m) => m.identityType === 'email');
1386
+ methods = methods.filter((m) => m.identityType === 'email' || m.identityType === undefined);
1329
1387
  }
1330
1388
  return methods;
1331
1389
  }
@@ -1355,7 +1413,9 @@ class VerificationState {
1355
1413
  this._identityType = type;
1356
1414
  }
1357
1415
  getAllowedIdentityTypes() {
1358
- const allowed = this._sdkConfig?.allowedIdentityTypes ?? ['email'];
1416
+ if (!this._sdkConfig)
1417
+ return ['email'];
1418
+ const allowed = this._sdkConfig.allowedIdentityTypes ?? ['email'];
1359
1419
  return allowed;
1360
1420
  }
1361
1421
  // --- Auth Context (OIDC/simple mode) ---
@@ -1380,11 +1440,9 @@ class VerificationState {
1380
1440
  }
1381
1441
  /**
1382
1442
  * Check if we should show passkey registration prompt
1383
- * Based on: passkey enabled, user doesn't have one, not dismissed
1443
+ * Based on: passkey enabled, the current SVID doesn't have one, not dismissed
1384
1444
  */
1385
1445
  shouldShowPasskeyPrompt() {
1386
- if (this._identityType !== 'email')
1387
- return false;
1388
1446
  if (!this.isPasskeyEnabled())
1389
1447
  return false;
1390
1448
  if (this.isPasskeyPromptDismissed())
@@ -1468,7 +1526,7 @@ class PasskeyHandler {
1468
1526
  this.accountId = config.accountId;
1469
1527
  }
1470
1528
  /**
1471
- * Check if user has registered passkeys and validate email domain
1529
+ * Check if the current identifier resolves to an SVID with registered passkeys.
1472
1530
  * @returns Check result with emailValid and hasPasskey, or null on error
1473
1531
  */
1474
1532
  async checkPasskey() {
@@ -1508,14 +1566,17 @@ class PasskeyHandler {
1508
1566
  */
1509
1567
  async openPasskeyPopup(mode, registrationToken) {
1510
1568
  return new Promise((resolve) => {
1511
- // Build popup URL. Pass identity as `email` or `phone` (mutually
1512
- // exclusive) so the popup can render and POST the correct field.
1569
+ // Registration carries the freshly verified identifier so the backend
1570
+ // can attach the credential to that SVID. Verification is discoverable:
1571
+ // the credential userHandle routes completion to the SVID.
1513
1572
  const params = new URLSearchParams({
1514
1573
  mode,
1515
1574
  origin: window.location.origin,
1516
1575
  });
1517
- const identityParam = this.state.identityType === 'phone' ? 'phone' : 'email';
1518
- params.set(identityParam, this.state.recipient);
1576
+ if (mode === 'register' && this.state.recipient) {
1577
+ const identityParam = this.state.identityType === 'phone' ? 'phone' : 'email';
1578
+ params.set(identityParam, this.state.recipient);
1579
+ }
1519
1580
  // Add auth context for OIDC/simple mode flows
1520
1581
  const authContext = this.state.authContext;
1521
1582
  if (authContext?.authRequestId) {
@@ -1572,6 +1633,9 @@ class PasskeyHandler {
1572
1633
  token: data.data.token || '',
1573
1634
  identity: data.data.identity || this.state.recipient,
1574
1635
  identityType: data.data.identity_type || 'email',
1636
+ svid: data.data.svid,
1637
+ sessionId: data.data.session_id,
1638
+ refreshToken: data.data.refresh_token,
1575
1639
  redirect: data.data.redirect,
1576
1640
  },
1577
1641
  });
@@ -1614,12 +1678,7 @@ class PasskeyHandler {
1614
1678
  isValidMessageOrigin(origin) {
1615
1679
  try {
1616
1680
  const url = new URL(origin);
1617
- const hostname = url.hostname;
1618
- // Allow sparkvault.com and subdomains
1619
- if (hostname === 'sparkvault.com' || hostname.endsWith('.sparkvault.com')) {
1620
- return true;
1621
- }
1622
- return false;
1681
+ return url.protocol === 'https:' && url.origin === origin && !url.port && isSparkVaultOwnedHostname(url.hostname);
1623
1682
  }
1624
1683
  catch {
1625
1684
  return false;
@@ -1675,7 +1734,7 @@ class TotpHandler {
1675
1734
  this.state = state;
1676
1735
  }
1677
1736
  /**
1678
- * Send TOTP code via email or SMS
1737
+ * Send TOTP code via email, SMS, or voice
1679
1738
  */
1680
1739
  async send(method) {
1681
1740
  try {
@@ -1759,6 +1818,9 @@ class TotpHandler {
1759
1818
  token: response.token,
1760
1819
  identity: response.identity ?? this.state.recipient,
1761
1820
  identityType: response.identity_type ?? this.state.identityType,
1821
+ svid: response.svid,
1822
+ sessionId: response.session_id,
1823
+ refreshToken: response.refresh_token,
1762
1824
  redirect: response.redirect,
1763
1825
  },
1764
1826
  };
@@ -4099,26 +4161,33 @@ class MethodSelectView {
4099
4161
  this.methodClickHandlers = new Map();
4100
4162
  this.props = props;
4101
4163
  // Bind handlers once for proper removal
4102
- this.boundHandleBack = () => this.props.onBack();
4164
+ this.boundHandleBack = () => this.props.onBack?.();
4103
4165
  }
4104
4166
  render() {
4105
4167
  const container = div();
4106
- this.backLink = this.createBackLink();
4168
+ this.backLink = this.props.onBack ? this.createBackLink() : null;
4107
4169
  const title = document.createElement('h3');
4108
4170
  title.className = 'sv-title';
4109
4171
  title.textContent = 'Choose how to sign in';
4110
- const subtitle = document.createElement('p');
4111
- subtitle.className = 'sv-subtitle';
4112
- subtitle.textContent = escapeHtml(this.props.email);
4172
+ const email = this.props.email;
4173
+ const subtitle = email ? document.createElement('p') : null;
4174
+ if (subtitle && email) {
4175
+ subtitle.className = 'sv-subtitle';
4176
+ subtitle.textContent = escapeHtml(email);
4177
+ }
4113
4178
  const methodsContainer = div();
4114
4179
  for (const method of this.props.methods) {
4115
4180
  const methodButton = this.createMethodButton(method);
4116
4181
  this.methodButtons.push(methodButton);
4117
4182
  methodsContainer.appendChild(methodButton);
4118
4183
  }
4119
- container.appendChild(this.backLink);
4184
+ if (this.backLink) {
4185
+ container.appendChild(this.backLink);
4186
+ }
4120
4187
  container.appendChild(title);
4121
- container.appendChild(subtitle);
4188
+ if (subtitle) {
4189
+ container.appendChild(subtitle);
4190
+ }
4122
4191
  container.appendChild(methodsContainer);
4123
4192
  return container;
4124
4193
  }
@@ -4179,7 +4248,7 @@ class MethodSelectView {
4179
4248
  /**
4180
4249
  * TOTP Verification View
4181
4250
  *
4182
- * Collects and validates the 6-digit TOTP code sent via email or SMS.
4251
+ * Collects and validates the 6-digit TOTP code sent via email, SMS, or voice.
4183
4252
  */
4184
4253
  const CODE_LENGTH = 6;
4185
4254
  class TotpVerifyView {
@@ -4503,13 +4572,13 @@ class PasskeyView {
4503
4572
  this.fallbackLink = null;
4504
4573
  this.props = props;
4505
4574
  // Bind handlers once for proper removal
4506
- this.boundHandleBack = () => this.props.onBack();
4575
+ this.boundHandleBack = () => this.props.onBack?.();
4507
4576
  this.boundHandleStart = () => this.handleStart();
4508
4577
  this.boundHandleFallback = props.onFallback ? () => props.onFallback() : null;
4509
4578
  }
4510
4579
  render() {
4511
4580
  const container = div();
4512
- this.backLink = this.createBackLink();
4581
+ this.backLink = this.props.onBack ? this.createBackLink() : null;
4513
4582
  const iconContainer = div();
4514
4583
  iconContainer.appendChild(createPasskeyIcon());
4515
4584
  const title = document.createElement('h3');
@@ -4518,10 +4587,10 @@ class PasskeyView {
4518
4587
  const subtitle = document.createElement('p');
4519
4588
  subtitle.className = 'sv-subtitle';
4520
4589
  if (this.props.mode === 'register') {
4521
- subtitle.innerHTML = `Create a passkey for<br><strong>${escapeHtml(this.props.email)}</strong><br>to sign in faster next time`;
4590
+ subtitle.innerHTML = `Create a passkey for<br><strong>${escapeHtml(this.props.identityLabel)}</strong><br>to sign in faster next time`;
4522
4591
  }
4523
4592
  else {
4524
- subtitle.innerHTML = `Use your passkey to verify<br><strong>${escapeHtml(this.props.email)}</strong>`;
4593
+ subtitle.innerHTML = 'Use a passkey linked to your SparkVault Identity';
4525
4594
  }
4526
4595
  this.errorContainer = div();
4527
4596
  if (this.props.error) {
@@ -4538,7 +4607,9 @@ class PasskeyView {
4538
4607
  this.actionButton.className = 'sv-btn sv-btn-primary';
4539
4608
  this.actionButton.textContent = buttonText;
4540
4609
  this.actionButton.addEventListener('click', this.boundHandleStart);
4541
- container.appendChild(this.backLink);
4610
+ if (this.backLink) {
4611
+ container.appendChild(this.backLink);
4612
+ }
4542
4613
  container.appendChild(iconContainer);
4543
4614
  container.appendChild(title);
4544
4615
  container.appendChild(subtitle);
@@ -4554,7 +4625,7 @@ class PasskeyView {
4554
4625
  createFallbackLink() {
4555
4626
  const link = document.createElement('button');
4556
4627
  link.className = 'sv-fallback-link';
4557
- link.textContent = 'Having trouble? Use email code instead';
4628
+ link.textContent = 'Having trouble? Use a code instead';
4558
4629
  if (this.boundHandleFallback) {
4559
4630
  link.addEventListener('click', this.boundHandleFallback);
4560
4631
  }
@@ -4627,7 +4698,7 @@ class PasskeyPromptView {
4627
4698
  title.textContent = 'Add a passkey for faster sign-in?';
4628
4699
  const subtitle = document.createElement('p');
4629
4700
  subtitle.className = 'sv-subtitle';
4630
- subtitle.innerHTML = `Next time, sign in instantly as<br><strong>${escapeHtml(this.props.email)}</strong>`;
4701
+ subtitle.innerHTML = `Next time, sign in instantly as<br><strong>${escapeHtml(this.props.identityLabel)}</strong>`;
4631
4702
  // Error message if registration failed
4632
4703
  const errorContainer = div();
4633
4704
  if (this.props.error) {
@@ -5320,13 +5391,7 @@ class IdentityRenderer {
5320
5391
  ? config.branding.backdrop_blur
5321
5392
  : true;
5322
5393
  this.container.updateBackdropBlur(resolvedBackdropBlur);
5323
- // Proceed to appropriate view
5324
- if (this.recipient) {
5325
- this.showMethodSelect();
5326
- }
5327
- else {
5328
- this.showIdentityInput();
5329
- }
5394
+ await this.showInitialAuthStep();
5330
5395
  }
5331
5396
  catch (error) {
5332
5397
  this.setState(this.viewStateForConfigError(error));
@@ -5383,12 +5448,7 @@ class IdentityRenderer {
5383
5448
  ? config.branding.backdrop_blur
5384
5449
  : true;
5385
5450
  this.container.updateBackdropBlur(resolvedBackdropBlur);
5386
- if (this.recipient) {
5387
- this.showMethodSelect();
5388
- }
5389
- else {
5390
- this.showIdentityInput();
5391
- }
5451
+ await this.showInitialAuthStep();
5392
5452
  }
5393
5453
  catch (error) {
5394
5454
  this.setState(this.viewStateForConfigError(error));
@@ -5420,6 +5480,13 @@ class IdentityRenderer {
5420
5480
  this.verificationState.setViewState(state);
5421
5481
  this.render();
5422
5482
  }
5483
+ async showInitialAuthStep() {
5484
+ if (this.recipient || this.getAllowedIdentityTypes().length === 0) {
5485
+ await this.showMethodSelect();
5486
+ return;
5487
+ }
5488
+ this.showIdentityInput();
5489
+ }
5423
5490
  render() {
5424
5491
  const body = this.container.getBody();
5425
5492
  if (!body)
@@ -5468,7 +5535,7 @@ class IdentityRenderer {
5468
5535
  email: state.email,
5469
5536
  methods: state.methods,
5470
5537
  onSelectMethod: (method) => this.handleMethodSelect(method),
5471
- onBack: () => this.showIdentityInput(),
5538
+ onBack: state.email ? () => this.showIdentityInput() : undefined,
5472
5539
  });
5473
5540
  case 'totp-verify':
5474
5541
  return new TotpVerifyView({
@@ -5485,7 +5552,7 @@ class IdentityRenderer {
5485
5552
  const { passkey } = this.verificationState;
5486
5553
  const isFromPrompt = state.mode === 'register' && passkey.pendingResult !== null;
5487
5554
  return new PasskeyView({
5488
- email: state.email,
5555
+ identityLabel: state.recipient,
5489
5556
  mode: state.mode,
5490
5557
  error: state.error,
5491
5558
  onStart: () => this.handlePasskeyStart(),
@@ -5493,7 +5560,11 @@ class IdentityRenderer {
5493
5560
  ? () => this.handlePasskeyRegistrationSkip()
5494
5561
  : state.mode === 'verify'
5495
5562
  // From passkey verify, enable fallback so showMethodSelect doesn't loop back
5496
- ? () => this.handlePasskeyFallback()
5563
+ ? this.passkeyFallbackMethodsAvailable()
5564
+ ? () => this.handlePasskeyFallback()
5565
+ : this.recipient
5566
+ ? () => this.showIdentityInput()
5567
+ : undefined
5497
5568
  : () => this.showMethodSelect(),
5498
5569
  onFallback: passkey.hasPasskey && !passkey.isFallbackMode
5499
5570
  ? () => this.handlePasskeyFallback()
@@ -5502,7 +5573,7 @@ class IdentityRenderer {
5502
5573
  }
5503
5574
  case 'passkey-prompt':
5504
5575
  return new PasskeyPromptView({
5505
- email: state.email,
5576
+ identityLabel: state.recipient,
5506
5577
  onAddPasskey: () => this.handlePasskeyPromptAdd(state.pendingResult),
5507
5578
  onSkip: () => this.handlePasskeyPromptSkip(state.pendingResult),
5508
5579
  error: state.error,
@@ -5554,45 +5625,22 @@ class IdentityRenderer {
5554
5625
  }
5555
5626
  // Enrich method IDs with metadata from static registry
5556
5627
  let methods = enrichMethods(methodIds);
5557
- // Filter by identity type (phone users can only use SMS TOTP)
5558
- methods = methods.filter((m) => {
5559
- if (this.identityType === 'phone') {
5560
- // Phone users can only use SMS TOTP
5561
- return m.type === 'totp' && m.id === 'totp_sms';
5562
- }
5563
- // Email users can use email-based methods
5564
- return m.identityType === 'email' || m.identityType === undefined;
5565
- });
5566
- if (methods.length === 0) {
5567
- this.setState({
5568
- view: 'error',
5569
- message: 'No authentication methods available',
5570
- code: 'no_methods',
5628
+ if (this.recipient) {
5629
+ // Filter by identifier type. Passkeys are SVID-scoped, so they remain
5630
+ // available regardless of whether the fallback identifier is email or phone.
5631
+ methods = methods.filter((m) => {
5632
+ if (this.identityType === 'phone') {
5633
+ return m.type === 'passkey' || m.identityType === 'phone';
5634
+ }
5635
+ // Email users can use email-based methods
5636
+ return m.identityType === 'email' || m.identityType === undefined;
5571
5637
  });
5572
- return;
5573
5638
  }
5574
- // Check if passkeys are enabled and user is email-based
5575
- const hasPasskeyMethod = methods.some((m) => m.type === 'passkey');
5576
- const { passkey } = this.verificationState;
5577
- if (hasPasskeyMethod && this.identityType === 'email' && !passkey.isFallbackMode) {
5578
- this.setState({ view: 'loading' });
5579
- // Use passkey handler for the check (also validates email domain)
5580
- const checkResult = await this.passkeyHandler.checkPasskey();
5581
- // Handle invalid email domain
5582
- if (checkResult !== null && !checkResult.emailValid) {
5583
- this.showIdentityInput('This email domain cannot receive messages. Please check your email address.');
5584
- return;
5585
- }
5586
- if (checkResult?.hasPasskey === true) {
5587
- // User has passkey - go straight to passkey verification
5588
- this.setState({
5589
- view: 'passkey',
5590
- email: this.recipient,
5591
- mode: 'verify',
5592
- });
5593
- return;
5594
- }
5595
- // User doesn't have passkey - filter out passkey option
5639
+ else {
5640
+ // No PII prompt is needed when only SVID-routed methods are enabled.
5641
+ methods = methods.filter((m) => m.type === 'passkey' || m.type === 'social');
5642
+ }
5643
+ if (this.verificationState.passkey.isFallbackMode) {
5596
5644
  methods = methods.filter((m) => m.type !== 'passkey');
5597
5645
  }
5598
5646
  if (methods.length === 0) {
@@ -5624,7 +5672,11 @@ class IdentityRenderer {
5624
5672
  try {
5625
5673
  switch (method.type) {
5626
5674
  case 'totp': {
5627
- const totpMethod = method.id === 'totp_sms' ? 'sms' : 'email';
5675
+ const totpMethod = method.id === 'totp_sms'
5676
+ ? 'sms'
5677
+ : method.id === 'totp_voice'
5678
+ ? 'voice'
5679
+ : 'email';
5628
5680
  this.setState({ view: 'loading' });
5629
5681
  const result = await this.totpHandler.send(totpMethod);
5630
5682
  if (!result.success) {
@@ -5646,7 +5698,7 @@ class IdentityRenderer {
5646
5698
  case 'passkey': {
5647
5699
  this.setState({
5648
5700
  view: 'passkey',
5649
- email: this.recipient,
5701
+ recipient: this.recipient,
5650
5702
  mode: 'verify',
5651
5703
  });
5652
5704
  break;
@@ -5719,7 +5771,7 @@ class IdentityRenderer {
5719
5771
  if (!result.result.redirect && await this.shouldShowPasskeyPrompt()) {
5720
5772
  this.setState({
5721
5773
  view: 'passkey-prompt',
5722
- email: this.recipient,
5774
+ recipient: this.recipient,
5723
5775
  pendingResult: result.result,
5724
5776
  });
5725
5777
  return;
@@ -5821,7 +5873,7 @@ class IdentityRenderer {
5821
5873
  if (result.errorType === 'not_found') {
5822
5874
  this.setState({
5823
5875
  view: 'passkey',
5824
- email: this.recipient,
5876
+ recipient: this.recipient,
5825
5877
  mode: 'register',
5826
5878
  error: result.error,
5827
5879
  });
@@ -5829,7 +5881,7 @@ class IdentityRenderer {
5829
5881
  else {
5830
5882
  this.setState({
5831
5883
  view: 'passkey',
5832
- email: this.recipient,
5884
+ recipient: this.recipient,
5833
5885
  mode: currentMode,
5834
5886
  error: result.error,
5835
5887
  });
@@ -5837,13 +5889,28 @@ class IdentityRenderer {
5837
5889
  }
5838
5890
  }
5839
5891
  /**
5840
- * Handle fallback from passkey to email/SMS code.
5841
- * User clicked "Having trouble? Use email code instead".
5892
+ * Handle fallback from passkey to an identifier-based code.
5893
+ * User clicked "Having trouble? Use a code instead".
5842
5894
  */
5843
5895
  async handlePasskeyFallback() {
5844
5896
  this.verificationState.enablePasskeyFallback();
5845
5897
  await this.showMethodSelect();
5846
5898
  }
5899
+ passkeyFallbackMethodsAvailable() {
5900
+ if (!this.sdkConfig)
5901
+ return false;
5902
+ const methods = enrichMethods(this.sdkConfig.methods ?? []);
5903
+ if (!this.recipient) {
5904
+ return methods.some((m) => m.type === 'social');
5905
+ }
5906
+ return methods.some((m) => {
5907
+ if (m.type === 'passkey')
5908
+ return false;
5909
+ if (this.identityType === 'phone')
5910
+ return m.identityType === 'phone';
5911
+ return m.identityType === 'email';
5912
+ });
5913
+ }
5847
5914
  /**
5848
5915
  * Handle skipping passkey registration when coming from passkey prompt.
5849
5916
  * User clicked "Back" or cancelled registration after being prompted.
@@ -5921,7 +5988,7 @@ class IdentityRenderer {
5921
5988
  const { passkey } = this.verificationState;
5922
5989
  if (passkey.hasPasskey === null) {
5923
5990
  const checkResult = await this.passkeyHandler.checkPasskey();
5924
- // Only prompt if email is valid and user doesn't have a passkey
5991
+ // Only prompt if the verified identifier resolves to an SVID without a passkey.
5925
5992
  return checkResult !== null && checkResult.emailValid && !checkResult.hasPasskey;
5926
5993
  }
5927
5994
  return !passkey.hasPasskey;
@@ -5949,7 +6016,7 @@ class IdentityRenderer {
5949
6016
  // User cancelled browser dialog - go back to prompt so they can skip or try again
5950
6017
  this.setState({
5951
6018
  view: 'passkey-prompt',
5952
- email: this.recipient,
6019
+ recipient: this.recipient,
5953
6020
  pendingResult,
5954
6021
  });
5955
6022
  }
@@ -5958,7 +6025,7 @@ class IdentityRenderer {
5958
6025
  const errorMessage = result.error || 'Failed to create passkey. Please try again.';
5959
6026
  this.setState({
5960
6027
  view: 'passkey-prompt',
5961
- email: this.recipient,
6028
+ recipient: this.recipient,
5962
6029
  pendingResult,
5963
6030
  error: errorMessage,
5964
6031
  });
@@ -6083,6 +6150,9 @@ class IdentityRenderer {
6083
6150
  token: data.token,
6084
6151
  identity: data.identity,
6085
6152
  identityType: data.identityType || 'email',
6153
+ svid: typeof data.svid === 'string' ? data.svid : undefined,
6154
+ sessionId: typeof data.session_id === 'string' ? data.session_id : undefined,
6155
+ refreshToken: typeof data.refresh_token === 'string' ? data.refresh_token : undefined,
6086
6156
  // For simple-mode flows the server also emits a signed
6087
6157
  // success-callback URL; without it the SDK has nowhere to send
6088
6158
  // the user after WebSocket success.
@@ -6129,7 +6199,7 @@ class IdentityRenderer {
6129
6199
  if (!result.redirect && await this.shouldShowPasskeyPrompt()) {
6130
6200
  this.setState({
6131
6201
  view: 'passkey-prompt',
6132
- email: this.recipient,
6202
+ recipient: this.recipient,
6133
6203
  pendingResult: result,
6134
6204
  });
6135
6205
  return;
@@ -6521,6 +6591,11 @@ class IdentityModule {
6521
6591
  * });
6522
6592
  */
6523
6593
  async verify(options = {}) {
6594
+ const redirectResult = await this.consumeRedirectResult();
6595
+ if (redirectResult) {
6596
+ options.onSuccess?.(redirectResult);
6597
+ return redirectResult;
6598
+ }
6524
6599
  if (this.renderer) {
6525
6600
  this.renderer.close();
6526
6601
  }
@@ -6632,8 +6707,19 @@ class IdentityModule {
6632
6707
  observer?.observe(document.body, { childList: true, subtree: true });
6633
6708
  });
6634
6709
  };
6635
- // Defer attachment until DOM is ready
6636
- onDomReady(attachToElements);
6710
+ // Defer attachment until DOM is ready, then complete any full-page
6711
+ // social-login redirect result before the user has to click again.
6712
+ onDomReady(() => {
6713
+ attachToElements();
6714
+ this.consumeRedirectResult()
6715
+ .then((result) => {
6716
+ if (result)
6717
+ options.onSuccess?.(result);
6718
+ })
6719
+ .catch((error) => {
6720
+ options.onError?.(error instanceof Error ? error : new Error(String(error)));
6721
+ });
6722
+ });
6637
6723
  // Return cleanup function
6638
6724
  return () => {
6639
6725
  observer?.disconnect();
@@ -6671,6 +6757,34 @@ class IdentityModule {
6671
6757
  }
6672
6758
  return payload;
6673
6759
  }
6760
+ /**
6761
+ * Complete full-page redirect results produced by social-login flows.
6762
+ * The Identity Product redirects back with `#token=...` so the token does
6763
+ * not hit server logs. Consume it once, validate the issuer/expiry shape,
6764
+ * then remove it from the URL bar.
6765
+ */
6766
+ async consumeRedirectResult() {
6767
+ if (typeof window === 'undefined' || !window.location.hash) {
6768
+ return null;
6769
+ }
6770
+ const params = new URLSearchParams(window.location.hash.slice(1));
6771
+ const token = params.get('token');
6772
+ if (!token) {
6773
+ return null;
6774
+ }
6775
+ const claims = await this.verifyToken(token);
6776
+ const result = {
6777
+ token,
6778
+ identity: claims.identity,
6779
+ identityType: claims.identity_type,
6780
+ svid: claims.svid,
6781
+ sessionId: claims.session_id,
6782
+ };
6783
+ if (typeof window.history?.replaceState === 'function') {
6784
+ window.history.replaceState(window.history.state, document.title, `${window.location.pathname}${window.location.search}`);
6785
+ }
6786
+ return result;
6787
+ }
6674
6788
  /**
6675
6789
  * Close the identity dialog/inline UI if open.
6676
6790
  */