@insforge/react 0.2.4 → 0.2.6

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
@@ -158,6 +158,70 @@ function InsforgeProvider({
158
158
  },
159
159
  [insforge, onAuthChange, syncTokenToCookie]
160
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
+ );
161
225
  const signIn = useCallback(
162
226
  async (email, password) => {
163
227
  const sdkResult = await insforge.auth.signInWithPassword({
@@ -281,6 +345,7 @@ function InsforgeProvider({
281
345
  signOut,
282
346
  updateUser,
283
347
  reloadAuth: loadAuthState,
348
+ handleAuthCallback,
284
349
  baseUrl,
285
350
  sendPasswordResetCode,
286
351
  resetPassword,
@@ -1195,10 +1260,8 @@ function SignInForm({
1195
1260
  );
1196
1261
  }
1197
1262
  function SignIn({
1198
- afterSignInUrl = "/",
1199
1263
  onSuccess,
1200
1264
  onError,
1201
- onRedirect,
1202
1265
  ...uiProps
1203
1266
  }) {
1204
1267
  const { signIn, baseUrl } = useInsforge();
@@ -1224,11 +1287,6 @@ function SignIn({
1224
1287
  if (onSuccess) {
1225
1288
  if (user) onSuccess(user, accessToken || "");
1226
1289
  }
1227
- if (onRedirect) {
1228
- onRedirect(afterSignInUrl);
1229
- } else {
1230
- window.location.href = afterSignInUrl;
1231
- }
1232
1290
  } catch (err) {
1233
1291
  const errorMessage = err.message || "Sign in failed";
1234
1292
  setError(errorMessage);
@@ -1242,7 +1300,6 @@ function SignIn({
1242
1300
  setOauthLoading(provider);
1243
1301
  setError("");
1244
1302
  const redirectTo = `${window.location.origin}/auth/callback`;
1245
- sessionStorage.setItem("oauth_final_destination", afterSignInUrl || "/");
1246
1303
  await insforge.auth.signInWithOAuth({
1247
1304
  provider,
1248
1305
  redirectTo
@@ -1424,10 +1481,8 @@ function SignUpForm({
1424
1481
  );
1425
1482
  }
1426
1483
  function SignUp({
1427
- afterSignUpUrl = "/",
1428
1484
  onSuccess,
1429
1485
  onError,
1430
- onRedirect,
1431
1486
  ...uiProps
1432
1487
  }) {
1433
1488
  const { signUp, baseUrl } = useInsforge();
@@ -1458,11 +1513,6 @@ function SignUp({
1458
1513
  if (onSuccess) {
1459
1514
  if (user) onSuccess(user, accessToken || "");
1460
1515
  }
1461
- if (onRedirect) {
1462
- onRedirect(afterSignUpUrl);
1463
- } else {
1464
- window.location.href = afterSignUpUrl;
1465
- }
1466
1516
  } catch (err) {
1467
1517
  const errorMessage = err.message || "Sign up failed";
1468
1518
  setError(errorMessage);
@@ -1476,7 +1526,6 @@ function SignUp({
1476
1526
  setOauthLoading(provider);
1477
1527
  setError("");
1478
1528
  const redirectTo = `${window.location.origin}/auth/callback`;
1479
- sessionStorage.setItem("oauth_final_destination", afterSignUpUrl || "/");
1480
1529
  await insforge.auth.signInWithOAuth({
1481
1530
  provider,
1482
1531
  redirectTo
@@ -1693,7 +1742,7 @@ function InsforgeCallback({
1693
1742
  onRedirect
1694
1743
  }) {
1695
1744
  const isProcessingRef = useRef(false);
1696
- const { reloadAuth } = useInsforge();
1745
+ const { handleAuthCallback } = useInsforge();
1697
1746
  useEffect(() => {
1698
1747
  const processCallback = async () => {
1699
1748
  if (isProcessingRef.current) return;
@@ -1713,7 +1762,30 @@ function InsforgeCallback({
1713
1762
  }
1714
1763
  return;
1715
1764
  }
1716
- const result = await reloadAuth();
1765
+ const accessToken = searchParams.get("access_token");
1766
+ const userId = searchParams.get("user_id");
1767
+ const email = searchParams.get("email");
1768
+ const name = searchParams.get("name");
1769
+ if (!accessToken) {
1770
+ const errorMsg = "no_token";
1771
+ if (onError) {
1772
+ onError(errorMsg);
1773
+ } else {
1774
+ const errorUrl = "/?error=" + encodeURIComponent(errorMsg);
1775
+ if (onRedirect) {
1776
+ onRedirect(errorUrl);
1777
+ } else {
1778
+ window.location.href = errorUrl;
1779
+ }
1780
+ }
1781
+ return;
1782
+ }
1783
+ const result = await handleAuthCallback({
1784
+ accessToken,
1785
+ userId: userId || void 0,
1786
+ email: email || void 0,
1787
+ name: name || void 0
1788
+ });
1717
1789
  if (!result.success) {
1718
1790
  const errorMsg = result.error || "authentication_failed";
1719
1791
  if (onError) {
@@ -1742,7 +1814,7 @@ function InsforgeCallback({
1742
1814
  }
1743
1815
  };
1744
1816
  processCallback();
1745
- }, []);
1817
+ }, [handleAuthCallback, redirectTo, onSuccess, onError, onRedirect]);
1746
1818
  const defaultLoading = /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center min-h-screen", children: /* @__PURE__ */ jsxs("div", { className: "text-center", children: [
1747
1819
  /* @__PURE__ */ jsx("h2", { className: "text-2xl font-semibold mb-4", children: "Completing authentication..." }),
1748
1820
  /* @__PURE__ */ jsx("div", { className: "animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600 mx-auto" })
@@ -2113,16 +2185,9 @@ function getInsforgeRoutes(config) {
2113
2185
  signUp = "/sign-up",
2114
2186
  verifyEmail = "/verify-email",
2115
2187
  forgotPassword = "/forgot-password",
2116
- resetPassword = "/reset-password",
2117
- callback = "/auth/callback"
2188
+ resetPassword = "/reset-password"
2118
2189
  } = paths;
2119
- const routes = [
2120
- // Always include callback route
2121
- {
2122
- path: callback,
2123
- element: /* @__PURE__ */ jsx(InsforgeCallback, {})
2124
- }
2125
- ];
2190
+ const routes = [];
2126
2191
  if (builtInAuth) {
2127
2192
  routes.push(
2128
2193
  {