@insforge/react 0.6.5 → 0.6.6

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.d.cts CHANGED
@@ -14,6 +14,10 @@ import '@insforge/sdk';
14
14
  import '@insforge/shared-schemas';
15
15
  import 'zod';
16
16
 
17
+ interface InitialAuthState {
18
+ user?: InsforgeUser | null;
19
+ userId?: string | null;
20
+ }
17
21
  interface InsforgeProviderProps {
18
22
  children: ReactNode;
19
23
  baseUrl: string;
@@ -25,6 +29,11 @@ interface InsforgeProviderProps {
25
29
  onAuthChange?: (user: InsforgeUser | null) => void;
26
30
  onSignIn?: (authToken: string) => Promise<void>;
27
31
  onSignOut?: () => Promise<void>;
32
+ /**
33
+ * Initial auth state from server (for SSR hydration)
34
+ * @internal - Not intended for public use, used by Next.js package
35
+ */
36
+ initialState?: InitialAuthState;
28
37
  }
29
38
  /**
30
39
  * Unified Insforge Provider - manages authentication state and configuration
@@ -64,7 +73,7 @@ interface InsforgeProviderProps {
64
73
  * </InsforgeProvider>
65
74
  * ```
66
75
  */
67
- declare function InsforgeProviderCore({ children, baseUrl, afterSignInUrl, onAuthChange, onSignIn, onSignOut, }: InsforgeProviderProps): react_jsx_runtime.JSX.Element;
76
+ declare function InsforgeProviderCore({ children, baseUrl, afterSignInUrl, onAuthChange, onSignIn, onSignOut, initialState, }: InsforgeProviderProps): react_jsx_runtime.JSX.Element;
68
77
  declare function InsforgeProvider(props: InsforgeProviderProps): react_jsx_runtime.JSX.Element;
69
78
  /**
70
79
  * Hook to access Insforge context
@@ -100,4 +109,4 @@ declare function getProviderConfig(provider: OAuthProvider): OAuthProviderConfig
100
109
  */
101
110
  declare function getAllProviderConfigs(): Partial<Record<OAuthProvider, OAuthProviderConfig>>;
102
111
 
103
- export { InsforgeProvider, InsforgeProviderCore, type InsforgeProviderProps, OAUTH_PROVIDER_CONFIG, OAuthProviderConfig, getAllProviderConfigs, getProviderConfig, useInsforge };
112
+ export { type InitialAuthState, InsforgeProvider, InsforgeProviderCore, type InsforgeProviderProps, OAUTH_PROVIDER_CONFIG, OAuthProviderConfig, getAllProviderConfigs, getProviderConfig, useInsforge };
package/dist/index.d.ts CHANGED
@@ -14,6 +14,10 @@ import '@insforge/sdk';
14
14
  import '@insforge/shared-schemas';
15
15
  import 'zod';
16
16
 
17
+ interface InitialAuthState {
18
+ user?: InsforgeUser | null;
19
+ userId?: string | null;
20
+ }
17
21
  interface InsforgeProviderProps {
18
22
  children: ReactNode;
19
23
  baseUrl: string;
@@ -25,6 +29,11 @@ interface InsforgeProviderProps {
25
29
  onAuthChange?: (user: InsforgeUser | null) => void;
26
30
  onSignIn?: (authToken: string) => Promise<void>;
27
31
  onSignOut?: () => Promise<void>;
32
+ /**
33
+ * Initial auth state from server (for SSR hydration)
34
+ * @internal - Not intended for public use, used by Next.js package
35
+ */
36
+ initialState?: InitialAuthState;
28
37
  }
29
38
  /**
30
39
  * Unified Insforge Provider - manages authentication state and configuration
@@ -64,7 +73,7 @@ interface InsforgeProviderProps {
64
73
  * </InsforgeProvider>
65
74
  * ```
66
75
  */
67
- declare function InsforgeProviderCore({ children, baseUrl, afterSignInUrl, onAuthChange, onSignIn, onSignOut, }: InsforgeProviderProps): react_jsx_runtime.JSX.Element;
76
+ declare function InsforgeProviderCore({ children, baseUrl, afterSignInUrl, onAuthChange, onSignIn, onSignOut, initialState, }: InsforgeProviderProps): react_jsx_runtime.JSX.Element;
68
77
  declare function InsforgeProvider(props: InsforgeProviderProps): react_jsx_runtime.JSX.Element;
69
78
  /**
70
79
  * Hook to access Insforge context
@@ -100,4 +109,4 @@ declare function getProviderConfig(provider: OAuthProvider): OAuthProviderConfig
100
109
  */
101
110
  declare function getAllProviderConfigs(): Partial<Record<OAuthProvider, OAuthProviderConfig>>;
102
111
 
103
- export { InsforgeProvider, InsforgeProviderCore, type InsforgeProviderProps, OAUTH_PROVIDER_CONFIG, OAuthProviderConfig, getAllProviderConfigs, getProviderConfig, useInsforge };
112
+ export { type InitialAuthState, InsforgeProvider, InsforgeProviderCore, type InsforgeProviderProps, OAUTH_PROVIDER_CONFIG, OAuthProviderConfig, getAllProviderConfigs, getProviderConfig, useInsforge };
package/dist/index.js CHANGED
@@ -104,16 +104,23 @@ var InsforgeManager = class _InsforgeManager {
104
104
  this.config = { ...this.config, ...config };
105
105
  }
106
106
  // Public initialization method
107
+ // Following Clerk's pattern: Even if we have initialState (isLoaded=true from cookies),
108
+ // we still need to load full user data from SDK/API
107
109
  async initialize() {
108
- if (this.isLoaded) {
109
- return;
110
- }
111
110
  if (this.isInitializing) {
112
111
  return;
113
112
  }
114
113
  this.isInitializing = true;
115
114
  try {
116
- await this.loadAuthState();
115
+ const sessionResult = this.sdk.auth.getCurrentSession();
116
+ const hasToken = !!sessionResult.data?.session?.accessToken;
117
+ if (hasToken) {
118
+ await this.loadAuthState();
119
+ } else if (this.user === void 0) {
120
+ this.user = null;
121
+ this.isLoaded = true;
122
+ this.notifyListeners();
123
+ }
117
124
  } finally {
118
125
  this.isInitializing = false;
119
126
  }
@@ -139,6 +146,8 @@ var InsforgeManager = class _InsforgeManager {
139
146
  this.listeners.forEach((listener) => listener(state));
140
147
  }
141
148
  // Load auth state
149
+ // This loads the FULL user data from SDK (including complete profile)
150
+ // Called after hydration to get complete user information beyond what's in cookies
142
151
  async loadAuthState() {
143
152
  try {
144
153
  const sessionResult = this.sdk.auth.getCurrentSession();
@@ -161,6 +170,7 @@ var InsforgeManager = class _InsforgeManager {
161
170
  email: userResult.data.user.email,
162
171
  name: profile?.nickname || "",
163
172
  avatarUrl: profile?.avatarUrl || ""
173
+ // You can add more profile fields here as needed
164
174
  };
165
175
  this.user = userData;
166
176
  if (this.config.onAuthChange) {
@@ -421,6 +431,22 @@ var InsforgeManager = class _InsforgeManager {
421
431
  }
422
432
  this.notifyListeners();
423
433
  }
434
+ // Set initial state from server (for SSR hydration)
435
+ // This is ONLY basic user info from cookies, not full profile
436
+ // Following Clerk's pattern: initialState prevents hydration mismatch
437
+ // but full user data is still loaded via initialize()
438
+ setInitialState(initialState) {
439
+ if (this.user !== void 0) {
440
+ return;
441
+ }
442
+ if (initialState.userId) {
443
+ this.user = initialState.user ?? null;
444
+ } else {
445
+ this.user = null;
446
+ }
447
+ this.isLoaded = true;
448
+ this.notifyListeners();
449
+ }
424
450
  getConfig() {
425
451
  return this.config;
426
452
  }
@@ -461,7 +487,8 @@ function InsforgeProviderCore({
461
487
  afterSignInUrl = "/",
462
488
  onAuthChange,
463
489
  onSignIn,
464
- onSignOut
490
+ onSignOut,
491
+ initialState
465
492
  }) {
466
493
  const manager = useMemo(
467
494
  () => InsforgeManager.getInstance({
@@ -473,6 +500,12 @@ function InsforgeProviderCore({
473
500
  }),
474
501
  [baseUrl, afterSignInUrl, onAuthChange, onSignIn, onSignOut]
475
502
  );
503
+ if (initialState) {
504
+ const currentState = manager.getState();
505
+ if (currentState.userId === void 0 && initialState.userId !== void 0) {
506
+ manager.setInitialState(initialState);
507
+ }
508
+ }
476
509
  const [state, setState] = useState(() => manager.getState());
477
510
  useEffect(() => {
478
511
  const unsubscribe = manager.subscribe((newState) => {