@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.
@@ -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);
@@ -3695,7 +3755,7 @@ function SignIn({ onError, ...uiProps }) {
3695
3755
  }
3696
3756
  function handleOAuth(provider) {
3697
3757
  try {
3698
- void loginWithOAuth(provider, redirectUrl || "");
3758
+ void loginWithOAuth(provider, window.location.href);
3699
3759
  } catch (err) {
3700
3760
  const errorMessage = err instanceof Error ? err.message : "OAuth login failed";
3701
3761
  setError(errorMessage);
@@ -3847,7 +3907,7 @@ function createPasswordSchema(options) {
3847
3907
  }
3848
3908
  createPasswordSchema();
3849
3909
  function SignUp({ onError, emailRedirectTo, ...uiProps }) {
3850
- const { signUp, verifyEmail, loginWithOAuth } = useInsforge();
3910
+ const { signUp, verifyEmail, loginWithOAuth, isSignedIn, getSession, afterSignInUrl } = useInsforge();
3851
3911
  const { authConfig } = usePublicAuthConfig();
3852
3912
  const [email, setEmail] = useState("");
3853
3913
  const [password, setPassword] = useState("");
@@ -3857,6 +3917,37 @@ function SignUp({ onError, emailRedirectTo, ...uiProps }) {
3857
3917
  const [oauthLoading] = useState(null);
3858
3918
  const searchParams = useSearchParams();
3859
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]);
3860
3951
  async function handleSubmit(e) {
3861
3952
  e.preventDefault();
3862
3953
  setLoading(true);
@@ -3942,7 +4033,7 @@ function SignUp({ onError, emailRedirectTo, ...uiProps }) {
3942
4033
  }
3943
4034
  function handleOAuth(provider) {
3944
4035
  try {
3945
- void loginWithOAuth(provider, redirectUrl || "");
4036
+ void loginWithOAuth(provider, window.location.href);
3946
4037
  } catch (err) {
3947
4038
  const errorMessage = err instanceof Error ? err.message : "OAuth login failed";
3948
4039
  setError(errorMessage);