@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.
package/dist/index.js CHANGED
@@ -107,6 +107,23 @@ function InsforgeProvider({
107
107
  }
108
108
  };
109
109
  }, []);
110
+ const getPublicAuthConfig = react.useCallback(async () => {
111
+ try {
112
+ const result = await insforge.auth.getPublicAuthConfig();
113
+ if (result.data) {
114
+ return result.data;
115
+ } else {
116
+ console.error("[InsforgeProvider] Failed to get public auth config:", result.error);
117
+ return null;
118
+ }
119
+ } catch (error) {
120
+ console.error(
121
+ "[InsforgeProvider] Failed to get public auth config:",
122
+ error
123
+ );
124
+ return null;
125
+ }
126
+ }, [insforge]);
110
127
  const handleAuthSuccess = react.useCallback(
111
128
  async (authToken, fallbackUser) => {
112
129
  const userResult = await insforge.auth.getCurrentUser();
@@ -143,6 +160,70 @@ function InsforgeProvider({
143
160
  },
144
161
  [insforge, onAuthChange, syncTokenToCookie]
145
162
  );
163
+ const handleAuthCallback = react.useCallback(
164
+ async (params) => {
165
+ try {
166
+ await insforge.auth.setSession({
167
+ accessToken: params.accessToken,
168
+ user: {
169
+ id: params.userId || "",
170
+ email: params.email || "",
171
+ name: params.name || "",
172
+ emailVerified: false,
173
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
174
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
175
+ }
176
+ });
177
+ const userResult = await insforge.auth.getCurrentUser();
178
+ if (!userResult.data) {
179
+ await insforge.auth.signOut();
180
+ if (clearCookie) {
181
+ try {
182
+ await clearCookie();
183
+ } catch (error) {
184
+ }
185
+ }
186
+ return { success: false, error: "invalid_token" };
187
+ }
188
+ const profile = userResult.data.profile;
189
+ const userData = {
190
+ id: userResult.data.user.id,
191
+ email: userResult.data.user.email,
192
+ name: profile?.nickname || params.name || "",
193
+ avatarUrl: profile?.avatarUrl || ""
194
+ };
195
+ setUser(userData);
196
+ if (onAuthChange) {
197
+ onAuthChange(userData);
198
+ }
199
+ if (syncTokenToCookie) {
200
+ try {
201
+ await syncTokenToCookie(params.accessToken);
202
+ } catch (error) {
203
+ }
204
+ }
205
+ return { success: true };
206
+ } catch (error) {
207
+ console.error("[InsforgeProvider] Auth callback failed:", error);
208
+ await insforge.auth.signOut();
209
+ if (clearCookie) {
210
+ try {
211
+ await clearCookie();
212
+ } catch (error2) {
213
+ }
214
+ }
215
+ setUser(null);
216
+ if (onAuthChange) {
217
+ onAuthChange(null);
218
+ }
219
+ return {
220
+ success: false,
221
+ error: error instanceof Error ? error.message : "authentication_failed"
222
+ };
223
+ }
224
+ },
225
+ [insforge, onAuthChange, syncTokenToCookie, clearCookie]
226
+ );
146
227
  const signIn = react.useCallback(
147
228
  async (email, password) => {
148
229
  const sdkResult = await insforge.auth.signInWithPassword({
@@ -238,7 +319,10 @@ function InsforgeProvider({
238
319
  );
239
320
  const resetPassword = react.useCallback(
240
321
  async (token, newPassword) => {
241
- const sdkResult = await insforge.auth.resetPassword({ newPassword, otp: token });
322
+ const sdkResult = await insforge.auth.resetPassword({
323
+ newPassword,
324
+ otp: token
325
+ });
242
326
  return sdkResult.data;
243
327
  },
244
328
  [insforge]
@@ -263,10 +347,12 @@ function InsforgeProvider({
263
347
  signOut,
264
348
  updateUser,
265
349
  reloadAuth: loadAuthState,
350
+ handleAuthCallback,
266
351
  baseUrl,
267
352
  sendPasswordResetCode,
268
353
  resetPassword,
269
- verifyEmail
354
+ verifyEmail,
355
+ getPublicAuthConfig
270
356
  },
271
357
  children
272
358
  }
@@ -280,42 +366,23 @@ function useInsforge() {
280
366
  return context;
281
367
  }
282
368
  function usePublicAuthConfig() {
283
- const { baseUrl } = useInsforge();
284
- const [oauthProviders, setOAuthProviders] = react.useState([]);
369
+ const { getPublicAuthConfig } = useInsforge();
285
370
  const [emailConfig, setEmailConfig] = react.useState(null);
286
371
  const [isLoaded, setIsLoaded] = react.useState(false);
287
372
  react.useEffect(() => {
288
- let mounted = true;
289
373
  async function fetchConfig() {
290
- try {
291
- const response = await fetch(`${baseUrl}/api/auth/public-config`);
292
- if (!mounted) return;
293
- if (!response.ok) {
294
- console.warn("[usePublicAuthConfig] Failed to fetch public auth config:", response.statusText);
295
- setOAuthProviders([]);
296
- setEmailConfig(null);
297
- } else {
298
- const data = await response.json();
299
- const providerNames = data.providers?.map((p) => p.provider) || [];
300
- setOAuthProviders(providerNames);
301
- setEmailConfig(data.email || null);
302
- }
303
- setIsLoaded(true);
304
- } catch (error) {
305
- console.warn("[usePublicAuthConfig] Unexpected error:", error);
306
- if (mounted) {
307
- setOAuthProviders([]);
308
- setEmailConfig(null);
309
- setIsLoaded(true);
310
- }
374
+ const result = await getPublicAuthConfig();
375
+ if (result) {
376
+ setEmailConfig(result);
377
+ } else {
378
+ console.error("[usePublicAuthConfig] Failed to get public auth config");
379
+ setEmailConfig(null);
311
380
  }
381
+ setIsLoaded(true);
312
382
  }
313
383
  fetchConfig();
314
- return () => {
315
- mounted = false;
316
- };
317
- }, [baseUrl]);
318
- return { oauthProviders, emailConfig, isLoaded };
384
+ }, [getPublicAuthConfig]);
385
+ return { emailConfig, isLoaded };
319
386
  }
320
387
  function AuthBranding() {
321
388
  return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-[#FAFAFA] px-2 py-4 flex flex-row justify-center items-center gap-1", children: [
@@ -1202,7 +1269,7 @@ function SignIn({
1202
1269
  ...uiProps
1203
1270
  }) {
1204
1271
  const { signIn, baseUrl } = useInsforge();
1205
- const { oauthProviders, emailConfig } = usePublicAuthConfig();
1272
+ const { emailConfig } = usePublicAuthConfig();
1206
1273
  const [email, setEmail] = react.useState("");
1207
1274
  const [password, setPassword] = react.useState("");
1208
1275
  const [error, setError] = react.useState("");
@@ -1254,7 +1321,9 @@ function SignIn({
1254
1321
  setOauthLoading(null);
1255
1322
  }
1256
1323
  }
1257
- if (!emailConfig) return null;
1324
+ if (!emailConfig) {
1325
+ return null;
1326
+ }
1258
1327
  return /* @__PURE__ */ jsxRuntime.jsx(
1259
1328
  SignInForm,
1260
1329
  {
@@ -1266,7 +1335,7 @@ function SignIn({
1266
1335
  error,
1267
1336
  loading,
1268
1337
  oauthLoading,
1269
- availableProviders: oauthProviders,
1338
+ availableProviders: emailConfig?.oAuthProviders || [],
1270
1339
  onOAuthClick: handleOAuth,
1271
1340
  emailAuthConfig: emailConfig,
1272
1341
  ...uiProps
@@ -1429,7 +1498,7 @@ function SignUp({
1429
1498
  ...uiProps
1430
1499
  }) {
1431
1500
  const { signUp, baseUrl } = useInsforge();
1432
- const { oauthProviders, emailConfig } = usePublicAuthConfig();
1501
+ const { emailConfig } = usePublicAuthConfig();
1433
1502
  const [email, setEmail] = react.useState("");
1434
1503
  const [password, setPassword] = react.useState("");
1435
1504
  const [error, setError] = react.useState("");
@@ -1486,7 +1555,9 @@ function SignUp({
1486
1555
  setOauthLoading(null);
1487
1556
  }
1488
1557
  }
1489
- if (!emailConfig) return null;
1558
+ if (!emailConfig) {
1559
+ return null;
1560
+ }
1490
1561
  return /* @__PURE__ */ jsxRuntime.jsx(
1491
1562
  SignUpForm,
1492
1563
  {
@@ -1498,7 +1569,7 @@ function SignUp({
1498
1569
  error,
1499
1570
  loading,
1500
1571
  oauthLoading,
1501
- availableProviders: oauthProviders,
1572
+ availableProviders: emailConfig?.oAuthProviders || [],
1502
1573
  onOAuthClick: handleOAuth,
1503
1574
  emailAuthConfig: emailConfig,
1504
1575
  ...uiProps
@@ -1689,7 +1760,7 @@ function InsforgeCallback({
1689
1760
  onRedirect
1690
1761
  }) {
1691
1762
  const isProcessingRef = react.useRef(false);
1692
- const { reloadAuth } = useInsforge();
1763
+ const { handleAuthCallback } = useInsforge();
1693
1764
  react.useEffect(() => {
1694
1765
  const processCallback = async () => {
1695
1766
  if (isProcessingRef.current) return;
@@ -1709,7 +1780,30 @@ function InsforgeCallback({
1709
1780
  }
1710
1781
  return;
1711
1782
  }
1712
- const result = await reloadAuth();
1783
+ const accessToken = searchParams.get("access_token") || searchParams.get("auth_token");
1784
+ const userId = searchParams.get("user_id");
1785
+ const email = searchParams.get("email");
1786
+ const name = searchParams.get("name");
1787
+ if (!accessToken) {
1788
+ const errorMsg = "no_token";
1789
+ if (onError) {
1790
+ onError(errorMsg);
1791
+ } else {
1792
+ const errorUrl = "/?error=" + encodeURIComponent(errorMsg);
1793
+ if (onRedirect) {
1794
+ onRedirect(errorUrl);
1795
+ } else {
1796
+ window.location.href = errorUrl;
1797
+ }
1798
+ }
1799
+ return;
1800
+ }
1801
+ const result = await handleAuthCallback({
1802
+ accessToken,
1803
+ userId: userId || void 0,
1804
+ email: email || void 0,
1805
+ name: name || void 0
1806
+ });
1713
1807
  if (!result.success) {
1714
1808
  const errorMsg = result.error || "authentication_failed";
1715
1809
  if (onError) {
@@ -1738,7 +1832,7 @@ function InsforgeCallback({
1738
1832
  }
1739
1833
  };
1740
1834
  processCallback();
1741
- }, []);
1835
+ }, [handleAuthCallback, redirectTo, onSuccess, onError, onRedirect]);
1742
1836
  const defaultLoading = /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex items-center justify-center min-h-screen", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-center", children: [
1743
1837
  /* @__PURE__ */ jsxRuntime.jsx("h2", { className: "text-2xl font-semibold mb-4", children: "Completing authentication..." }),
1744
1838
  /* @__PURE__ */ jsxRuntime.jsx("div", { className: "animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600 mx-auto" })