@passkeyme/auth 2.0.12 → 2.1.0

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