@proveanything/smartlinks-auth-ui 0.4.3 → 0.4.5

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
@@ -12421,8 +12421,32 @@ function getFriendlyErrorMessage(error) {
12421
12421
  // Fall back to the server's message (already human-readable from backend)
12422
12422
  return error.message;
12423
12423
  }
12424
- // Handle standard Error objects — check message for known patterns
12424
+ // Handle standard Error objects
12425
12425
  if (error instanceof Error) {
12426
+ // SDK bug workaround: SDK may do `throw new Error(responseBodyObject)` which produces
12427
+ // message "[object Object]". Check for API error properties attached to the Error instance.
12428
+ const errAny = error;
12429
+ // Check if the Error has API error properties directly attached (e.g., error.statusCode, error.errorCode)
12430
+ if (typeof errAny.statusCode === 'number' || errAny.errorCode || errAny.response) {
12431
+ // Try to extract from attached properties
12432
+ const apiLike = errAny.response || errAny;
12433
+ if (isApiErrorLike(apiLike)) {
12434
+ return getFriendlyErrorMessage(apiLike);
12435
+ }
12436
+ }
12437
+ // Check if the Error has a `cause` with API error details (modern Error cause pattern)
12438
+ if (errAny.cause && typeof errAny.cause === 'object') {
12439
+ if (isApiErrorLike(errAny.cause)) {
12440
+ return getFriendlyErrorMessage(errAny.cause);
12441
+ }
12442
+ }
12443
+ // If the message is "[object Object]", the error was constructed from a plain object
12444
+ // This is useless - return a generic message instead
12445
+ if (error.message === '[object Object]') {
12446
+ // Log the actual error for debugging
12447
+ console.warn('[AuthKit] Error with [object Object] message. Raw error:', JSON.stringify(errAny, Object.getOwnPropertyNames(errAny)));
12448
+ return 'An unexpected error occurred. Please try again.';
12449
+ }
12426
12450
  // Check if the message itself contains a known API error pattern
12427
12451
  if (/already (registered|exists)/i.test(error.message)) {
12428
12452
  return 'This email is already registered.';
@@ -12494,6 +12518,27 @@ function getErrorCode(error) {
12494
12518
  }
12495
12519
  return undefined;
12496
12520
  }
12521
+ /**
12522
+ * Error codes that indicate the user needs to verify their email.
12523
+ */
12524
+ const EMAIL_VERIFICATION_ERROR_CODES = new Set([
12525
+ 'EMAIL_NOT_VERIFIED',
12526
+ 'ACCOUNT_LOCKED',
12527
+ 'EMAIL_VERIFICATION_EXPIRED',
12528
+ ]);
12529
+ /**
12530
+ * Checks if an error requires email verification action from the user.
12531
+ */
12532
+ function requiresEmailVerification(error) {
12533
+ const code = getErrorCode(error);
12534
+ if (code && EMAIL_VERIFICATION_ERROR_CODES.has(code))
12535
+ return true;
12536
+ // Also check the flag from the response body
12537
+ if (error && typeof error === 'object' && 'requiresEmailVerification' in error) {
12538
+ return error.requiresEmailVerification === true;
12539
+ }
12540
+ return false;
12541
+ }
12497
12542
 
12498
12543
  // VERSION: Update this when making changes to help identify which version is running
12499
12544
  const AUTH_UI_VERSION = '44';
@@ -13236,10 +13281,18 @@ const SmartlinksAuthUI = ({ apiEndpoint, clientId, clientName, accountData, onAu
13236
13281
  if (response.token) {
13237
13282
  // Check for account lock or verification requirements
13238
13283
  if (response.accountLocked) {
13239
- throw new Error('Your account has been locked due to unverified email. Please check your email or request a new verification link.');
13284
+ setShowResendVerification(true);
13285
+ setResendEmail(data.email);
13286
+ setError('Your account has been locked due to unverified email. Click below to resend the verification link.');
13287
+ setLoading(false);
13288
+ return;
13240
13289
  }
13241
13290
  if (response.requiresEmailVerification) {
13242
- throw new Error('Please verify your email before logging in. Check your inbox for the verification link.');
13291
+ setShowResendVerification(true);
13292
+ setResendEmail(data.email);
13293
+ setError('Please verify your email before signing in. Click below to resend the verification link.');
13294
+ setLoading(false);
13295
+ return;
13243
13296
  }
13244
13297
  await auth.login(response.token, response.user, response.accountData, false, getExpirationFromResponse(response));
13245
13298
  setAuthSuccess(true);
@@ -13248,14 +13301,26 @@ const SmartlinksAuthUI = ({ apiEndpoint, clientId, clientName, accountData, onAu
13248
13301
  // Note: No automatic redirect - app controls navigation via onAuthSuccess callback
13249
13302
  }
13250
13303
  else {
13251
- throw new Error('Authentication failed - please verify your email before logging in.');
13304
+ // No token returned - likely requires email verification
13305
+ setShowResendVerification(true);
13306
+ setResendEmail(data.email);
13307
+ setError('Please verify your email before signing in. Click below to resend the verification link.');
13308
+ setLoading(false);
13309
+ return;
13252
13310
  }
13253
13311
  }
13254
13312
  }
13255
13313
  catch (err) {
13314
+ // Debug: log the raw error shape to help diagnose SDK error wrapping issues
13315
+ log.error('handleEmailAuth error:', typeof err, err instanceof Error ? `Error.message=${err.message}` : '', JSON.stringify(err, Object.getOwnPropertyNames(err || {})));
13316
+ // Check if error requires email verification (403 EMAIL_NOT_VERIFIED, ACCOUNT_LOCKED, etc.)
13317
+ if (requiresEmailVerification(err)) {
13318
+ setShowResendVerification(true);
13319
+ setResendEmail(data.email);
13320
+ setError(getFriendlyErrorMessage(err) + ' Click below to resend the verification link.');
13321
+ }
13256
13322
  // Check if error is about email already registered (409 conflict)
13257
- // Handle both SmartlinksApiError (statusCode 409) and plain Error with keyword matching
13258
- if (mode === 'register' && (isConflictError(err) ||
13323
+ else if (mode === 'register' && (isConflictError(err) ||
13259
13324
  (err instanceof Error && /already (registered|exists)/i.test(err.message)))) {
13260
13325
  setShowResendVerification(true);
13261
13326
  setResendEmail(data.email);
@@ -13264,7 +13329,8 @@ const SmartlinksAuthUI = ({ apiEndpoint, clientId, clientName, accountData, onAu
13264
13329
  else {
13265
13330
  setError(getFriendlyErrorMessage(err));
13266
13331
  }
13267
- onAuthError?.(err instanceof Error ? err : new Error(getFriendlyErrorMessage(err)));
13332
+ const friendlyMsg = getFriendlyErrorMessage(err);
13333
+ onAuthError?.(err instanceof Error ? new Error(friendlyMsg) : new Error(friendlyMsg));
13268
13334
  }
13269
13335
  finally {
13270
13336
  setLoading(false);
@@ -13852,14 +13918,14 @@ const SmartlinksAuthUI = ({ apiEndpoint, clientId, clientName, accountData, onAu
13852
13918
  color: config?.branding?.inheritHostStyles
13853
13919
  ? 'hsl(var(--foreground, 215 25% 15%))'
13854
13920
  : (resolvedTheme === 'dark' ? '#f1f5f9' : '#374151')
13855
- }, children: "Verification Link Expired" }), jsxRuntime.jsx("p", { style: {
13921
+ }, children: "Email Verification Required" }), jsxRuntime.jsx("p", { style: {
13856
13922
  marginBottom: '1rem',
13857
13923
  fontSize: '0.875rem',
13858
13924
  color: config?.branding?.inheritHostStyles
13859
13925
  ? 'hsl(var(--muted-foreground, 215 15% 45%))'
13860
13926
  : (resolvedTheme === 'dark' ? '#94a3b8' : '#6B7280'),
13861
13927
  lineHeight: '1.5'
13862
- }, children: "Your verification link has expired or is no longer valid. Please enter your email address below and we'll send you a new verification link." }), jsxRuntime.jsx("input", { type: "email", value: resendEmail || '', onChange: (e) => setResendEmail(e.target.value), placeholder: "your@email.com", className: config?.branding?.inheritHostStyles ? 'auth-input' : undefined, style: config?.branding?.inheritHostStyles ? {
13928
+ }, children: "Your email address needs to be verified before you can sign in. Enter your email below and we'll send you a verification link." }), jsxRuntime.jsx("input", { type: "email", value: resendEmail || '', onChange: (e) => setResendEmail(e.target.value), placeholder: "your@email.com", className: config?.branding?.inheritHostStyles ? 'auth-input' : undefined, style: config?.branding?.inheritHostStyles ? {
13863
13929
  width: '100%',
13864
13930
  padding: '0.625rem',
13865
13931
  marginBottom: '1rem',
@@ -14852,7 +14918,15 @@ function useIframeMessages(iframeRef, options) {
14852
14918
  }
14853
14919
  catch (err) {
14854
14920
  console.error('[SmartlinksFrame] Proxy error:', err);
14855
- response.error = err?.message || 'Unknown error';
14921
+ const statusCode = err?.statusCode ?? err?.response?.status ?? err?.response?.statusCode;
14922
+ const errorBody = err?.details ?? err?.response?.data ?? err?.response ?? err?.cause;
14923
+ response.error = typeof err?.message === 'string' ? err.message : 'Unknown error';
14924
+ if (typeof statusCode === 'number') {
14925
+ response.statusCode = statusCode;
14926
+ }
14927
+ if (errorBody && typeof errorBody === 'object') {
14928
+ response.errorBody = JSON.parse(JSON.stringify(errorBody));
14929
+ }
14856
14930
  onErrorRef.current?.(err);
14857
14931
  }
14858
14932
  sendResponse(event.source, event.origin, response);