@insforge/react 1.1.4 → 1.1.6-test.0

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.
@@ -1757,6 +1757,7 @@ function useInsforge() {
1757
1757
  exchangeResetPasswordToken: () => Promise.resolve({ error: { message: "SSR mode" } }),
1758
1758
  loginWithOAuth: () => Promise.resolve(),
1759
1759
  getPublicAuthConfig: () => Promise.resolve(null),
1760
+ getSession: () => Promise.resolve(null),
1760
1761
  baseUrl: "",
1761
1762
  afterSignInUrl: "/"
1762
1763
  };
@@ -3625,8 +3626,36 @@ function SignInForm({
3625
3626
  ] })
3626
3627
  ] });
3627
3628
  }
3629
+
3630
+ // src/lib/hosted-auth.ts
3631
+ function isHostedAuthEnvironment() {
3632
+ if (typeof window === "undefined") {
3633
+ return false;
3634
+ }
3635
+ const { hostname, port, protocol } = window.location;
3636
+ if (hostname === "localhost" && port === "7130") {
3637
+ return true;
3638
+ }
3639
+ if (protocol === "https:" && hostname.endsWith(".insforge.app")) {
3640
+ return true;
3641
+ }
3642
+ return false;
3643
+ }
3644
+ function buildLegacyAuthUrl(redirectUrl, session) {
3645
+ const url = new URL(redirectUrl);
3646
+ url.searchParams.set("access_token", session.accessToken);
3647
+ url.searchParams.set("user_id", session.userId);
3648
+ url.searchParams.set("email", session.email);
3649
+ if (session.name) {
3650
+ url.searchParams.set("name", session.name);
3651
+ }
3652
+ if (session.csrfToken) {
3653
+ url.searchParams.set("csrf_token", session.csrfToken);
3654
+ }
3655
+ return url.toString();
3656
+ }
3628
3657
  function SignIn({ onError, ...uiProps }) {
3629
- const { signIn, verifyEmail, loginWithOAuth } = useInsforge();
3658
+ const { signIn, verifyEmail, loginWithOAuth, isSignedIn, getSession, afterSignInUrl } = useInsforge();
3630
3659
  const { authConfig } = usePublicAuthConfig();
3631
3660
  const [email, setEmail] = useState("");
3632
3661
  const [password, setPassword] = useState("");
@@ -3636,6 +3665,37 @@ function SignIn({ onError, ...uiProps }) {
3636
3665
  const [oauthLoading] = useState(null);
3637
3666
  const searchParams = useSearchParams();
3638
3667
  const redirectUrl = searchParams.get("redirect");
3668
+ const isHandlingOAuthRedirectRef = useRef(false);
3669
+ useEffect(() => {
3670
+ async function handleOAuthComplete() {
3671
+ if (!isSignedIn || isHandlingOAuthRedirectRef.current) return;
3672
+ const isHosted = isHostedAuthEnvironment();
3673
+ if (isHosted && redirectUrl) {
3674
+ isHandlingOAuthRedirectRef.current = true;
3675
+ try {
3676
+ const session = await getSession();
3677
+ if (!session) {
3678
+ isHandlingOAuthRedirectRef.current = false;
3679
+ return;
3680
+ }
3681
+ const legacyUrl = buildLegacyAuthUrl(redirectUrl, {
3682
+ accessToken: session.accessToken,
3683
+ userId: session.user.id,
3684
+ email: session.user.email,
3685
+ name: session.user.profile?.name || ""
3686
+ });
3687
+ window.location.href = legacyUrl;
3688
+ } catch (err) {
3689
+ console.error("Failed to redirect after OAuth:", err);
3690
+ isHandlingOAuthRedirectRef.current = false;
3691
+ }
3692
+ } else if (!isHosted) {
3693
+ const finalUrl = redirectUrl || afterSignInUrl || "/";
3694
+ window.location.href = new URL(finalUrl, window.location.origin).toString();
3695
+ }
3696
+ }
3697
+ void handleOAuthComplete();
3698
+ }, [isSignedIn, redirectUrl, getSession, afterSignInUrl]);
3639
3699
  async function handleSubmit(e) {
3640
3700
  e.preventDefault();
3641
3701
  setLoading(true);
@@ -3650,9 +3710,16 @@ function SignIn({ onError, ...uiProps }) {
3650
3710
  }
3651
3711
  throw new Error(result.error);
3652
3712
  }
3653
- const { user, redirectTo } = result;
3713
+ const { user, accessToken, redirectTo, csrfToken } = result;
3654
3714
  if (user) {
3655
3715
  const finalUrl = new URL(redirectTo || redirectUrl || "", window.location.origin);
3716
+ finalUrl.searchParams.set("access_token", accessToken);
3717
+ finalUrl.searchParams.set("user_id", user.id);
3718
+ finalUrl.searchParams.set("email", user.email);
3719
+ finalUrl.searchParams.set("name", user.profile?.name || "");
3720
+ if (csrfToken) {
3721
+ finalUrl.searchParams.set("csrf_token", csrfToken);
3722
+ }
3656
3723
  window.location.href = finalUrl.toString();
3657
3724
  }
3658
3725
  } catch (err) {
@@ -3673,6 +3740,13 @@ function SignIn({ onError, ...uiProps }) {
3673
3740
  throw new Error("Verification failed");
3674
3741
  }
3675
3742
  const finalUrl = new URL(result.redirectTo || redirectUrl || "", window.location.origin);
3743
+ finalUrl.searchParams.set("access_token", result.accessToken);
3744
+ finalUrl.searchParams.set("user_id", result.user.id);
3745
+ finalUrl.searchParams.set("email", result.user.email);
3746
+ finalUrl.searchParams.set("name", result.user.profile?.name || "");
3747
+ if (result.csrfToken) {
3748
+ finalUrl.searchParams.set("csrf_token", result.csrfToken);
3749
+ }
3676
3750
  window.location.href = finalUrl.toString();
3677
3751
  } catch (err) {
3678
3752
  const errorMessage = err instanceof Error ? err.message : "Invalid verification code";
@@ -3681,7 +3755,7 @@ function SignIn({ onError, ...uiProps }) {
3681
3755
  }
3682
3756
  function handleOAuth(provider) {
3683
3757
  try {
3684
- void loginWithOAuth(provider, redirectUrl || "");
3758
+ void loginWithOAuth(provider, window.location.href);
3685
3759
  } catch (err) {
3686
3760
  const errorMessage = err instanceof Error ? err.message : "OAuth login failed";
3687
3761
  setError(errorMessage);
@@ -3833,7 +3907,7 @@ function createPasswordSchema(options) {
3833
3907
  }
3834
3908
  createPasswordSchema();
3835
3909
  function SignUp({ onError, emailRedirectTo, ...uiProps }) {
3836
- const { signUp, verifyEmail, loginWithOAuth } = useInsforge();
3910
+ const { signUp, verifyEmail, loginWithOAuth, isSignedIn, getSession, afterSignInUrl } = useInsforge();
3837
3911
  const { authConfig } = usePublicAuthConfig();
3838
3912
  const [email, setEmail] = useState("");
3839
3913
  const [password, setPassword] = useState("");
@@ -3843,6 +3917,37 @@ function SignUp({ onError, emailRedirectTo, ...uiProps }) {
3843
3917
  const [oauthLoading] = useState(null);
3844
3918
  const searchParams = useSearchParams();
3845
3919
  const redirectUrl = searchParams.get("redirect");
3920
+ const isHandlingOAuthRedirectRef = useRef(false);
3921
+ useEffect(() => {
3922
+ async function handleOAuthComplete() {
3923
+ if (!isSignedIn || isHandlingOAuthRedirectRef.current) return;
3924
+ const isHosted = isHostedAuthEnvironment();
3925
+ if (isHosted && redirectUrl) {
3926
+ isHandlingOAuthRedirectRef.current = true;
3927
+ try {
3928
+ const session = await getSession();
3929
+ if (!session) {
3930
+ isHandlingOAuthRedirectRef.current = false;
3931
+ return;
3932
+ }
3933
+ const legacyUrl = buildLegacyAuthUrl(redirectUrl, {
3934
+ accessToken: session.accessToken,
3935
+ userId: session.user.id,
3936
+ email: session.user.email,
3937
+ name: session.user.profile?.name || ""
3938
+ });
3939
+ window.location.href = legacyUrl;
3940
+ } catch (err) {
3941
+ console.error("Failed to redirect after OAuth:", err);
3942
+ isHandlingOAuthRedirectRef.current = false;
3943
+ }
3944
+ } else if (!isHosted) {
3945
+ const finalUrl = redirectUrl || afterSignInUrl || "/";
3946
+ window.location.href = new URL(finalUrl, window.location.origin).toString();
3947
+ }
3948
+ }
3949
+ void handleOAuthComplete();
3950
+ }, [isSignedIn, redirectUrl, getSession, afterSignInUrl]);
3846
3951
  async function handleSubmit(e) {
3847
3952
  e.preventDefault();
3848
3953
  setLoading(true);
@@ -3884,7 +3989,15 @@ function SignUp({ onError, emailRedirectTo, ...uiProps }) {
3884
3989
  return;
3885
3990
  }
3886
3991
  if (result.accessToken && result.user) {
3992
+ const csrfToken = result.csrfToken;
3887
3993
  const finalUrl = new URL(result.redirectTo || redirectUrl || "", window.location.origin);
3994
+ finalUrl.searchParams.set("access_token", result.accessToken);
3995
+ finalUrl.searchParams.set("user_id", result.user.id);
3996
+ finalUrl.searchParams.set("email", result.user.email);
3997
+ finalUrl.searchParams.set("name", result.user.profile?.name || "");
3998
+ if (csrfToken) {
3999
+ finalUrl.searchParams.set("csrf_token", csrfToken);
4000
+ }
3888
4001
  window.location.href = finalUrl.toString();
3889
4002
  }
3890
4003
  } catch (err) {
@@ -3905,6 +4018,13 @@ function SignUp({ onError, emailRedirectTo, ...uiProps }) {
3905
4018
  throw new Error("Verification failed");
3906
4019
  }
3907
4020
  const finalUrl = new URL(result.redirectTo || redirectUrl || "", window.location.origin);
4021
+ finalUrl.searchParams.set("access_token", result.accessToken);
4022
+ finalUrl.searchParams.set("user_id", result.user.id);
4023
+ finalUrl.searchParams.set("email", result.user.email);
4024
+ finalUrl.searchParams.set("name", result.user.profile?.name || "");
4025
+ if (result.csrfToken) {
4026
+ finalUrl.searchParams.set("csrf_token", result.csrfToken);
4027
+ }
3908
4028
  window.location.href = finalUrl.toString();
3909
4029
  } catch (err) {
3910
4030
  const errorMessage = err instanceof Error ? err.message : "Invalid verification code";
@@ -3913,7 +4033,7 @@ function SignUp({ onError, emailRedirectTo, ...uiProps }) {
3913
4033
  }
3914
4034
  function handleOAuth(provider) {
3915
4035
  try {
3916
- void loginWithOAuth(provider, redirectUrl || "");
4036
+ void loginWithOAuth(provider, window.location.href);
3917
4037
  } catch (err) {
3918
4038
  const errorMessage = err instanceof Error ? err.message : "OAuth login failed";
3919
4039
  setError(errorMessage);