@nauth-toolkit/client 0.1.74 → 0.1.77

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.cjs CHANGED
@@ -827,11 +827,35 @@ var NAuthClient = class {
827
827
  const refreshFn = async () => {
828
828
  return this.post(this.config.endpoints.refresh, body, false);
829
829
  };
830
- const tokens = await this.tokenManager.refreshOnce(refreshFn);
830
+ const tokens = await this.tokenManager.refreshOnce(refreshFn, { persist: tokenDelivery === "json" });
831
831
  this.config.onTokenRefresh?.();
832
832
  this.eventEmitter.emit({ type: "auth:refresh", data: { success: true }, timestamp: Date.now() });
833
833
  return tokens;
834
834
  }
835
+ // ============================================================================
836
+ // Local state management (no network)
837
+ // ============================================================================
838
+ /**
839
+ * Clear all local auth state without making any network requests.
840
+ *
841
+ * WHY:
842
+ * - When refresh fails with 401 (session expired), clients should immediately drop any cached
843
+ * auth state (user + tokens) to prevent "sticky auth" across hard reloads.
844
+ * - In cookie delivery modes, httpOnly cookies can only be cleared by the backend; this method
845
+ * only clears client-side state (e.g., cached user + persisted tokens in JSON mode).
846
+ *
847
+ * @param options - Optional behavior flags
848
+ * @returns Promise that resolves when local state is cleared
849
+ *
850
+ * @example
851
+ * ```typescript
852
+ * // Called by framework adapters/interceptors when refresh fails with 401
853
+ * await client.clearLocalAuthState();
854
+ * ```
855
+ */
856
+ async clearLocalAuthState(options) {
857
+ await this.clearAuthState(options?.forgetDevice ?? false);
858
+ }
835
859
  /**
836
860
  * Logout current session.
837
861
  *
@@ -1197,8 +1221,8 @@ var NAuthClient = class {
1197
1221
  this.eventEmitter.emit({ type: "oauth:started", data: { provider }, timestamp: Date.now() });
1198
1222
  if (hasWindow()) {
1199
1223
  const startPath = this.config.endpoints.socialRedirectStart.replace(":provider", provider);
1200
- const base = this.config.baseUrl.replace(/\/$/, "");
1201
- const startUrl = new URL(`${base}${startPath}`);
1224
+ const fullUrl = this.buildUrl(startPath);
1225
+ const startUrl = new URL(fullUrl);
1202
1226
  const returnTo = options?.returnTo ?? this.config.redirects?.success ?? "/";
1203
1227
  startUrl.searchParams.set("returnTo", returnTo);
1204
1228
  if (options?.action === "link") {