@insforge/react 1.1.5 → 1.1.6-test.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.
@@ -1778,6 +1778,7 @@ function useInsforge() {
1778
1778
  exchangeResetPasswordToken: () => Promise.resolve({ error: { message: "SSR mode" } }),
1779
1779
  loginWithOAuth: () => Promise.resolve(),
1780
1780
  getPublicAuthConfig: () => Promise.resolve(null),
1781
+ getSession: () => Promise.resolve(null),
1781
1782
  baseUrl: "",
1782
1783
  afterSignInUrl: "/"
1783
1784
  };
@@ -3646,8 +3647,36 @@ function SignInForm({
3646
3647
  ] })
3647
3648
  ] });
3648
3649
  }
3650
+
3651
+ // src/lib/hosted-auth.ts
3652
+ function isHostedAuthEnvironment() {
3653
+ if (typeof window === "undefined") {
3654
+ return false;
3655
+ }
3656
+ const { hostname, port, protocol } = window.location;
3657
+ if (hostname === "localhost" && port === "7130") {
3658
+ return true;
3659
+ }
3660
+ if (protocol === "https:" && hostname.endsWith(".insforge.app")) {
3661
+ return true;
3662
+ }
3663
+ return false;
3664
+ }
3665
+ function buildLegacyAuthUrl(redirectUrl, session) {
3666
+ const url = new URL(redirectUrl);
3667
+ url.searchParams.set("access_token", session.accessToken);
3668
+ url.searchParams.set("user_id", session.userId);
3669
+ url.searchParams.set("email", session.email);
3670
+ if (session.name) {
3671
+ url.searchParams.set("name", session.name);
3672
+ }
3673
+ if (session.csrfToken) {
3674
+ url.searchParams.set("csrf_token", session.csrfToken);
3675
+ }
3676
+ return url.toString();
3677
+ }
3649
3678
  function SignIn({ onError, ...uiProps }) {
3650
- const { signIn, verifyEmail, loginWithOAuth } = useInsforge();
3679
+ const { signIn, verifyEmail, loginWithOAuth, isSignedIn, getSession, afterSignInUrl } = useInsforge();
3651
3680
  const { authConfig } = usePublicAuthConfig();
3652
3681
  const [email, setEmail] = React2.useState("");
3653
3682
  const [password, setPassword] = React2.useState("");
@@ -3657,6 +3686,37 @@ function SignIn({ onError, ...uiProps }) {
3657
3686
  const [oauthLoading] = React2.useState(null);
3658
3687
  const searchParams = useSearchParams();
3659
3688
  const redirectUrl = searchParams.get("redirect");
3689
+ const isHandlingOAuthRedirectRef = React2.useRef(false);
3690
+ React2.useEffect(() => {
3691
+ async function handleOAuthComplete() {
3692
+ if (!isSignedIn || isHandlingOAuthRedirectRef.current) return;
3693
+ const isHosted = isHostedAuthEnvironment();
3694
+ if (isHosted && redirectUrl) {
3695
+ isHandlingOAuthRedirectRef.current = true;
3696
+ try {
3697
+ const session = await getSession();
3698
+ if (!session) {
3699
+ isHandlingOAuthRedirectRef.current = false;
3700
+ return;
3701
+ }
3702
+ const legacyUrl = buildLegacyAuthUrl(redirectUrl, {
3703
+ accessToken: session.accessToken,
3704
+ userId: session.user.id,
3705
+ email: session.user.email,
3706
+ name: session.user.profile?.name || ""
3707
+ });
3708
+ window.location.href = legacyUrl;
3709
+ } catch (err) {
3710
+ console.error("Failed to redirect after OAuth:", err);
3711
+ isHandlingOAuthRedirectRef.current = false;
3712
+ }
3713
+ } else if (!isHosted) {
3714
+ const finalUrl = redirectUrl || afterSignInUrl || "/";
3715
+ window.location.href = new URL(finalUrl, window.location.origin).toString();
3716
+ }
3717
+ }
3718
+ void handleOAuthComplete();
3719
+ }, [isSignedIn, redirectUrl, getSession, afterSignInUrl]);
3660
3720
  async function handleSubmit(e) {
3661
3721
  e.preventDefault();
3662
3722
  setLoading(true);
@@ -3716,7 +3776,7 @@ function SignIn({ onError, ...uiProps }) {
3716
3776
  }
3717
3777
  function handleOAuth(provider) {
3718
3778
  try {
3719
- void loginWithOAuth(provider, redirectUrl || "");
3779
+ void loginWithOAuth(provider, window.location.href);
3720
3780
  } catch (err) {
3721
3781
  const errorMessage = err instanceof Error ? err.message : "OAuth login failed";
3722
3782
  setError(errorMessage);
@@ -3868,7 +3928,7 @@ function createPasswordSchema(options) {
3868
3928
  }
3869
3929
  createPasswordSchema();
3870
3930
  function SignUp({ onError, emailRedirectTo, ...uiProps }) {
3871
- const { signUp, verifyEmail, loginWithOAuth } = useInsforge();
3931
+ const { signUp, verifyEmail, loginWithOAuth, isSignedIn, getSession, afterSignInUrl } = useInsforge();
3872
3932
  const { authConfig } = usePublicAuthConfig();
3873
3933
  const [email, setEmail] = React2.useState("");
3874
3934
  const [password, setPassword] = React2.useState("");
@@ -3878,6 +3938,37 @@ function SignUp({ onError, emailRedirectTo, ...uiProps }) {
3878
3938
  const [oauthLoading] = React2.useState(null);
3879
3939
  const searchParams = useSearchParams();
3880
3940
  const redirectUrl = searchParams.get("redirect");
3941
+ const isHandlingOAuthRedirectRef = React2.useRef(false);
3942
+ React2.useEffect(() => {
3943
+ async function handleOAuthComplete() {
3944
+ if (!isSignedIn || isHandlingOAuthRedirectRef.current) return;
3945
+ const isHosted = isHostedAuthEnvironment();
3946
+ if (isHosted && redirectUrl) {
3947
+ isHandlingOAuthRedirectRef.current = true;
3948
+ try {
3949
+ const session = await getSession();
3950
+ if (!session) {
3951
+ isHandlingOAuthRedirectRef.current = false;
3952
+ return;
3953
+ }
3954
+ const legacyUrl = buildLegacyAuthUrl(redirectUrl, {
3955
+ accessToken: session.accessToken,
3956
+ userId: session.user.id,
3957
+ email: session.user.email,
3958
+ name: session.user.profile?.name || ""
3959
+ });
3960
+ window.location.href = legacyUrl;
3961
+ } catch (err) {
3962
+ console.error("Failed to redirect after OAuth:", err);
3963
+ isHandlingOAuthRedirectRef.current = false;
3964
+ }
3965
+ } else if (!isHosted) {
3966
+ const finalUrl = redirectUrl || afterSignInUrl || "/";
3967
+ window.location.href = new URL(finalUrl, window.location.origin).toString();
3968
+ }
3969
+ }
3970
+ void handleOAuthComplete();
3971
+ }, [isSignedIn, redirectUrl, getSession, afterSignInUrl]);
3881
3972
  async function handleSubmit(e) {
3882
3973
  e.preventDefault();
3883
3974
  setLoading(true);
@@ -3963,7 +4054,7 @@ function SignUp({ onError, emailRedirectTo, ...uiProps }) {
3963
4054
  }
3964
4055
  function handleOAuth(provider) {
3965
4056
  try {
3966
- void loginWithOAuth(provider, redirectUrl || "");
4057
+ void loginWithOAuth(provider, window.location.href);
3967
4058
  } catch (err) {
3968
4059
  const errorMessage = err instanceof Error ? err.message : "OAuth login failed";
3969
4060
  setError(errorMessage);