@insforge/react 1.0.2-dev.9 → 1.0.2-refresh.2

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
@@ -29,6 +29,7 @@ interface InsforgeProviderProps {
29
29
  onAuthChange?: (user: InsforgeUser | null) => void;
30
30
  onSignIn?: (authToken: string) => Promise<void>;
31
31
  onSignOut?: () => Promise<void>;
32
+ onRefresh?: (authToken: string) => Promise<void>;
32
33
  /**
33
34
  * Initial auth state from server (for SSR hydration)
34
35
  * @internal - Not intended for public use, used by Next.js package
@@ -73,7 +74,7 @@ interface InsforgeProviderProps {
73
74
  * </InsforgeProvider>
74
75
  * ```
75
76
  */
76
- declare function InsforgeProviderCore({ children, baseUrl, afterSignInUrl, onAuthChange, onSignIn, onSignOut, initialState, }: InsforgeProviderProps): react_jsx_runtime.JSX.Element;
77
+ declare function InsforgeProviderCore({ children, baseUrl, afterSignInUrl, onAuthChange, onSignIn, onSignOut, onRefresh, initialState, }: InsforgeProviderProps): react_jsx_runtime.JSX.Element;
77
78
  declare function InsforgeProvider(props: InsforgeProviderProps): react_jsx_runtime.JSX.Element;
78
79
  /**
79
80
  * Hook to access Insforge context
package/dist/index.d.ts CHANGED
@@ -29,6 +29,7 @@ interface InsforgeProviderProps {
29
29
  onAuthChange?: (user: InsforgeUser | null) => void;
30
30
  onSignIn?: (authToken: string) => Promise<void>;
31
31
  onSignOut?: () => Promise<void>;
32
+ onRefresh?: (authToken: string) => Promise<void>;
32
33
  /**
33
34
  * Initial auth state from server (for SSR hydration)
34
35
  * @internal - Not intended for public use, used by Next.js package
@@ -73,7 +74,7 @@ interface InsforgeProviderProps {
73
74
  * </InsforgeProvider>
74
75
  * ```
75
76
  */
76
- declare function InsforgeProviderCore({ children, baseUrl, afterSignInUrl, onAuthChange, onSignIn, onSignOut, initialState, }: InsforgeProviderProps): react_jsx_runtime.JSX.Element;
77
+ declare function InsforgeProviderCore({ children, baseUrl, afterSignInUrl, onAuthChange, onSignIn, onSignOut, onRefresh, initialState, }: InsforgeProviderProps): react_jsx_runtime.JSX.Element;
77
78
  declare function InsforgeProvider(props: InsforgeProviderProps): react_jsx_runtime.JSX.Element;
78
79
  /**
79
80
  * Hook to access Insforge context
package/dist/index.js CHANGED
@@ -544,9 +544,10 @@ var InsforgeManager = class _InsforgeManager {
544
544
  // Called after hydration to get complete user information beyond what's in cookies
545
545
  async loadAuthState() {
546
546
  try {
547
- const sessionResult = this.sdk.auth.getCurrentSession();
548
- const token2 = sessionResult.data?.session?.accessToken || null;
549
- if (!token2) {
547
+ const {
548
+ data: { session }
549
+ } = await this.sdk.auth.getCurrentSession();
550
+ if (!session) {
550
551
  this.user = null;
551
552
  if (this.config.onAuthChange) {
552
553
  this.config.onAuthChange(null);
@@ -555,9 +556,18 @@ var InsforgeManager = class _InsforgeManager {
555
556
  this.notifyListeners();
556
557
  return { success: false, error: "no_session" };
557
558
  }
559
+ if (this.config.onRefresh && session.accessToken) {
560
+ try {
561
+ await this.config.onRefresh(session.accessToken);
562
+ } catch (error) {
563
+ if (error instanceof Error) {
564
+ console.error("[InsforgeManager] Error syncing token on refresh:", error.message);
565
+ }
566
+ }
567
+ }
558
568
  const userResult = await this.sdk.auth.getCurrentUser();
559
569
  if (userResult.data) {
560
- const profile = userResult.data.profile;
570
+ const profile = userResult.data.user.profile;
561
571
  const userData = {
562
572
  id: userResult.data.user.id,
563
573
  email: userResult.data.user.email,
@@ -573,36 +583,16 @@ var InsforgeManager = class _InsforgeManager {
573
583
  this.notifyListeners();
574
584
  return { success: true };
575
585
  } else {
576
- await this.sdk.auth.signOut();
577
- if (this.config.onSignOut) {
578
- try {
579
- await this.config.onSignOut();
580
- } catch (error) {
581
- if (error instanceof Error) {
582
- console.error("[InsforgeManager] Error clearing cookie:", error.message);
583
- }
584
- }
585
- }
586
586
  this.user = null;
587
587
  if (this.config.onAuthChange) {
588
588
  this.config.onAuthChange(null);
589
589
  }
590
590
  this.isLoaded = true;
591
591
  this.notifyListeners();
592
- return { success: false, error: "invalid_token" };
592
+ const errorMessage = userResult.error?.message ?? "failed_to_get_user";
593
+ return { success: false, error: errorMessage };
593
594
  }
594
595
  } catch (error) {
595
- console.error("[InsforgeManager] Token validation failed:", error);
596
- await this.sdk.auth.signOut();
597
- if (this.config.onSignOut) {
598
- try {
599
- await this.config.onSignOut();
600
- } catch (error2) {
601
- if (error2 instanceof Error) {
602
- console.error("[InsforgeManager] Error clearing cookie:", error2.message);
603
- }
604
- }
605
- }
606
596
  this.user = null;
607
597
  if (this.config.onAuthChange) {
608
598
  this.config.onAuthChange(null);
@@ -619,7 +609,7 @@ var InsforgeManager = class _InsforgeManager {
619
609
  async handleAuthSuccess(authToken, fallbackUser) {
620
610
  const userResult = await this.sdk.auth.getCurrentUser();
621
611
  if (userResult.data) {
622
- const profile = userResult.data.profile;
612
+ const profile = userResult.data.user.profile;
623
613
  const userData = {
624
614
  id: userResult.data.user.id,
625
615
  email: userResult.data.user.email,
@@ -666,7 +656,7 @@ var InsforgeManager = class _InsforgeManager {
666
656
  sdkResult.data.user ? {
667
657
  id: sdkResult.data.user.id,
668
658
  email: sdkResult.data.user.email,
669
- name: sdkResult.data.user.name
659
+ name: sdkResult.data.user.profile?.name || ""
670
660
  } : void 0
671
661
  );
672
662
  return sdkResult.data;
@@ -687,7 +677,7 @@ var InsforgeManager = class _InsforgeManager {
687
677
  sdkResult.data.user ? {
688
678
  id: sdkResult.data.user.id,
689
679
  email: sdkResult.data.user.email,
690
- name: sdkResult.data.user.name
680
+ name: sdkResult.data.user.profile?.name || ""
691
681
  } : void 0
692
682
  );
693
683
  }
@@ -733,12 +723,12 @@ var InsforgeManager = class _InsforgeManager {
733
723
  if (result.data) {
734
724
  const userResult = await this.sdk.auth.getCurrentUser();
735
725
  if (userResult.data) {
736
- const profile = userResult.data.profile;
726
+ const profile = userResult.data.user.profile;
737
727
  const updatedUser = {
738
728
  id: userResult.data.user.id,
739
729
  email: userResult.data.user.email,
740
- name: profile?.nickname || "",
741
- avatarUrl: profile?.avatarUrl || ""
730
+ name: profile?.name || "",
731
+ avatarUrl: profile?.avatar_url || ""
742
732
  };
743
733
  this.user = updatedUser;
744
734
  if (this.config.onAuthChange) {
@@ -775,10 +765,18 @@ var InsforgeManager = class _InsforgeManager {
775
765
  return sdkResult.data;
776
766
  } else {
777
767
  return {
768
+ user: {
769
+ id: "",
770
+ createdAt: "",
771
+ email: "",
772
+ emailVerified: false,
773
+ updatedAt: "",
774
+ metadata: null,
775
+ profile: null
776
+ },
778
777
  accessToken: "",
779
- error: {
780
- message: sdkResult.error?.message || "Email verification failed"
781
- }
778
+ redirectTo: void 0,
779
+ csrfToken: void 0
782
780
  };
783
781
  }
784
782
  }
@@ -2187,6 +2185,7 @@ function InsforgeProviderCore({
2187
2185
  onAuthChange,
2188
2186
  onSignIn,
2189
2187
  onSignOut,
2188
+ onRefresh,
2190
2189
  initialState
2191
2190
  }) {
2192
2191
  const manager = useMemo(
@@ -2195,9 +2194,10 @@ function InsforgeProviderCore({
2195
2194
  afterSignInUrl,
2196
2195
  onAuthChange,
2197
2196
  onSignIn,
2198
- onSignOut
2197
+ onSignOut,
2198
+ onRefresh
2199
2199
  }),
2200
- [baseUrl, afterSignInUrl, onAuthChange, onSignIn, onSignOut]
2200
+ [baseUrl, afterSignInUrl, onAuthChange, onSignIn, onSignOut, onRefresh]
2201
2201
  );
2202
2202
  if (initialState) {
2203
2203
  const currentState = manager.getState();
@@ -4175,12 +4175,16 @@ function SignIn({ onError, ...uiProps }) {
4175
4175
  throw new Error(result.error);
4176
4176
  }
4177
4177
  const { user, accessToken, redirectTo } = result;
4178
+ const csrfToken = result.csrfToken;
4178
4179
  if (user) {
4179
4180
  const finalUrl = new URL(redirectTo || redirectUrl || "", window.location.origin);
4180
4181
  finalUrl.searchParams.set("access_token", accessToken);
4181
4182
  finalUrl.searchParams.set("user_id", user.id);
4182
4183
  finalUrl.searchParams.set("email", user.email);
4183
- finalUrl.searchParams.set("name", user.name);
4184
+ finalUrl.searchParams.set("name", user.profile?.name || "");
4185
+ if (csrfToken) {
4186
+ finalUrl.searchParams.set("csrf_token", csrfToken);
4187
+ }
4184
4188
  window.location.href = finalUrl.toString();
4185
4189
  }
4186
4190
  } catch (err) {
@@ -4197,17 +4201,18 @@ function SignIn({ onError, ...uiProps }) {
4197
4201
  setError("");
4198
4202
  try {
4199
4203
  const result = await verifyEmail(code, email);
4200
- if (result?.error) {
4201
- throw new Error(result.error.message || "Verification failed");
4204
+ if (!result) {
4205
+ throw new Error("Verification failed");
4202
4206
  }
4203
- if (result?.user) {
4204
- const finalUrl = new URL(result.redirectTo || redirectUrl || "", window.location.origin);
4205
- finalUrl.searchParams.set("access_token", result.accessToken);
4206
- finalUrl.searchParams.set("user_id", result.user.id);
4207
- finalUrl.searchParams.set("email", result.user.email);
4208
- finalUrl.searchParams.set("name", result.user.name);
4209
- window.location.href = finalUrl.toString();
4207
+ const finalUrl = new URL(result.redirectTo || redirectUrl || "", window.location.origin);
4208
+ finalUrl.searchParams.set("access_token", result.accessToken);
4209
+ finalUrl.searchParams.set("user_id", result.user.id);
4210
+ finalUrl.searchParams.set("email", result.user.email);
4211
+ finalUrl.searchParams.set("name", result.user.profile?.name || "");
4212
+ if (result.csrfToken) {
4213
+ finalUrl.searchParams.set("csrf_token", result.csrfToken);
4210
4214
  }
4215
+ window.location.href = finalUrl.toString();
4211
4216
  } catch (err) {
4212
4217
  const errorMessage = err instanceof Error ? err.message : "Invalid verification code";
4213
4218
  setError(errorMessage);
@@ -4461,11 +4466,15 @@ function SignUp({ onError, ...uiProps }) {
4461
4466
  return;
4462
4467
  }
4463
4468
  if (result.accessToken && result.user) {
4469
+ const csrfToken = result.csrfToken;
4464
4470
  const finalUrl = new URL(result.redirectTo || redirectUrl || "", window.location.origin);
4465
4471
  finalUrl.searchParams.set("access_token", result.accessToken);
4466
4472
  finalUrl.searchParams.set("user_id", result.user.id);
4467
4473
  finalUrl.searchParams.set("email", result.user.email);
4468
- finalUrl.searchParams.set("name", result.user.name);
4474
+ finalUrl.searchParams.set("name", result.user.profile?.name || "");
4475
+ if (csrfToken) {
4476
+ finalUrl.searchParams.set("csrf_token", csrfToken);
4477
+ }
4469
4478
  window.location.href = finalUrl.toString();
4470
4479
  }
4471
4480
  } catch (err) {
@@ -4482,17 +4491,18 @@ function SignUp({ onError, ...uiProps }) {
4482
4491
  setError("");
4483
4492
  try {
4484
4493
  const result = await verifyEmail(code, email);
4485
- if (result?.error) {
4486
- throw new Error(result.error.message || "Verification failed");
4494
+ if (!result) {
4495
+ throw new Error("Verification failed");
4487
4496
  }
4488
- if (result?.user) {
4489
- const finalUrl = new URL(result.redirectTo || redirectUrl || "", window.location.origin);
4490
- finalUrl.searchParams.set("access_token", result.accessToken);
4491
- finalUrl.searchParams.set("user_id", result.user.id);
4492
- finalUrl.searchParams.set("email", result.user.email);
4493
- finalUrl.searchParams.set("name", result.user.name);
4494
- window.location.href = finalUrl.toString();
4497
+ const finalUrl = new URL(result.redirectTo || redirectUrl || "", window.location.origin);
4498
+ finalUrl.searchParams.set("access_token", result.accessToken);
4499
+ finalUrl.searchParams.set("user_id", result.user.id);
4500
+ finalUrl.searchParams.set("email", result.user.email);
4501
+ finalUrl.searchParams.set("name", result.user.profile?.name || "");
4502
+ if (result.csrfToken) {
4503
+ finalUrl.searchParams.set("csrf_token", result.csrfToken);
4495
4504
  }
4505
+ window.location.href = finalUrl.toString();
4496
4506
  } catch (err) {
4497
4507
  const errorMessage = err instanceof Error ? err.message : "Invalid verification code";
4498
4508
  setError(errorMessage);