@proveanything/smartlinks-auth-ui 0.5.14 → 0.5.16

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.
@@ -1 +1 @@
1
- {"version":3,"file":"SmartlinksAuthUI.d.ts","sourceRoot":"","sources":["../../src/components/SmartlinksAuthUI.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA+C,MAAM,OAAO,CAAC;AAcpE,OAAO,KAAK,EAAE,qBAAqB,EAAyF,MAAM,UAAU,CAAC;AA6S7I,QAAA,MAAM,mBAAmB,QAAa,OAAO,CAAC,IAAI,CAqBjD,CAAC;AAqDF,OAAO,EAAE,mBAAmB,EAAE,CAAC;AAI/B,eAAO,MAAM,gBAAgB,EAAE,KAAK,CAAC,EAAE,CAAC,qBAAqB,CA6qE5D,CAAC"}
1
+ {"version":3,"file":"SmartlinksAuthUI.d.ts","sourceRoot":"","sources":["../../src/components/SmartlinksAuthUI.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA+C,MAAM,OAAO,CAAC;AAcpE,OAAO,KAAK,EAAE,qBAAqB,EAAyF,MAAM,UAAU,CAAC;AA2V7I,QAAA,MAAM,mBAAmB,QAAa,OAAO,CAAC,IAAI,CAqBjD,CAAC;AAqDF,OAAO,EAAE,mBAAmB,EAAE,CAAC;AAI/B,eAAO,MAAM,gBAAgB,EAAE,KAAK,CAAC,EAAE,CAAC,qBAAqB,CA4rE5D,CAAC"}
package/dist/index.esm.js CHANGED
@@ -13088,8 +13088,48 @@ const useAuth = () => {
13088
13088
  };
13089
13089
 
13090
13090
  // VERSION: Update this when making changes to help identify which version is running
13091
- const AUTH_UI_VERSION = '46';
13091
+ const AUTH_UI_VERSION = '47';
13092
13092
  const LOG_PREFIX = `[SmartlinksAuthUI:v${AUTH_UI_VERSION}]`;
13093
+ const PERMANENT_WHATSAPP_EXCHANGE_ERROR_CODES = new Set([
13094
+ 'TOKEN_ALREADY_USED',
13095
+ 'SESSION_ALREADY_USED',
13096
+ 'WHATSAPP_SESSION_ALREADY_USED',
13097
+ 'INVALID_TOKEN',
13098
+ 'TOKEN_EXPIRED',
13099
+ 'INVALID_SESSION',
13100
+ 'SESSION_EXPIRED',
13101
+ 'SESSION_NOT_FOUND',
13102
+ ]);
13103
+ const getExchangeErrorCode = (error) => {
13104
+ if (!error || typeof error !== 'object')
13105
+ return undefined;
13106
+ const err = error;
13107
+ return err.errorCode || err.details?.errorCode || err.details?.error || err.response?.data?.errorCode || err.response?.data?.error;
13108
+ };
13109
+ const getExchangeErrorStatus = (error) => {
13110
+ if (!error || typeof error !== 'object')
13111
+ return undefined;
13112
+ const err = error;
13113
+ return err.statusCode || err.status || err.response?.status;
13114
+ };
13115
+ const getExchangeErrorMessage = (error) => {
13116
+ if (error instanceof Error)
13117
+ return error.message || '';
13118
+ if (!error || typeof error !== 'object')
13119
+ return '';
13120
+ const err = error;
13121
+ return err.message || err.details?.message || err.response?.data?.message || '';
13122
+ };
13123
+ const isPermanentWhatsAppExchangeError = (error) => {
13124
+ const errorCode = getExchangeErrorCode(error)?.toUpperCase();
13125
+ if (errorCode && PERMANENT_WHATSAPP_EXCHANGE_ERROR_CODES.has(errorCode)) {
13126
+ return true;
13127
+ }
13128
+ const status = getExchangeErrorStatus(error);
13129
+ const message = getExchangeErrorMessage(error).toLowerCase();
13130
+ const looksPermanentMessage = /(already used|already been used|session.*used|expired|invalid|not found|consumed)/i.test(message);
13131
+ return looksPermanentMessage && [400, 401, 404, 409, 410].includes(status ?? -1);
13132
+ };
13093
13133
  // Normalize malformed query strings where a second '?' is used instead of '&'.
13094
13134
  // Some host platforms append "?mode=...&token=..." to a URL that already has a "?pageId=...",
13095
13135
  // resulting in "?pageId=xxx?mode=resetPassword&token=yyy". Convert any extra '?' to '&'
@@ -13112,7 +13152,10 @@ const appendWhatsAppResumeParams = (url, token) => {
13112
13152
  else {
13113
13153
  nextUrl.searchParams.delete('token');
13114
13154
  }
13115
- return nextUrl.toString();
13155
+ const serializedUrl = nextUrl.toString();
13156
+ return token === '{{token}}'
13157
+ ? serializedUrl.replace(encodeURIComponent(token), token)
13158
+ : serializedUrl;
13116
13159
  }
13117
13160
  catch {
13118
13161
  return url;
@@ -13182,7 +13225,7 @@ const interpolateReply = (input, vars) => {
13182
13225
  .replace(/\{\{\s*phoneNumber\s*\}\}/gi, vars.phoneNumber?.trim() || '')
13183
13226
  .replace(/\{\{\s*returnUrl\s*\}\}/gi, vars.returnUrl?.trim() || '')
13184
13227
  .replace(/\{\{\s*clientId\s*\}\}/gi, vars.clientId?.trim() || '')
13185
- .replace(/\{\{\s*token\s*\}\}/gi, vars.token?.trim() || '');
13228
+ .replace(/\{\{\s*token\s*\}\}/gi, vars.token === undefined ? '{{token}}' : vars.token.trim());
13186
13229
  };
13187
13230
  const buildWhatsAppReply = (cfg, vars) => {
13188
13231
  if (!cfg)
@@ -14731,10 +14774,11 @@ const SmartlinksAuthUI = ({ apiEndpoint, clientId, clientName, accountData, onAu
14731
14774
  // Resolve reply config: per-call prop wins, otherwise fall back to AuthKit default.
14732
14775
  const replyConfig = whatsappReply ?? config?.whatsappReply;
14733
14776
  const effectiveRedirectUrl = getRedirectUrl();
14777
+ const resumableRedirectUrl = appendWhatsAppResumeParams(effectiveRedirectUrl, '{{token}}');
14734
14778
  const reply = buildWhatsAppReply(replyConfig, {
14735
14779
  name: displayName,
14736
14780
  clientName,
14737
- returnUrl: effectiveRedirectUrl,
14781
+ returnUrl: resumableRedirectUrl,
14738
14782
  clientId,
14739
14783
  });
14740
14784
  // Resolve outbound prefill message: per-call prop wins, then AuthKit default.
@@ -14744,7 +14788,7 @@ const SmartlinksAuthUI = ({ apiEndpoint, clientId, clientName, accountData, onAu
14744
14788
  // formed session.user — no follow-up updateProfile call needed.
14745
14789
  const trimmedName = displayName?.trim() || undefined;
14746
14790
  const contactData = trimmedName ? { name: trimmedName } : undefined;
14747
- const result = await api.sendWhatsApp(effectiveRedirectUrl, undefined, reply, prefillMessage, contactData);
14791
+ const result = await api.sendWhatsApp(resumableRedirectUrl, undefined, reply, prefillMessage, contactData);
14748
14792
  whatsappSendRef.current = {
14749
14793
  token: result.token,
14750
14794
  sessionKey: result.sessionKey,
@@ -14787,6 +14831,10 @@ const SmartlinksAuthUI = ({ apiEndpoint, clientId, clientName, accountData, onAu
14787
14831
  try {
14788
14832
  await updatePendingWhatsAppSession({ exchangeStartedAt: Date.now() });
14789
14833
  const session = await api.exchangeWhatsAppSession(send.token, send.sessionKey);
14834
+ const resultErrorMessage = getActionResultErrorMessage(session);
14835
+ if (resultErrorMessage) {
14836
+ throw Object.assign(new Error(resultErrorMessage), session);
14837
+ }
14790
14838
  if (session?.token && session.user) {
14791
14839
  await auth.login(session.token, session.user, session.accountData, true, getExpirationFromResponse(session));
14792
14840
  if (!proxyMode) {
@@ -14799,6 +14847,15 @@ const SmartlinksAuthUI = ({ apiEndpoint, clientId, clientName, accountData, onAu
14799
14847
  }
14800
14848
  }
14801
14849
  catch (err) {
14850
+ if (isPermanentWhatsAppExchangeError(err)) {
14851
+ log.warn('WhatsApp session exchange failed permanently; clearing stale pending session:', err);
14852
+ setAuthSuccess(false);
14853
+ setSuccessMessage(undefined);
14854
+ setError('This WhatsApp sign-in session has already been used or expired. Please start again.');
14855
+ setRestoredWhatsAppSend(null);
14856
+ await clearPendingWhatsAppSession();
14857
+ return true;
14858
+ }
14802
14859
  const latestPending = await loadPendingWhatsAppSession();
14803
14860
  if (!latestPending) {
14804
14861
  return true;