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