@insforge/react 1.0.7 → 1.0.9-dev.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
@@ -36,9 +36,9 @@ interface InsforgeProviderProps {
36
36
  */
37
37
  afterSignInUrl?: string;
38
38
  onAuthChange?: (user: InsforgeUser | null) => void;
39
- onSignIn?: (authToken: string) => Promise<void>;
39
+ onSignIn?: (authToken: string, user: InsforgeUser) => Promise<void>;
40
40
  onSignOut?: () => Promise<void>;
41
- onRefresh?: (authToken: string) => Promise<void>;
41
+ onRefresh?: (authToken: string, user: InsforgeUser) => Promise<void>;
42
42
  /**
43
43
  * Initial auth state from server (for SSR hydration)
44
44
  * @internal - Not intended for public use, used by Next.js package
package/dist/index.d.ts CHANGED
@@ -36,9 +36,9 @@ interface InsforgeProviderProps {
36
36
  */
37
37
  afterSignInUrl?: string;
38
38
  onAuthChange?: (user: InsforgeUser | null) => void;
39
- onSignIn?: (authToken: string) => Promise<void>;
39
+ onSignIn?: (authToken: string, user: InsforgeUser) => Promise<void>;
40
40
  onSignOut?: () => Promise<void>;
41
- onRefresh?: (authToken: string) => Promise<void>;
41
+ onRefresh?: (authToken: string, user: InsforgeUser) => Promise<void>;
42
42
  /**
43
43
  * Initial auth state from server (for SSR hydration)
44
44
  * @internal - Not intended for public use, used by Next.js package
package/dist/index.js CHANGED
@@ -562,7 +562,7 @@ var InsforgeManager = class _InsforgeManager {
562
562
  }
563
563
  if (this.config.onRefresh && session.accessToken) {
564
564
  try {
565
- await this.config.onRefresh(session.accessToken);
565
+ await this.config.onRefresh(session.accessToken, session.user);
566
566
  } catch (error) {
567
567
  if (error instanceof Error) {
568
568
  console.error("[InsforgeManager] Error syncing token on refresh:", error.message);
@@ -607,39 +607,22 @@ var InsforgeManager = class _InsforgeManager {
607
607
  }
608
608
  }
609
609
  // Helper to handle auth success
610
- async handleAuthSuccess(authToken, fallbackUser) {
611
- const userResult = await this.sdk.auth.getCurrentUser();
612
- if (userResult.data) {
613
- const userData = {
614
- id: userResult.data.user.id,
615
- email: userResult.data.user.email,
616
- profile: userResult.data.user.profile
617
- };
618
- this.user = userData;
610
+ async handleAuthSuccess(accessToken, user) {
611
+ if (user) {
612
+ this.user = user;
619
613
  this.notifyListeners();
620
614
  if (this.config.onAuthChange) {
621
- this.config.onAuthChange(userData);
615
+ this.config.onAuthChange(user);
622
616
  }
623
617
  if (this.config.onSignIn) {
624
618
  try {
625
- await this.config.onSignIn(authToken);
619
+ await this.config.onSignIn(accessToken, user);
626
620
  } catch (error) {
627
621
  if (error instanceof Error) {
628
622
  console.error("[InsforgeManager] Error syncing token to cookie:", error.message);
629
623
  }
630
624
  }
631
625
  }
632
- } else if (fallbackUser) {
633
- const userData = {
634
- id: fallbackUser.id || "",
635
- email: fallbackUser.email || "",
636
- profile: fallbackUser.profile || null
637
- };
638
- this.user = userData;
639
- this.notifyListeners();
640
- if (this.config.onAuthChange) {
641
- this.config.onAuthChange(userData);
642
- }
643
626
  }
644
627
  }
645
628
  // Business methods
@@ -654,7 +637,7 @@ var InsforgeManager = class _InsforgeManager {
654
637
  sdkResult.data.user ? {
655
638
  id: sdkResult.data.user.id,
656
639
  email: sdkResult.data.user.email,
657
- profile: sdkResult.data.user.profile || void 0
640
+ profile: sdkResult.data.user.profile ?? null
658
641
  } : void 0
659
642
  );
660
643
  return sdkResult.data;
@@ -669,13 +652,16 @@ var InsforgeManager = class _InsforgeManager {
669
652
  async signUp(email, password) {
670
653
  const sdkResult = await this.sdk.auth.signUp({ email, password });
671
654
  if (sdkResult.data) {
655
+ if (sdkResult.data.requireEmailVerification) {
656
+ return sdkResult.data;
657
+ }
672
658
  if (sdkResult.data.accessToken) {
673
659
  await this.handleAuthSuccess(
674
660
  sdkResult.data.accessToken,
675
661
  sdkResult.data.user ? {
676
662
  id: sdkResult.data.user.id,
677
663
  email: sdkResult.data.user.email,
678
- profile: sdkResult.data.user.profile || void 0
664
+ profile: sdkResult.data.user.profile ?? null
679
665
  } : void 0
680
666
  );
681
667
  }
@@ -754,21 +740,20 @@ var InsforgeManager = class _InsforgeManager {
754
740
  async verifyEmail(otp, email) {
755
741
  const sdkResult = await this.sdk.auth.verifyEmail({ otp, email: email || void 0 });
756
742
  if (sdkResult.data) {
743
+ await this.handleAuthSuccess(
744
+ sdkResult.data.accessToken,
745
+ sdkResult.data.user ? {
746
+ id: sdkResult.data.user.id,
747
+ email: sdkResult.data.user.email,
748
+ profile: sdkResult.data.user.profile ?? null
749
+ } : void 0
750
+ );
757
751
  return sdkResult.data;
758
752
  } else {
759
753
  return {
760
- user: {
761
- id: "",
762
- createdAt: "",
763
- email: "",
764
- emailVerified: false,
765
- updatedAt: "",
766
- metadata: null,
767
- profile: null
768
- },
769
- accessToken: "",
770
- redirectTo: void 0,
771
- csrfToken: void 0
754
+ error: sdkResult.error?.message || "Failed to verify email",
755
+ statusCode: sdkResult.error?.statusCode,
756
+ errorCode: sdkResult.error?.error
772
757
  };
773
758
  }
774
759
  }
@@ -2200,19 +2185,7 @@ function InsforgeProviderCore({
2200
2185
  const unsubscribe = manager.subscribe((newState) => {
2201
2186
  setState(newState);
2202
2187
  });
2203
- void manager.initialize().then(() => {
2204
- const params = new URLSearchParams(window.location.search);
2205
- if (params.has("access_token")) {
2206
- const url = new URL(window.location.href);
2207
- url.searchParams.delete("access_token");
2208
- url.searchParams.delete("user_id");
2209
- url.searchParams.delete("email");
2210
- url.searchParams.delete("name");
2211
- url.searchParams.delete("csrf_token");
2212
- url.searchParams.delete("error");
2213
- window.history.replaceState({}, document.title, url.toString());
2214
- }
2215
- });
2188
+ void manager.initialize();
2216
2189
  return () => {
2217
2190
  unsubscribe();
2218
2191
  };
@@ -2267,7 +2240,7 @@ function useInsforge() {
2267
2240
  sendVerificationEmail: () => Promise.resolve(null),
2268
2241
  sendResetPasswordEmail: () => Promise.resolve(null),
2269
2242
  resetPassword: () => Promise.resolve(null),
2270
- verifyEmail: () => Promise.resolve(null),
2243
+ verifyEmail: () => Promise.resolve({ error: "SSR mode" }),
2271
2244
  exchangeResetPasswordToken: () => Promise.resolve({ error: { message: "SSR mode" } }),
2272
2245
  loginWithOAuth: () => Promise.resolve(),
2273
2246
  getPublicAuthConfig: () => Promise.resolve(null),
@@ -4176,8 +4149,7 @@ function SignIn({ onError, ...uiProps }) {
4176
4149
  }
4177
4150
  throw new Error(result.error);
4178
4151
  }
4179
- const { user, accessToken, redirectTo } = result;
4180
- const csrfToken = result.csrfToken;
4152
+ const { user, accessToken, redirectTo, csrfToken } = result;
4181
4153
  if (user) {
4182
4154
  const finalUrl = new URL(redirectTo || redirectUrl || "", window.location.origin);
4183
4155
  finalUrl.searchParams.set("access_token", accessToken);
@@ -4203,7 +4175,7 @@ function SignIn({ onError, ...uiProps }) {
4203
4175
  setError("");
4204
4176
  try {
4205
4177
  const result = await verifyEmail(code, email);
4206
- if (!result) {
4178
+ if ("error" in result) {
4207
4179
  throw new Error("Verification failed");
4208
4180
  }
4209
4181
  const finalUrl = new URL(result.redirectTo || redirectUrl || "", window.location.origin);
@@ -4462,7 +4434,7 @@ function SignUp({ onError, ...uiProps }) {
4462
4434
  if ("error" in result) {
4463
4435
  throw new Error(result.error);
4464
4436
  }
4465
- if (result.requireEmailVerification && !result.accessToken) {
4437
+ if (result.requireEmailVerification) {
4466
4438
  setStep("awaiting-verification");
4467
4439
  setLoading(false);
4468
4440
  return;
@@ -4493,7 +4465,7 @@ function SignUp({ onError, ...uiProps }) {
4493
4465
  setError("");
4494
4466
  try {
4495
4467
  const result = await verifyEmail(code, email);
4496
- if (!result) {
4468
+ if ("error" in result) {
4497
4469
  throw new Error("Verification failed");
4498
4470
  }
4499
4471
  const finalUrl = new URL(result.redirectTo || redirectUrl || "", window.location.origin);
@@ -5077,8 +5049,8 @@ function VerifyEmail({ token: token2, onSuccess, onError, ...uiProps }) {
5077
5049
  }
5078
5050
  try {
5079
5051
  const result = await verifyEmail(token2);
5080
- if (!result?.accessToken) {
5081
- const errorMessage = result ? "Verification succeeded but no access token received" : "Email verification failed";
5052
+ if ("error" in result) {
5053
+ const errorMessage = result.error || "Email verification failed";
5082
5054
  setError(errorMessage);
5083
5055
  setStatus("error");
5084
5056
  if (onError) {