@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.
package/dist/index.d.cts CHANGED
@@ -7,7 +7,7 @@ import { InsforgeUser, InsforgeContextValue, OAuthProvider } from '@insforge/sha
7
7
  export { InsforgeAuthConfig, InsforgeAuthMethods, InsforgeAuthState, InsforgeContextValue, InsforgeUser, OAuthProvider } from '@insforge/shared';
8
8
  import { InsForgeClient } from '@insforge/sdk';
9
9
  export { useAuth, usePublicAuthConfig, useUser } from './hooks.cjs';
10
- export { checkPasswordStrength, createPasswordSchema, emailSchema, passwordSchema, resolveAuthPath, resolveAuthUrl, validateEmail, validatePassword } from './lib.cjs';
10
+ export { LegacyAuthSession, buildLegacyAuthUrl, checkPasswordStrength, createPasswordSchema, emailSchema, isHostedAuthEnvironment, passwordSchema, resolveAuthPath, resolveAuthUrl, validateEmail, validatePassword } from './lib.cjs';
11
11
  import { OAuthProviderConfig } from './types.cjs';
12
12
  export { AuthConfig, EmailVerificationMethod } from './types.cjs';
13
13
  export { BrowserNavigationAdapter, NavigationAdapter, NavigationProvider, NavigationProviderProps, useNavigationAdapter, useSearchParams } from './navigation.cjs';
package/dist/index.d.ts CHANGED
@@ -7,7 +7,7 @@ import { InsforgeUser, InsforgeContextValue, OAuthProvider } from '@insforge/sha
7
7
  export { InsforgeAuthConfig, InsforgeAuthMethods, InsforgeAuthState, InsforgeContextValue, InsforgeUser, OAuthProvider } from '@insforge/shared';
8
8
  import { InsForgeClient } from '@insforge/sdk';
9
9
  export { useAuth, usePublicAuthConfig, useUser } from './hooks.js';
10
- export { checkPasswordStrength, createPasswordSchema, emailSchema, passwordSchema, resolveAuthPath, resolveAuthUrl, validateEmail, validatePassword } from './lib.js';
10
+ export { LegacyAuthSession, buildLegacyAuthUrl, checkPasswordStrength, createPasswordSchema, emailSchema, isHostedAuthEnvironment, passwordSchema, resolveAuthPath, resolveAuthUrl, validateEmail, validatePassword } from './lib.js';
11
11
  import { OAuthProviderConfig } from './types.js';
12
12
  export { AuthConfig, EmailVerificationMethod } from './types.js';
13
13
  export { BrowserNavigationAdapter, NavigationAdapter, NavigationProvider, NavigationProviderProps, useNavigationAdapter, useSearchParams } from './navigation.js';
package/dist/index.js CHANGED
@@ -821,6 +821,26 @@ var InsforgeManager = class _InsforgeManager {
821
821
  getSDK() {
822
822
  return this.sdk;
823
823
  }
824
+ /**
825
+ * Get current session with accessToken
826
+ * Used for hosted auth to construct legacy flow redirect URL
827
+ *
828
+ * @returns Session with accessToken and user, or null if not signed in
829
+ */
830
+ async getSession() {
831
+ const result = await this.sdk.auth.getCurrentSession();
832
+ if (result.data?.session) {
833
+ return {
834
+ accessToken: result.data.session.accessToken,
835
+ user: {
836
+ id: result.data.session.user.id,
837
+ email: result.data.session.user.email,
838
+ profile: result.data.session.user.profile
839
+ }
840
+ };
841
+ }
842
+ return null;
843
+ }
824
844
  // Cleanup
825
845
  cleanup() {
826
846
  if (this.refreshIntervalRef) {
@@ -2200,7 +2220,8 @@ function InsforgeProviderCore({
2200
2220
  verifyEmail: (otp, email) => manager.verifyEmail(otp, email),
2201
2221
  exchangeResetPasswordToken: (email, code) => manager.exchangeResetPasswordToken(email, code),
2202
2222
  loginWithOAuth: (provider, redirectTo) => manager.loginWithOAuth(provider, redirectTo),
2203
- getPublicAuthConfig: () => manager.getPublicAuthConfig()
2223
+ getPublicAuthConfig: () => manager.getPublicAuthConfig(),
2224
+ getSession: () => manager.getSession()
2204
2225
  }),
2205
2226
  [manager]
2206
2227
  // Only depends on manager (stable)
@@ -2260,6 +2281,7 @@ function useInsforge() {
2260
2281
  exchangeResetPasswordToken: () => Promise.resolve({ error: { message: "SSR mode" } }),
2261
2282
  loginWithOAuth: () => Promise.resolve(),
2262
2283
  getPublicAuthConfig: () => Promise.resolve(null),
2284
+ getSession: () => Promise.resolve(null),
2263
2285
  baseUrl: "",
2264
2286
  afterSignInUrl: "/"
2265
2287
  };
@@ -4140,8 +4162,36 @@ function SignInForm({
4140
4162
  ] })
4141
4163
  ] });
4142
4164
  }
4165
+
4166
+ // src/lib/hosted-auth.ts
4167
+ function isHostedAuthEnvironment() {
4168
+ if (typeof window === "undefined") {
4169
+ return false;
4170
+ }
4171
+ const { hostname, port, protocol } = window.location;
4172
+ if (hostname === "localhost" && port === "7130") {
4173
+ return true;
4174
+ }
4175
+ if (protocol === "https:" && hostname.endsWith(".insforge.app")) {
4176
+ return true;
4177
+ }
4178
+ return false;
4179
+ }
4180
+ function buildLegacyAuthUrl(redirectUrl, session) {
4181
+ const url = new URL(redirectUrl);
4182
+ url.searchParams.set("access_token", session.accessToken);
4183
+ url.searchParams.set("user_id", session.userId);
4184
+ url.searchParams.set("email", session.email);
4185
+ if (session.name) {
4186
+ url.searchParams.set("name", session.name);
4187
+ }
4188
+ if (session.csrfToken) {
4189
+ url.searchParams.set("csrf_token", session.csrfToken);
4190
+ }
4191
+ return url.toString();
4192
+ }
4143
4193
  function SignIn({ onError, ...uiProps }) {
4144
- const { signIn, verifyEmail, loginWithOAuth } = useInsforge();
4194
+ const { signIn, verifyEmail, loginWithOAuth, isSignedIn, getSession, afterSignInUrl } = useInsforge();
4145
4195
  const { authConfig } = usePublicAuthConfig();
4146
4196
  const [email, setEmail] = useState("");
4147
4197
  const [password, setPassword] = useState("");
@@ -4151,6 +4201,37 @@ function SignIn({ onError, ...uiProps }) {
4151
4201
  const [oauthLoading] = useState(null);
4152
4202
  const searchParams = useSearchParams();
4153
4203
  const redirectUrl = searchParams.get("redirect");
4204
+ const isHandlingOAuthRedirectRef = useRef(false);
4205
+ useEffect(() => {
4206
+ async function handleOAuthComplete() {
4207
+ if (!isSignedIn || isHandlingOAuthRedirectRef.current) return;
4208
+ const isHosted = isHostedAuthEnvironment();
4209
+ if (isHosted && redirectUrl) {
4210
+ isHandlingOAuthRedirectRef.current = true;
4211
+ try {
4212
+ const session = await getSession();
4213
+ if (!session) {
4214
+ isHandlingOAuthRedirectRef.current = false;
4215
+ return;
4216
+ }
4217
+ const legacyUrl = buildLegacyAuthUrl(redirectUrl, {
4218
+ accessToken: session.accessToken,
4219
+ userId: session.user.id,
4220
+ email: session.user.email,
4221
+ name: session.user.profile?.name || ""
4222
+ });
4223
+ window.location.href = legacyUrl;
4224
+ } catch (err) {
4225
+ console.error("Failed to redirect after OAuth:", err);
4226
+ isHandlingOAuthRedirectRef.current = false;
4227
+ }
4228
+ } else if (!isHosted) {
4229
+ const finalUrl = redirectUrl || afterSignInUrl || "/";
4230
+ window.location.href = new URL(finalUrl, window.location.origin).toString();
4231
+ }
4232
+ }
4233
+ void handleOAuthComplete();
4234
+ }, [isSignedIn, redirectUrl, getSession, afterSignInUrl]);
4154
4235
  async function handleSubmit(e) {
4155
4236
  e.preventDefault();
4156
4237
  setLoading(true);
@@ -4165,9 +4246,16 @@ function SignIn({ onError, ...uiProps }) {
4165
4246
  }
4166
4247
  throw new Error(result.error);
4167
4248
  }
4168
- const { user, redirectTo } = result;
4249
+ const { user, accessToken, redirectTo, csrfToken } = result;
4169
4250
  if (user) {
4170
4251
  const finalUrl = new URL(redirectTo || redirectUrl || "", window.location.origin);
4252
+ finalUrl.searchParams.set("access_token", accessToken);
4253
+ finalUrl.searchParams.set("user_id", user.id);
4254
+ finalUrl.searchParams.set("email", user.email);
4255
+ finalUrl.searchParams.set("name", user.profile?.name || "");
4256
+ if (csrfToken) {
4257
+ finalUrl.searchParams.set("csrf_token", csrfToken);
4258
+ }
4171
4259
  window.location.href = finalUrl.toString();
4172
4260
  }
4173
4261
  } catch (err) {
@@ -4188,6 +4276,13 @@ function SignIn({ onError, ...uiProps }) {
4188
4276
  throw new Error("Verification failed");
4189
4277
  }
4190
4278
  const finalUrl = new URL(result.redirectTo || redirectUrl || "", window.location.origin);
4279
+ finalUrl.searchParams.set("access_token", result.accessToken);
4280
+ finalUrl.searchParams.set("user_id", result.user.id);
4281
+ finalUrl.searchParams.set("email", result.user.email);
4282
+ finalUrl.searchParams.set("name", result.user.profile?.name || "");
4283
+ if (result.csrfToken) {
4284
+ finalUrl.searchParams.set("csrf_token", result.csrfToken);
4285
+ }
4191
4286
  window.location.href = finalUrl.toString();
4192
4287
  } catch (err) {
4193
4288
  const errorMessage = err instanceof Error ? err.message : "Invalid verification code";
@@ -4196,7 +4291,7 @@ function SignIn({ onError, ...uiProps }) {
4196
4291
  }
4197
4292
  function handleOAuth(provider) {
4198
4293
  try {
4199
- void loginWithOAuth(provider, redirectUrl || "");
4294
+ void loginWithOAuth(provider, window.location.href);
4200
4295
  } catch (err) {
4201
4296
  const errorMessage = err instanceof Error ? err.message : "OAuth login failed";
4202
4297
  setError(errorMessage);
@@ -4391,7 +4486,7 @@ function checkPasswordStrength(password) {
4391
4486
  return { score, feedback };
4392
4487
  }
4393
4488
  function SignUp({ onError, emailRedirectTo, ...uiProps }) {
4394
- const { signUp, verifyEmail, loginWithOAuth } = useInsforge();
4489
+ const { signUp, verifyEmail, loginWithOAuth, isSignedIn, getSession, afterSignInUrl } = useInsforge();
4395
4490
  const { authConfig } = usePublicAuthConfig();
4396
4491
  const [email, setEmail] = useState("");
4397
4492
  const [password, setPassword] = useState("");
@@ -4401,6 +4496,37 @@ function SignUp({ onError, emailRedirectTo, ...uiProps }) {
4401
4496
  const [oauthLoading] = useState(null);
4402
4497
  const searchParams = useSearchParams();
4403
4498
  const redirectUrl = searchParams.get("redirect");
4499
+ const isHandlingOAuthRedirectRef = useRef(false);
4500
+ useEffect(() => {
4501
+ async function handleOAuthComplete() {
4502
+ if (!isSignedIn || isHandlingOAuthRedirectRef.current) return;
4503
+ const isHosted = isHostedAuthEnvironment();
4504
+ if (isHosted && redirectUrl) {
4505
+ isHandlingOAuthRedirectRef.current = true;
4506
+ try {
4507
+ const session = await getSession();
4508
+ if (!session) {
4509
+ isHandlingOAuthRedirectRef.current = false;
4510
+ return;
4511
+ }
4512
+ const legacyUrl = buildLegacyAuthUrl(redirectUrl, {
4513
+ accessToken: session.accessToken,
4514
+ userId: session.user.id,
4515
+ email: session.user.email,
4516
+ name: session.user.profile?.name || ""
4517
+ });
4518
+ window.location.href = legacyUrl;
4519
+ } catch (err) {
4520
+ console.error("Failed to redirect after OAuth:", err);
4521
+ isHandlingOAuthRedirectRef.current = false;
4522
+ }
4523
+ } else if (!isHosted) {
4524
+ const finalUrl = redirectUrl || afterSignInUrl || "/";
4525
+ window.location.href = new URL(finalUrl, window.location.origin).toString();
4526
+ }
4527
+ }
4528
+ void handleOAuthComplete();
4529
+ }, [isSignedIn, redirectUrl, getSession, afterSignInUrl]);
4404
4530
  async function handleSubmit(e) {
4405
4531
  e.preventDefault();
4406
4532
  setLoading(true);
@@ -4442,7 +4568,15 @@ function SignUp({ onError, emailRedirectTo, ...uiProps }) {
4442
4568
  return;
4443
4569
  }
4444
4570
  if (result.accessToken && result.user) {
4571
+ const csrfToken = result.csrfToken;
4445
4572
  const finalUrl = new URL(result.redirectTo || redirectUrl || "", window.location.origin);
4573
+ finalUrl.searchParams.set("access_token", result.accessToken);
4574
+ finalUrl.searchParams.set("user_id", result.user.id);
4575
+ finalUrl.searchParams.set("email", result.user.email);
4576
+ finalUrl.searchParams.set("name", result.user.profile?.name || "");
4577
+ if (csrfToken) {
4578
+ finalUrl.searchParams.set("csrf_token", csrfToken);
4579
+ }
4446
4580
  window.location.href = finalUrl.toString();
4447
4581
  }
4448
4582
  } catch (err) {
@@ -4463,6 +4597,13 @@ function SignUp({ onError, emailRedirectTo, ...uiProps }) {
4463
4597
  throw new Error("Verification failed");
4464
4598
  }
4465
4599
  const finalUrl = new URL(result.redirectTo || redirectUrl || "", window.location.origin);
4600
+ finalUrl.searchParams.set("access_token", result.accessToken);
4601
+ finalUrl.searchParams.set("user_id", result.user.id);
4602
+ finalUrl.searchParams.set("email", result.user.email);
4603
+ finalUrl.searchParams.set("name", result.user.profile?.name || "");
4604
+ if (result.csrfToken) {
4605
+ finalUrl.searchParams.set("csrf_token", result.csrfToken);
4606
+ }
4466
4607
  window.location.href = finalUrl.toString();
4467
4608
  } catch (err) {
4468
4609
  const errorMessage = err instanceof Error ? err.message : "Invalid verification code";
@@ -4471,7 +4612,7 @@ function SignUp({ onError, emailRedirectTo, ...uiProps }) {
4471
4612
  }
4472
4613
  function handleOAuth(provider) {
4473
4614
  try {
4474
- void loginWithOAuth(provider, redirectUrl || "");
4615
+ void loginWithOAuth(provider, window.location.href);
4475
4616
  } catch (err) {
4476
4617
  const errorMessage = err instanceof Error ? err.message : "OAuth login failed";
4477
4618
  setError(errorMessage);
@@ -5780,6 +5921,6 @@ react-is/cjs/react-is.development.js:
5780
5921
  *)
5781
5922
  */
5782
5923
 
5783
- export { AuthBranding, AuthContainer, AuthDivider, AuthEmailVerificationStep, AuthErrorBanner, AuthFormField, AuthHeader, AuthLink, AuthOAuthButton, AuthOAuthProviders, AuthPasswordField, AuthPasswordStrengthIndicator, AuthResetPasswordVerificationStep, AuthSubmitButton, AuthVerificationCodeInput, BrowserNavigationAdapter, ForgotPassword, ForgotPasswordForm, InsforgeProvider, InsforgeProviderCore, NavigationProvider, OAUTH_PROVIDER_CONFIG, Protect, ResetPassword, ResetPasswordForm, SignIn, SignInButton, SignInForm, SignOutButton, SignUp, SignUpButton, SignUpForm, SignedIn, SignedOut, StyleProvider, UserButton, UserProfileModal, VerifyEmail, VerifyEmailStatus, checkPasswordStrength, createPasswordSchema, emailSchema, getAllProviderConfigs, getProviderConfig, passwordSchema, resolveAuthPath, resolveAuthUrl, theme, useAuth, useInsforge, useNavigationAdapter, usePublicAuthConfig, useSearchParams, useUser, validateEmail, validatePassword };
5924
+ export { AuthBranding, AuthContainer, AuthDivider, AuthEmailVerificationStep, AuthErrorBanner, AuthFormField, AuthHeader, AuthLink, AuthOAuthButton, AuthOAuthProviders, AuthPasswordField, AuthPasswordStrengthIndicator, AuthResetPasswordVerificationStep, AuthSubmitButton, AuthVerificationCodeInput, BrowserNavigationAdapter, ForgotPassword, ForgotPasswordForm, InsforgeProvider, InsforgeProviderCore, NavigationProvider, OAUTH_PROVIDER_CONFIG, Protect, ResetPassword, ResetPasswordForm, SignIn, SignInButton, SignInForm, SignOutButton, SignUp, SignUpButton, SignUpForm, SignedIn, SignedOut, StyleProvider, UserButton, UserProfileModal, VerifyEmail, VerifyEmailStatus, buildLegacyAuthUrl, checkPasswordStrength, createPasswordSchema, emailSchema, getAllProviderConfigs, getProviderConfig, isHostedAuthEnvironment, passwordSchema, resolveAuthPath, resolveAuthUrl, theme, useAuth, useInsforge, useNavigationAdapter, usePublicAuthConfig, useSearchParams, useUser, validateEmail, validatePassword };
5784
5925
  //# sourceMappingURL=index.js.map
5785
5926
  //# sourceMappingURL=index.js.map