@insforge/nextjs 0.6.7 → 0.6.9

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
@@ -5,20 +5,6 @@ import { createContext, useContext, useEffect, useState, useCallback, useRef } f
5
5
  import { createClient } from "@insforge/sdk";
6
6
  import { jsx } from "react/jsx-runtime";
7
7
  var InsforgeContext = createContext(void 0);
8
- async function fetchOAuthProviders(baseUrl) {
9
- try {
10
- const response = await fetch(`${baseUrl}/api/auth/oauth/configs`);
11
- if (!response.ok) return [];
12
- const result = await response.json();
13
- if (result?.data && Array.isArray(result.data)) {
14
- return result.data.map((config) => config.provider);
15
- }
16
- return [];
17
- } catch (error) {
18
- console.warn("Failed to fetch OAuth configs:", error);
19
- return [];
20
- }
21
- }
22
8
  function getTokenFromSDK() {
23
9
  console.log("[InsforgeProvider] Getting token from SDK");
24
10
  if (typeof window === "undefined") return null;
@@ -62,18 +48,8 @@ function InsforgeProvider({
62
48
  const [user, setUser] = useState(null);
63
49
  const [session, setSession] = useState(null);
64
50
  const [isLoaded, setIsLoaded] = useState(false);
65
- const [oauthProviders, setOauthProviders] = useState([]);
66
- const [isConfigLoaded, setIsConfigLoaded] = useState(false);
67
- const refreshIntervalRef = useRef();
51
+ const refreshIntervalRef = useRef(null);
68
52
  const [insforge] = useState(() => createClient({ baseUrl }));
69
- useEffect(() => {
70
- async function loadConfig() {
71
- const providers = await fetchOAuthProviders(baseUrl);
72
- setOauthProviders(providers);
73
- setIsConfigLoaded(true);
74
- }
75
- loadConfig();
76
- }, [baseUrl]);
77
53
  const loadAuthState = useCallback(async () => {
78
54
  try {
79
55
  const token = getTokenFromSDK();
@@ -273,59 +249,6 @@ function InsforgeProvider({
273
249
  },
274
250
  [user, onAuthChange, insforge]
275
251
  );
276
- const sendVerificationCode = useCallback(
277
- async (email, type) => {
278
- console.log(`[Verification] Sending ${type} code to ${email}`);
279
- console.log("[Verification] Dummy code: 123456");
280
- await new Promise((resolve) => setTimeout(resolve, 500));
281
- },
282
- [insforge]
283
- );
284
- const verifySignUpCode = useCallback(
285
- async (email, password, code) => {
286
- if (code !== "123456") {
287
- throw new Error("Invalid verification code");
288
- }
289
- const sdkResult = await insforge.auth.signUp({ email, password });
290
- if (sdkResult.data) {
291
- const userData = {
292
- id: sdkResult.data.user.id,
293
- email: sdkResult.data.user.email,
294
- name: sdkResult.data.user.name || void 0,
295
- createdAt: sdkResult.data.user.createdAt,
296
- updatedAt: sdkResult.data.user.updatedAt
297
- };
298
- const sessionData = {
299
- userId: sdkResult.data.user.id,
300
- token: sdkResult.data.accessToken,
301
- expiresAt: "",
302
- createdAt: (/* @__PURE__ */ new Date()).toISOString()
303
- };
304
- setUser(userData);
305
- setSession(sessionData);
306
- if (onAuthChange) {
307
- onAuthChange(userData);
308
- }
309
- try {
310
- await syncTokenToCookie(sdkResult.data.accessToken);
311
- } catch (error) {
312
- }
313
- } else {
314
- const errorMessage = sdkResult.error?.message || "Sign up failed";
315
- throw new Error(errorMessage);
316
- }
317
- },
318
- [insforge, onAuthChange]
319
- );
320
- const verifySignInCode = useCallback(
321
- async (email, code) => {
322
- if (code !== "123456") {
323
- throw new Error("Invalid verification code");
324
- }
325
- throw new Error("Passwordless sign in via verification code is not yet implemented in the backend");
326
- },
327
- [insforge, onAuthChange]
328
- );
329
252
  return /* @__PURE__ */ jsx(
330
253
  InsforgeContext.Provider,
331
254
  {
@@ -340,13 +263,10 @@ function InsforgeProvider({
340
263
  signUp,
341
264
  signOut,
342
265
  updateUser,
343
- // Verification
344
- sendVerificationCode,
345
- verifySignUpCode,
346
- verifySignInCode,
347
- // Config
348
- oauthProviders,
349
- isConfigLoaded,
266
+ // Email verification (commented out - verification disabled for now)
267
+ // sendVerificationCode,
268
+ // verifySignUpCode,
269
+ // verifySignInCode,
350
270
  // Base
351
271
  baseUrl
352
272
  },
@@ -381,13 +301,50 @@ function useSession() {
381
301
  }
382
302
 
383
303
  // src/hooks/useOAuthProviders.ts
304
+ import { useState as useState2, useEffect as useEffect2 } from "react";
384
305
  function useOAuthProviders() {
385
- const { oauthProviders, isConfigLoaded } = useInsforge();
386
- return { providers: oauthProviders, isLoaded: isConfigLoaded };
306
+ const { baseUrl } = useInsforge();
307
+ const [providers, setProviders] = useState2([]);
308
+ const [isLoaded, setIsLoaded] = useState2(false);
309
+ useEffect2(() => {
310
+ let mounted = true;
311
+ async function fetchProviders() {
312
+ try {
313
+ const response = await fetch(`${baseUrl}/api/auth/oauth/providers`);
314
+ if (!response.ok) {
315
+ if (mounted) {
316
+ setProviders([]);
317
+ setIsLoaded(true);
318
+ }
319
+ return;
320
+ }
321
+ const result = await response.json();
322
+ if (mounted) {
323
+ if (result?.data && Array.isArray(result.data)) {
324
+ setProviders(result.data);
325
+ } else {
326
+ setProviders([]);
327
+ }
328
+ setIsLoaded(true);
329
+ }
330
+ } catch (error) {
331
+ console.warn("[useOAuthProviders] Failed to fetch OAuth providers:", error);
332
+ if (mounted) {
333
+ setProviders([]);
334
+ setIsLoaded(true);
335
+ }
336
+ }
337
+ }
338
+ fetchProviders();
339
+ return () => {
340
+ mounted = false;
341
+ };
342
+ }, [baseUrl]);
343
+ return { providers, isLoaded };
387
344
  }
388
345
 
389
346
  // src/components/SignIn.tsx
390
- import { useState as useState3 } from "react";
347
+ import { useState as useState4 } from "react";
391
348
  import { createClient as createClient2 } from "@insforge/sdk";
392
349
 
393
350
  // src/components/auth/AuthBranding.tsx
@@ -500,7 +457,7 @@ function AuthFormField({ label, id, className = "", ...props }) {
500
457
  }
501
458
 
502
459
  // src/components/auth/AuthPasswordField.tsx
503
- import { useState as useState2 } from "react";
460
+ import { useState as useState3 } from "react";
504
461
  import { Eye, EyeOff } from "lucide-react";
505
462
 
506
463
  // src/components/auth/AuthPasswordStrengthIndicator.tsx
@@ -556,8 +513,8 @@ function AuthPasswordField({
556
513
  onFocus,
557
514
  ...props
558
515
  }) {
559
- const [showPassword, setShowPassword] = useState2(false);
560
- const [showStrength, setShowStrength] = useState2(false);
516
+ const [showPassword, setShowPassword] = useState3(false);
517
+ const [showStrength, setShowStrength] = useState3(false);
561
518
  const handleFocus = (e) => {
562
519
  if (showStrengthIndicator) {
563
520
  setShowStrength(true);
@@ -977,13 +934,14 @@ function SignIn({
977
934
  onSuccess,
978
935
  onError
979
936
  }) {
980
- const { signIn, oauthProviders, baseUrl } = useInsforge();
981
- const [email, setEmail] = useState3("");
982
- const [password, setPassword] = useState3("");
983
- const [error, setError] = useState3("");
984
- const [loading, setLoading] = useState3(false);
985
- const [oauthLoading, setOauthLoading] = useState3(null);
986
- const insforge = useState3(() => createClient2({ baseUrl }))[0];
937
+ const { signIn, baseUrl } = useInsforge();
938
+ const { providers: oauthProviders } = useOAuthProviders();
939
+ const [email, setEmail] = useState4("");
940
+ const [password, setPassword] = useState4("");
941
+ const [error, setError] = useState4("");
942
+ const [loading, setLoading] = useState4(false);
943
+ const [oauthLoading, setOauthLoading] = useState4(null);
944
+ const insforge = useState4(() => createClient2({ baseUrl }))[0];
987
945
  async function handleSubmit(e) {
988
946
  e.preventDefault();
989
947
  setLoading(true);
@@ -1046,11 +1004,7 @@ function SignIn({
1046
1004
  value: password,
1047
1005
  onChange: (e) => setPassword(e.target.value),
1048
1006
  required: true,
1049
- autoComplete: "current-password",
1050
- forgotPasswordLink: {
1051
- href: "#",
1052
- text: forgotPasswordText
1053
- }
1007
+ autoComplete: "current-password"
1054
1008
  }
1055
1009
  ),
1056
1010
  /* @__PURE__ */ jsx16(
@@ -1080,7 +1034,7 @@ function SignIn({
1080
1034
  }
1081
1035
 
1082
1036
  // src/components/SignUp.tsx
1083
- import { useState as useState4 } from "react";
1037
+ import { useState as useState5 } from "react";
1084
1038
  import { createClient as createClient3 } from "@insforge/sdk";
1085
1039
  import { Fragment as Fragment2, jsx as jsx17, jsxs as jsxs14 } from "react/jsx-runtime";
1086
1040
  function SignUp({
@@ -1094,9 +1048,9 @@ function SignUp({
1094
1048
  passwordPlaceholder = "\u2022\u2022\u2022\u2022\u2022\u2022",
1095
1049
  submitButtonText = "Sign Up",
1096
1050
  loadingButtonText = "Creating account...",
1097
- verifyButtonText = "Continue",
1098
- loadingVerifyButtonText = "Verifying...",
1099
- verifiedButtonText = "Verified",
1051
+ // verifyButtonText = "Continue", // Commented out - email verification disabled for now
1052
+ // loadingVerifyButtonText = "Verifying...", // Commented out - email verification disabled for now
1053
+ // verifiedButtonText = "Verified", // Commented out - email verification disabled for now
1100
1054
  signInText = "Already have an account?",
1101
1055
  signInLinkText = "Login Now",
1102
1056
  signInUrl = "/sign-in",
@@ -1104,18 +1058,14 @@ function SignUp({
1104
1058
  onSuccess,
1105
1059
  onError
1106
1060
  }) {
1107
- const { sendVerificationCode, verifySignUpCode, oauthProviders, baseUrl } = useInsforge();
1108
- const [email, setEmail] = useState4("");
1109
- const [password, setPassword] = useState4("");
1110
- const [verificationCode, setVerificationCode] = useState4("");
1111
- const [error, setError] = useState4("");
1112
- const [loading, setLoading] = useState4(false);
1113
- const [oauthLoading, setOauthLoading] = useState4(null);
1114
- const [verified, setVerified] = useState4(false);
1115
- const [step, setStep] = useState4(
1116
- "credentials"
1117
- );
1118
- const insforge = useState4(() => createClient3({ baseUrl }))[0];
1061
+ const { signUp, baseUrl } = useInsforge();
1062
+ const { providers: oauthProviders } = useOAuthProviders();
1063
+ const [email, setEmail] = useState5("");
1064
+ const [password, setPassword] = useState5("");
1065
+ const [error, setError] = useState5("");
1066
+ const [loading, setLoading] = useState5(false);
1067
+ const [oauthLoading, setOauthLoading] = useState5(null);
1068
+ const insforge = useState5(() => createClient3({ baseUrl }))[0];
1119
1069
  async function handleCredentialsSubmit(e) {
1120
1070
  e.preventDefault();
1121
1071
  setLoading(true);
@@ -1126,53 +1076,20 @@ function SignUp({
1126
1076
  return;
1127
1077
  }
1128
1078
  try {
1129
- await sendVerificationCode(email, "signup");
1130
- setStep("verification");
1131
- } catch (err) {
1132
- const errorMessage = err.message || "Failed to send verification code";
1133
- setError(errorMessage);
1134
- if (onError) onError(new Error(errorMessage));
1135
- } finally {
1136
- setLoading(false);
1137
- }
1138
- }
1139
- async function handleVerificationSubmit(e) {
1140
- e.preventDefault();
1141
- setLoading(true);
1142
- setError("");
1143
- if (verificationCode.length !== 6) {
1144
- setError("Please enter the complete verification code");
1145
- setLoading(false);
1146
- return;
1147
- }
1148
- try {
1149
- await verifySignUpCode(email, password, verificationCode);
1079
+ await signUp(email, password);
1150
1080
  if (onSuccess) {
1151
- setVerified(true);
1152
1081
  const userResult = await insforge.auth.getCurrentUser();
1153
1082
  if (userResult.data) onSuccess(userResult.data);
1154
1083
  }
1155
1084
  window.location.href = afterSignUpUrl;
1156
1085
  } catch (err) {
1157
- const errorMessage = err.message || "Invalid verification code";
1086
+ const errorMessage = err.message || "Sign up failed";
1158
1087
  setError(errorMessage);
1159
1088
  if (onError) onError(new Error(errorMessage));
1160
1089
  } finally {
1161
1090
  setLoading(false);
1162
1091
  }
1163
1092
  }
1164
- async function handleResendCode() {
1165
- setLoading(true);
1166
- setError("");
1167
- try {
1168
- await sendVerificationCode(email, "signup");
1169
- } catch (err) {
1170
- const errorMessage = err.message || "Failed to resend code";
1171
- setError(errorMessage);
1172
- } finally {
1173
- setLoading(false);
1174
- }
1175
- }
1176
1093
  async function handleOAuth(provider) {
1177
1094
  try {
1178
1095
  setOauthLoading(provider);
@@ -1192,105 +1109,64 @@ function SignUp({
1192
1109
  setOauthLoading(null);
1193
1110
  }
1194
1111
  }
1195
- if (step === "credentials") {
1196
- return /* @__PURE__ */ jsxs14(AuthContainer, { style: appearance.container, children: [
1197
- /* @__PURE__ */ jsx17(AuthHeader, { title, subtitle }),
1198
- /* @__PURE__ */ jsx17(AuthErrorBanner, { error }),
1199
- /* @__PURE__ */ jsxs14("form", { onSubmit: handleCredentialsSubmit, className: "insforge-form", children: [
1200
- /* @__PURE__ */ jsx17(
1201
- AuthFormField,
1202
- {
1203
- id: "email",
1204
- type: "email",
1205
- label: emailLabel,
1206
- placeholder: emailPlaceholder,
1207
- value: email,
1208
- onChange: (e) => setEmail(e.target.value),
1209
- required: true,
1210
- autoComplete: "email"
1211
- }
1212
- ),
1213
- /* @__PURE__ */ jsx17(
1214
- AuthPasswordField,
1215
- {
1216
- id: "password",
1217
- label: passwordLabel,
1218
- placeholder: passwordPlaceholder,
1219
- value: password,
1220
- onChange: (e) => setPassword(e.target.value),
1221
- required: true,
1222
- minLength: 8,
1223
- autoComplete: "new-password",
1224
- showStrengthIndicator: true
1225
- }
1226
- ),
1227
- /* @__PURE__ */ jsx17(
1228
- AuthSubmitButton,
1229
- {
1230
- isLoading: loading,
1231
- disabled: loading || oauthLoading !== null,
1232
- style: appearance.button,
1233
- children: loading ? loadingButtonText : submitButtonText
1234
- }
1235
- )
1236
- ] }),
1112
+ return /* @__PURE__ */ jsxs14(AuthContainer, { style: appearance.container, children: [
1113
+ /* @__PURE__ */ jsx17(AuthHeader, { title, subtitle }),
1114
+ /* @__PURE__ */ jsx17(AuthErrorBanner, { error }),
1115
+ /* @__PURE__ */ jsxs14("form", { onSubmit: handleCredentialsSubmit, className: "insforge-form", children: [
1237
1116
  /* @__PURE__ */ jsx17(
1238
- AuthLink,
1117
+ AuthFormField,
1239
1118
  {
1240
- text: signInText,
1241
- linkText: signInLinkText,
1242
- href: signInUrl
1119
+ id: "email",
1120
+ type: "email",
1121
+ label: emailLabel,
1122
+ placeholder: emailPlaceholder,
1123
+ value: email,
1124
+ onChange: (e) => setEmail(e.target.value),
1125
+ required: true,
1126
+ autoComplete: "email"
1243
1127
  }
1244
1128
  ),
1245
- oauthProviders.length > 0 && /* @__PURE__ */ jsxs14(Fragment2, { children: [
1246
- /* @__PURE__ */ jsx17(AuthDivider, { text: dividerText }),
1247
- /* @__PURE__ */ jsx17(
1248
- AuthOAuthProviders,
1249
- {
1250
- providers: oauthProviders,
1251
- onClick: handleOAuth,
1252
- disabled: loading || oauthLoading !== null,
1253
- loading: oauthLoading
1254
- }
1255
- )
1256
- ] })
1257
- ] });
1258
- }
1259
- return /* @__PURE__ */ jsxs14(AuthContainer, { style: appearance.container, children: [
1260
- /* @__PURE__ */ jsx17(AuthHeader, { title, subtitle }),
1261
- /* @__PURE__ */ jsx17(AuthErrorBanner, { error }),
1262
- /* @__PURE__ */ jsxs14("form", { onSubmit: handleVerificationSubmit, className: "insforge-form", children: [
1263
1129
  /* @__PURE__ */ jsx17(
1264
- AuthVerificationCodeInput,
1130
+ AuthPasswordField,
1265
1131
  {
1266
- email,
1267
- value: verificationCode,
1268
- onChange: setVerificationCode,
1269
- disabled: loading
1132
+ id: "password",
1133
+ label: passwordLabel,
1134
+ placeholder: passwordPlaceholder,
1135
+ value: password,
1136
+ onChange: (e) => setPassword(e.target.value),
1137
+ required: true,
1138
+ minLength: 8,
1139
+ autoComplete: "new-password",
1140
+ showStrengthIndicator: true
1270
1141
  }
1271
1142
  ),
1272
1143
  /* @__PURE__ */ jsx17(
1273
1144
  AuthSubmitButton,
1274
1145
  {
1275
1146
  isLoading: loading,
1276
- disabled: loading,
1147
+ disabled: loading || oauthLoading !== null,
1277
1148
  style: appearance.button,
1278
- confirmed: verified,
1279
- children: verified ? verifiedButtonText : loading ? loadingVerifyButtonText : verifyButtonText
1149
+ children: loading ? loadingButtonText : submitButtonText
1280
1150
  }
1281
1151
  )
1282
1152
  ] }),
1283
- /* @__PURE__ */ jsxs14("div", { className: "insforge-resend-code", children: [
1284
- "Did not received the code?",
1285
- " ",
1153
+ /* @__PURE__ */ jsx17(
1154
+ AuthLink,
1155
+ {
1156
+ text: signInText,
1157
+ linkText: signInLinkText,
1158
+ href: signInUrl
1159
+ }
1160
+ ),
1161
+ oauthProviders.length > 0 && /* @__PURE__ */ jsxs14(Fragment2, { children: [
1162
+ /* @__PURE__ */ jsx17(AuthDivider, { text: dividerText }),
1286
1163
  /* @__PURE__ */ jsx17(
1287
- "button",
1164
+ AuthOAuthProviders,
1288
1165
  {
1289
- type: "button",
1290
- onClick: handleResendCode,
1291
- disabled: loading,
1292
- className: "insforge-resend-link",
1293
- children: "Click to resend"
1166
+ providers: oauthProviders,
1167
+ onClick: handleOAuth,
1168
+ disabled: loading || oauthLoading !== null,
1169
+ loading: oauthLoading
1294
1170
  }
1295
1171
  )
1296
1172
  ] })
@@ -1298,7 +1174,7 @@ function SignUp({
1298
1174
  }
1299
1175
 
1300
1176
  // src/components/UserButton.tsx
1301
- import { useState as useState5, useRef as useRef3, useEffect as useEffect2 } from "react";
1177
+ import { useState as useState6, useRef as useRef3, useEffect as useEffect3 } from "react";
1302
1178
  import { LogOut } from "lucide-react";
1303
1179
  import { jsx as jsx18, jsxs as jsxs15 } from "react/jsx-runtime";
1304
1180
  function UserButton({
@@ -1307,9 +1183,9 @@ function UserButton({
1307
1183
  appearance = {}
1308
1184
  }) {
1309
1185
  const { user, signOut } = useInsforge();
1310
- const [isOpen, setIsOpen] = useState5(false);
1186
+ const [isOpen, setIsOpen] = useState6(false);
1311
1187
  const dropdownRef = useRef3(null);
1312
- useEffect2(() => {
1188
+ useEffect3(() => {
1313
1189
  function handleClickOutside(event) {
1314
1190
  if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
1315
1191
  setIsOpen(false);
@@ -1374,7 +1250,7 @@ function SignedOut({ children }) {
1374
1250
  }
1375
1251
 
1376
1252
  // src/components/Protect.tsx
1377
- import { useEffect as useEffect3 } from "react";
1253
+ import { useEffect as useEffect4 } from "react";
1378
1254
  import { useRouter } from "next/navigation";
1379
1255
  import { Fragment as Fragment5, jsx as jsx21 } from "react/jsx-runtime";
1380
1256
  function Protect({
@@ -1385,7 +1261,7 @@ function Protect({
1385
1261
  }) {
1386
1262
  const { isSignedIn, isLoaded, user } = useInsforge();
1387
1263
  const router = useRouter();
1388
- useEffect3(() => {
1264
+ useEffect4(() => {
1389
1265
  if (isLoaded && !isSignedIn) {
1390
1266
  router.push(redirectTo);
1391
1267
  } else if (isLoaded && isSignedIn && condition && user) {