@proveanything/smartlinks-auth-ui 0.5.10 → 0.5.12

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.
package/dist/index.js CHANGED
@@ -12978,13 +12978,18 @@ const getActionResultErrorMessage = (result) => {
12978
12978
  details: candidate.details,
12979
12979
  });
12980
12980
  };
12981
+ // ----- WhatsApp reply helpers -----
12982
+ const WHATSAPP_PENDING_SESSION_KEY = 'whatsapp_pending_session';
12981
12983
  const interpolateReply = (input, vars) => {
12982
12984
  if (!input)
12983
12985
  return input;
12984
12986
  return input
12985
12987
  .replace(/\{\{\s*name\s*\}\}/gi, vars.name?.trim() || 'there')
12986
12988
  .replace(/\{\{\s*clientName\s*\}\}/gi, vars.clientName?.trim() || '')
12987
- .replace(/\{\{\s*phoneNumber\s*\}\}/gi, vars.phoneNumber?.trim() || '');
12989
+ .replace(/\{\{\s*phoneNumber\s*\}\}/gi, vars.phoneNumber?.trim() || '')
12990
+ .replace(/\{\{\s*returnUrl\s*\}\}/gi, vars.returnUrl?.trim() || '')
12991
+ .replace(/\{\{\s*clientId\s*\}\}/gi, vars.clientId?.trim() || '')
12992
+ .replace(/\{\{\s*token\s*\}\}/gi, vars.token?.trim() || '');
12988
12993
  };
12989
12994
  const buildWhatsAppReply = (cfg, vars) => {
12990
12995
  if (!cfg)
@@ -13310,6 +13315,24 @@ const SmartlinksAuthUI = ({ apiEndpoint, clientId, clientName, accountData, onAu
13310
13315
  // Get the full current URL including hash routes, strip query params
13311
13316
  return window.location.href.split('?')[0];
13312
13317
  };
13318
+ const savePendingWhatsAppSession = async (session) => {
13319
+ if (proxyMode)
13320
+ return;
13321
+ const storage = await getStorage();
13322
+ await storage.setItem(WHATSAPP_PENDING_SESSION_KEY, session);
13323
+ };
13324
+ const loadPendingWhatsAppSession = async () => {
13325
+ if (proxyMode)
13326
+ return null;
13327
+ const storage = await getStorage();
13328
+ return storage.getItem(WHATSAPP_PENDING_SESSION_KEY);
13329
+ };
13330
+ const clearPendingWhatsAppSession = async () => {
13331
+ if (proxyMode)
13332
+ return;
13333
+ const storage = await getStorage();
13334
+ await storage.removeItem(WHATSAPP_PENDING_SESSION_KEY);
13335
+ };
13313
13336
  // Fetch UI configuration
13314
13337
  React.useEffect(() => {
13315
13338
  // Wait for SDK to be ready before fetching config
@@ -14399,6 +14422,48 @@ const SmartlinksAuthUI = ({ apiEndpoint, clientId, clientName, accountData, onAu
14399
14422
  };
14400
14423
  // Track the most recent WhatsApp send so we can exchange its sessionKey for a real login session.
14401
14424
  const whatsappSendRef = React.useRef(null);
14425
+ React.useEffect(() => {
14426
+ if (proxyMode)
14427
+ return;
14428
+ let cancelled = false;
14429
+ const resumePendingWhatsAppSession = async () => {
14430
+ try {
14431
+ const pending = await loadPendingWhatsAppSession();
14432
+ if (!pending || cancelled)
14433
+ return;
14434
+ // Expire stale pending sessions after 30 minutes to avoid resurrecting old attempts.
14435
+ if (Date.now() - pending.createdAt > 30 * 60 * 1000) {
14436
+ await clearPendingWhatsAppSession();
14437
+ return;
14438
+ }
14439
+ whatsappSendRef.current = {
14440
+ token: pending.token,
14441
+ sessionKey: pending.sessionKey,
14442
+ displayName: pending.displayName,
14443
+ };
14444
+ const status = await api.getWhatsAppStatus(pending.token);
14445
+ if (cancelled)
14446
+ return;
14447
+ if (status.verified) {
14448
+ await handleWhatsAppVerified(status);
14449
+ return;
14450
+ }
14451
+ if (status.status === 'pending') {
14452
+ setMode('whatsapp');
14453
+ }
14454
+ else if (status.status === 'failed' || status.status === 'expired' || status.status === 'unknown') {
14455
+ await clearPendingWhatsAppSession();
14456
+ }
14457
+ }
14458
+ catch (err) {
14459
+ log.warn('Failed to resume pending WhatsApp session:', err);
14460
+ }
14461
+ };
14462
+ resumePendingWhatsAppSession();
14463
+ return () => {
14464
+ cancelled = true;
14465
+ };
14466
+ }, [proxyMode, clientId]);
14402
14467
  const handleWhatsAppSend = async (displayName) => {
14403
14468
  setLoading(true);
14404
14469
  setError(undefined);
@@ -14407,9 +14472,12 @@ const SmartlinksAuthUI = ({ apiEndpoint, clientId, clientName, accountData, onAu
14407
14472
  // account from the inbound WhatsApp webhook (sender's number is the proof).
14408
14473
  // Resolve reply config: per-call prop wins, otherwise fall back to AuthKit default.
14409
14474
  const replyConfig = whatsappReply ?? config?.whatsappReply;
14475
+ const effectiveRedirectUrl = getRedirectUrl();
14410
14476
  const reply = buildWhatsAppReply(replyConfig, {
14411
14477
  name: displayName,
14412
14478
  clientName,
14479
+ returnUrl: effectiveRedirectUrl,
14480
+ clientId,
14413
14481
  });
14414
14482
  // Resolve outbound prefill message: per-call prop wins, then AuthKit default.
14415
14483
  const prefillMessage = whatsappPrefillMessage ?? config?.whatsappPrefillMessage;
@@ -14418,12 +14486,19 @@ const SmartlinksAuthUI = ({ apiEndpoint, clientId, clientName, accountData, onAu
14418
14486
  // formed session.user — no follow-up updateProfile call needed.
14419
14487
  const trimmedName = displayName?.trim() || undefined;
14420
14488
  const contactData = trimmedName ? { name: trimmedName } : undefined;
14421
- const result = await api.sendWhatsApp(getRedirectUrl(), undefined, reply, prefillMessage, contactData);
14489
+ const result = await api.sendWhatsApp(effectiveRedirectUrl, undefined, reply, prefillMessage, contactData);
14422
14490
  whatsappSendRef.current = {
14423
14491
  token: result.token,
14424
14492
  sessionKey: result.sessionKey,
14425
14493
  displayName: trimmedName,
14426
14494
  };
14495
+ await savePendingWhatsAppSession({
14496
+ token: result.token,
14497
+ sessionKey: result.sessionKey,
14498
+ displayName: trimmedName,
14499
+ redirectUrl: effectiveRedirectUrl,
14500
+ createdAt: Date.now(),
14501
+ });
14427
14502
  return result;
14428
14503
  }
14429
14504
  catch (err) {
@@ -14454,12 +14529,15 @@ const SmartlinksAuthUI = ({ apiEndpoint, clientId, clientName, accountData, onAu
14454
14529
  if (!proxyMode) {
14455
14530
  onAuthSuccess(session.token, session.user, session.accountData);
14456
14531
  }
14532
+ await clearPendingWhatsAppSession();
14533
+ return;
14457
14534
  }
14458
14535
  }
14459
14536
  catch (err) {
14460
14537
  log.warn('WhatsApp session exchange failed, falling back to redirect-only flow:', err);
14461
14538
  }
14462
14539
  }
14540
+ await clearPendingWhatsAppSession();
14463
14541
  performRedirect(target, 'magic-link');
14464
14542
  };
14465
14543
  // Show processing state for URL-based auth (verification, magic link, password reset)