@insforge/react 0.2.3 → 0.2.5

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.
@@ -16,42 +16,23 @@ function useInsforge() {
16
16
  return context;
17
17
  }
18
18
  function usePublicAuthConfig() {
19
- const { baseUrl } = useInsforge();
20
- const [oauthProviders, setOAuthProviders] = useState([]);
19
+ const { getPublicAuthConfig } = useInsforge();
21
20
  const [emailConfig, setEmailConfig] = useState(null);
22
21
  const [isLoaded, setIsLoaded] = useState(false);
23
22
  useEffect(() => {
24
- let mounted = true;
25
23
  async function fetchConfig() {
26
- try {
27
- const response = await fetch(`${baseUrl}/api/auth/public-config`);
28
- if (!mounted) return;
29
- if (!response.ok) {
30
- console.warn("[usePublicAuthConfig] Failed to fetch public auth config:", response.statusText);
31
- setOAuthProviders([]);
32
- setEmailConfig(null);
33
- } else {
34
- const data = await response.json();
35
- const providerNames = data.providers?.map((p) => p.provider) || [];
36
- setOAuthProviders(providerNames);
37
- setEmailConfig(data.email || null);
38
- }
39
- setIsLoaded(true);
40
- } catch (error) {
41
- console.warn("[usePublicAuthConfig] Unexpected error:", error);
42
- if (mounted) {
43
- setOAuthProviders([]);
44
- setEmailConfig(null);
45
- setIsLoaded(true);
46
- }
24
+ const result = await getPublicAuthConfig();
25
+ if (result) {
26
+ setEmailConfig(result);
27
+ } else {
28
+ console.error("[usePublicAuthConfig] Failed to get public auth config");
29
+ setEmailConfig(null);
47
30
  }
31
+ setIsLoaded(true);
48
32
  }
49
33
  fetchConfig();
50
- return () => {
51
- mounted = false;
52
- };
53
- }, [baseUrl]);
54
- return { oauthProviders, emailConfig, isLoaded };
34
+ }, [getPublicAuthConfig]);
35
+ return { emailConfig, isLoaded };
55
36
  }
56
37
  function AuthBranding() {
57
38
  return /* @__PURE__ */ jsxs("div", { className: "bg-[#FAFAFA] px-2 py-4 flex flex-row justify-center items-center gap-1", children: [
@@ -935,7 +916,7 @@ function SignIn({
935
916
  ...uiProps
936
917
  }) {
937
918
  const { signIn, baseUrl } = useInsforge();
938
- const { oauthProviders, emailConfig } = usePublicAuthConfig();
919
+ const { emailConfig } = usePublicAuthConfig();
939
920
  const [email, setEmail] = useState("");
940
921
  const [password, setPassword] = useState("");
941
922
  const [error, setError] = useState("");
@@ -987,7 +968,9 @@ function SignIn({
987
968
  setOauthLoading(null);
988
969
  }
989
970
  }
990
- if (!emailConfig) return null;
971
+ if (!emailConfig) {
972
+ return null;
973
+ }
991
974
  return /* @__PURE__ */ jsx(
992
975
  SignInForm,
993
976
  {
@@ -999,7 +982,7 @@ function SignIn({
999
982
  error,
1000
983
  loading,
1001
984
  oauthLoading,
1002
- availableProviders: oauthProviders,
985
+ availableProviders: emailConfig?.oAuthProviders || [],
1003
986
  onOAuthClick: handleOAuth,
1004
987
  emailAuthConfig: emailConfig,
1005
988
  ...uiProps
@@ -1162,7 +1145,7 @@ function SignUp({
1162
1145
  ...uiProps
1163
1146
  }) {
1164
1147
  const { signUp, baseUrl } = useInsforge();
1165
- const { oauthProviders, emailConfig } = usePublicAuthConfig();
1148
+ const { emailConfig } = usePublicAuthConfig();
1166
1149
  const [email, setEmail] = useState("");
1167
1150
  const [password, setPassword] = useState("");
1168
1151
  const [error, setError] = useState("");
@@ -1219,7 +1202,9 @@ function SignUp({
1219
1202
  setOauthLoading(null);
1220
1203
  }
1221
1204
  }
1222
- if (!emailConfig) return null;
1205
+ if (!emailConfig) {
1206
+ return null;
1207
+ }
1223
1208
  return /* @__PURE__ */ jsx(
1224
1209
  SignUpForm,
1225
1210
  {
@@ -1231,7 +1216,7 @@ function SignUp({
1231
1216
  error,
1232
1217
  loading,
1233
1218
  oauthLoading,
1234
- availableProviders: oauthProviders,
1219
+ availableProviders: emailConfig?.oAuthProviders || [],
1235
1220
  onOAuthClick: handleOAuth,
1236
1221
  emailAuthConfig: emailConfig,
1237
1222
  ...uiProps
@@ -1422,7 +1407,7 @@ function InsforgeCallback({
1422
1407
  onRedirect
1423
1408
  }) {
1424
1409
  const isProcessingRef = useRef(false);
1425
- const { reloadAuth } = useInsforge();
1410
+ const { handleAuthCallback } = useInsforge();
1426
1411
  useEffect(() => {
1427
1412
  const processCallback = async () => {
1428
1413
  if (isProcessingRef.current) return;
@@ -1442,7 +1427,30 @@ function InsforgeCallback({
1442
1427
  }
1443
1428
  return;
1444
1429
  }
1445
- const result = await reloadAuth();
1430
+ const accessToken = searchParams.get("access_token") || searchParams.get("auth_token");
1431
+ const userId = searchParams.get("user_id");
1432
+ const email = searchParams.get("email");
1433
+ const name = searchParams.get("name");
1434
+ if (!accessToken) {
1435
+ const errorMsg = "no_token";
1436
+ if (onError) {
1437
+ onError(errorMsg);
1438
+ } else {
1439
+ const errorUrl = "/?error=" + encodeURIComponent(errorMsg);
1440
+ if (onRedirect) {
1441
+ onRedirect(errorUrl);
1442
+ } else {
1443
+ window.location.href = errorUrl;
1444
+ }
1445
+ }
1446
+ return;
1447
+ }
1448
+ const result = await handleAuthCallback({
1449
+ accessToken,
1450
+ userId: userId || void 0,
1451
+ email: email || void 0,
1452
+ name: name || void 0
1453
+ });
1446
1454
  if (!result.success) {
1447
1455
  const errorMsg = result.error || "authentication_failed";
1448
1456
  if (onError) {
@@ -1471,7 +1479,7 @@ function InsforgeCallback({
1471
1479
  }
1472
1480
  };
1473
1481
  processCallback();
1474
- }, []);
1482
+ }, [handleAuthCallback, redirectTo, onSuccess, onError, onRedirect]);
1475
1483
  const defaultLoading = /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center min-h-screen", children: /* @__PURE__ */ jsxs("div", { className: "text-center", children: [
1476
1484
  /* @__PURE__ */ jsx("h2", { className: "text-2xl font-semibold mb-4", children: "Completing authentication..." }),
1477
1485
  /* @__PURE__ */ jsx("div", { className: "animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600 mx-auto" })