@proveanything/smartlinks-auth-ui 0.5.15 → 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.
package/dist/index.js CHANGED
@@ -13108,8 +13108,48 @@ const useAuth = () => {
13108
13108
  };
13109
13109
 
13110
13110
  // VERSION: Update this when making changes to help identify which version is running
13111
- const AUTH_UI_VERSION = '46';
13111
+ const AUTH_UI_VERSION = '47';
13112
13112
  const LOG_PREFIX = `[SmartlinksAuthUI:v${AUTH_UI_VERSION}]`;
13113
+ const PERMANENT_WHATSAPP_EXCHANGE_ERROR_CODES = new Set([
13114
+ 'TOKEN_ALREADY_USED',
13115
+ 'SESSION_ALREADY_USED',
13116
+ 'WHATSAPP_SESSION_ALREADY_USED',
13117
+ 'INVALID_TOKEN',
13118
+ 'TOKEN_EXPIRED',
13119
+ 'INVALID_SESSION',
13120
+ 'SESSION_EXPIRED',
13121
+ 'SESSION_NOT_FOUND',
13122
+ ]);
13123
+ const getExchangeErrorCode = (error) => {
13124
+ if (!error || typeof error !== 'object')
13125
+ return undefined;
13126
+ const err = error;
13127
+ return err.errorCode || err.details?.errorCode || err.details?.error || err.response?.data?.errorCode || err.response?.data?.error;
13128
+ };
13129
+ const getExchangeErrorStatus = (error) => {
13130
+ if (!error || typeof error !== 'object')
13131
+ return undefined;
13132
+ const err = error;
13133
+ return err.statusCode || err.status || err.response?.status;
13134
+ };
13135
+ const getExchangeErrorMessage = (error) => {
13136
+ if (error instanceof Error)
13137
+ return error.message || '';
13138
+ if (!error || typeof error !== 'object')
13139
+ return '';
13140
+ const err = error;
13141
+ return err.message || err.details?.message || err.response?.data?.message || '';
13142
+ };
13143
+ const isPermanentWhatsAppExchangeError = (error) => {
13144
+ const errorCode = getExchangeErrorCode(error)?.toUpperCase();
13145
+ if (errorCode && PERMANENT_WHATSAPP_EXCHANGE_ERROR_CODES.has(errorCode)) {
13146
+ return true;
13147
+ }
13148
+ const status = getExchangeErrorStatus(error);
13149
+ const message = getExchangeErrorMessage(error).toLowerCase();
13150
+ const looksPermanentMessage = /(already used|already been used|session.*used|expired|invalid|not found|consumed)/i.test(message);
13151
+ return looksPermanentMessage && [400, 401, 404, 409, 410].includes(status ?? -1);
13152
+ };
13113
13153
  // Normalize malformed query strings where a second '?' is used instead of '&'.
13114
13154
  // Some host platforms append "?mode=...&token=..." to a URL that already has a "?pageId=...",
13115
13155
  // resulting in "?pageId=xxx?mode=resetPassword&token=yyy". Convert any extra '?' to '&'
@@ -14811,6 +14851,10 @@ const SmartlinksAuthUI = ({ apiEndpoint, clientId, clientName, accountData, onAu
14811
14851
  try {
14812
14852
  await updatePendingWhatsAppSession({ exchangeStartedAt: Date.now() });
14813
14853
  const session = await api.exchangeWhatsAppSession(send.token, send.sessionKey);
14854
+ const resultErrorMessage = getActionResultErrorMessage(session);
14855
+ if (resultErrorMessage) {
14856
+ throw Object.assign(new Error(resultErrorMessage), session);
14857
+ }
14814
14858
  if (session?.token && session.user) {
14815
14859
  await auth.login(session.token, session.user, session.accountData, true, getExpirationFromResponse(session));
14816
14860
  if (!proxyMode) {
@@ -14823,6 +14867,15 @@ const SmartlinksAuthUI = ({ apiEndpoint, clientId, clientName, accountData, onAu
14823
14867
  }
14824
14868
  }
14825
14869
  catch (err) {
14870
+ if (isPermanentWhatsAppExchangeError(err)) {
14871
+ log.warn('WhatsApp session exchange failed permanently; clearing stale pending session:', err);
14872
+ setAuthSuccess(false);
14873
+ setSuccessMessage(undefined);
14874
+ setError('This WhatsApp sign-in session has already been used or expired. Please start again.');
14875
+ setRestoredWhatsAppSend(null);
14876
+ await clearPendingWhatsAppSession();
14877
+ return true;
14878
+ }
14826
14879
  const latestPending = await loadPendingWhatsAppSession();
14827
14880
  if (!latestPending) {
14828
14881
  return true;