@passkeyme/auth 2.0.12 → 2.0.13

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.esm.js CHANGED
@@ -1048,6 +1048,25 @@ class PasskeymeAuth {
1048
1048
  });
1049
1049
  }
1050
1050
  }
1051
+ /**
1052
+ * Internal helper: Redirect to login in hosted mode, or throw error in direct mode
1053
+ */
1054
+ redirectOrThrow(userMessage, technicalMessage) {
1055
+ if (this.config.mode === "hosted") {
1056
+ this.redirectToLogin();
1057
+ return { method: "redirect" };
1058
+ }
1059
+ else {
1060
+ throw new PasskeymeError({
1061
+ code: PasskeymeErrorCode.PASSKEY_NOT_SUPPORTED,
1062
+ message: technicalMessage,
1063
+ userMessage: userMessage,
1064
+ recoverable: true,
1065
+ retryable: false,
1066
+ suggestedAction: "Use OAuth sign-in instead",
1067
+ });
1068
+ }
1069
+ }
1051
1070
  /**
1052
1071
  * Handle authentication callback from hosted auth page
1053
1072
  */
@@ -1514,7 +1533,7 @@ class PasskeymeAuth {
1514
1533
  * 1. Check app config for discoverable credentials support
1515
1534
  * 2. If supported: attempt discoverable credentials first (no username)
1516
1535
  * 3. If not supported or fails: use stored username or prompt for username
1517
- * 4. Fallback to hosted auth if all passkey attempts fail
1536
+ * 4. Fallback to hosted auth if all passkey attempts fail (unless mode is 'direct')
1518
1537
  */
1519
1538
  async smartLogin(username, apiKey) {
1520
1539
  logger.debug("[DEBUG] smartLogin called with:", {
@@ -1529,8 +1548,9 @@ class PasskeymeAuth {
1529
1548
  logger.debug("[DEBUG] Passkey support check:", isSupported);
1530
1549
  if (!isSupported || !effectiveApiKey) {
1531
1550
  logger.debug("Conditions not met, redirecting to hosted auth. isSupported:", isSupported, "hasApiKey:", !!effectiveApiKey);
1532
- this.redirectToLogin();
1533
- return { method: "redirect" };
1551
+ return this.redirectOrThrow(isSupported
1552
+ ? "API key is required for passkey authentication."
1553
+ : "Your device doesn't support passkeys.", "Passkey authentication not available");
1534
1554
  }
1535
1555
  try {
1536
1556
  // Get app configuration to check discoverable credentials support
@@ -1553,8 +1573,7 @@ class PasskeymeAuth {
1553
1573
  // If passkeys are disabled at the app level, fallback to hosted auth
1554
1574
  if (!appConfig.passkeyEnabled) {
1555
1575
  logger.debug("Passkeys disabled for this app, falling back to hosted auth");
1556
- this.redirectToLogin();
1557
- return { method: "redirect" };
1576
+ return this.redirectOrThrow("Passkey authentication is not available for this app.", "Passkeys are disabled for this application");
1558
1577
  }
1559
1578
  let authUsername = username;
1560
1579
  // Smart username resolution following industry best practices
@@ -1576,15 +1595,13 @@ class PasskeymeAuth {
1576
1595
  const storageKey = `last_username_${this.config.appId}`;
1577
1596
  logger.debug("Looking for stored username with key:", storageKey);
1578
1597
  const storedUsername = await this.storage.getItem(storageKey);
1579
- logger.debug("Found stored username:", storedUsername);
1580
1598
  if (storedUsername) {
1581
1599
  logger.debug("Using stored username for targeted authentication:", storedUsername);
1582
1600
  authUsername = storedUsername;
1583
1601
  }
1584
1602
  else {
1585
1603
  logger.debug("No username available and discoverable auth failed/unsupported, falling back to hosted auth");
1586
- this.redirectToLogin();
1587
- return { method: "redirect" };
1604
+ return this.redirectOrThrow("Unable to authenticate with passkey. Please sign in with another method.", "No username available for passkey authentication");
1588
1605
  }
1589
1606
  }
1590
1607
  // Attempt passkey authentication with username
@@ -1603,9 +1620,15 @@ class PasskeymeAuth {
1603
1620
  if (this.config.debug) {
1604
1621
  logger.error("[PasskeymeAuth] Passkey authentication error details:", error);
1605
1622
  }
1606
- // Fallback to hosted auth for any passkey failures
1607
- this.redirectToLogin();
1608
- return { method: "redirect" };
1623
+ // Fallback to hosted auth for any passkey failures (unless mode is 'direct')
1624
+ // In direct mode, re-throw the original error for the caller to handle
1625
+ if (this.config.mode === "hosted") {
1626
+ this.redirectToLogin();
1627
+ return { method: "redirect" };
1628
+ }
1629
+ else {
1630
+ throw error;
1631
+ }
1609
1632
  }
1610
1633
  }
1611
1634
  /**