@checkstack/auth-frontend 0.2.0 → 0.3.1

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.
@@ -1,10 +1,13 @@
1
1
  import { useMemo } from "react";
2
2
  import { createAuthClient } from "better-auth/react";
3
- import { useRuntimeConfig } from "@checkstack/frontend-api";
3
+ import {
4
+ useRuntimeConfig,
5
+ getCachedRuntimeConfig,
6
+ } from "@checkstack/frontend-api";
4
7
 
5
8
  // Cache for lazy-initialized client
6
9
  let cachedClient: ReturnType<typeof createAuthClient> | undefined;
7
- let configPromise: Promise<string> | undefined;
10
+ let cachedBaseUrl: string | undefined;
8
11
 
9
12
  /**
10
13
  * React hook to get the auth client with proper runtime config.
@@ -25,31 +28,23 @@ export function useAuthClient() {
25
28
 
26
29
  /**
27
30
  * Lazy-initialized auth client for class-based APIs.
28
- * Fetches config from /api/config if not already cached.
29
- * Use useAuthClient hook in React components instead.
31
+ * Uses the cached runtime config from RuntimeConfigProvider.
32
+ *
33
+ * Note: This should only be called AFTER RuntimeConfigProvider has loaded.
34
+ * Components rendered inside the provider tree are guaranteed to have config available.
30
35
  */
31
36
  export function getAuthClientLazy(): ReturnType<typeof createAuthClient> {
32
- if (!cachedClient) {
33
- // Create with default URL initially
37
+ const config = getCachedRuntimeConfig();
38
+ const baseUrl = config?.baseUrl ?? "http://localhost:3000";
39
+
40
+ // Recreate client if baseUrl changed or not yet created
41
+ if (!cachedClient || cachedBaseUrl !== baseUrl) {
42
+ cachedBaseUrl = baseUrl;
34
43
  cachedClient = createAuthClient({
35
- baseURL: "http://localhost:3000",
44
+ baseURL: baseUrl,
36
45
  basePath: "/api/auth",
37
46
  });
38
-
39
- // Fetch real config and update
40
- if (!configPromise) {
41
- configPromise = fetch("/api/config")
42
- .then((res) => res.json())
43
- .then((data: { baseUrl: string }) => data.baseUrl)
44
- .catch(() => "http://localhost:3000");
45
- }
46
-
47
- configPromise.then((baseUrl) => {
48
- cachedClient = createAuthClient({
49
- baseURL: baseUrl,
50
- basePath: "/api/auth",
51
- });
52
- });
53
47
  }
48
+
54
49
  return cachedClient;
55
50
  }