@insforge/nextjs 0.6.6 → 0.6.8

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
51
  const refreshIntervalRef = useRef();
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,14 +301,47 @@ function useSession() {
381
301
  }
382
302
 
383
303
  // src/hooks/useOAuthProviders.ts
304
+ import { useState as useState2, useEffect as useEffect2 } from "react";
305
+ import { createClient as createClient2 } from "@insforge/sdk";
384
306
  function useOAuthProviders() {
385
- const { oauthProviders, isConfigLoaded } = useInsforge();
386
- return { providers: oauthProviders, isLoaded: isConfigLoaded };
307
+ const { baseUrl } = useInsforge();
308
+ const [providers, setProviders] = useState2([]);
309
+ const [isLoaded, setIsLoaded] = useState2(false);
310
+ useEffect2(() => {
311
+ let mounted = true;
312
+ async function fetchProviders() {
313
+ try {
314
+ const insforge = createClient2({ baseUrl });
315
+ const { data, error } = await insforge.auth.getOAuthProviders();
316
+ if (!mounted) return;
317
+ if (error) {
318
+ console.warn("[useOAuthProviders] Failed to fetch OAuth providers:", error);
319
+ setProviders([]);
320
+ } else if (data) {
321
+ setProviders(data);
322
+ } else {
323
+ setProviders([]);
324
+ }
325
+ setIsLoaded(true);
326
+ } catch (error) {
327
+ console.warn("[useOAuthProviders] Unexpected error:", error);
328
+ if (mounted) {
329
+ setProviders([]);
330
+ setIsLoaded(true);
331
+ }
332
+ }
333
+ }
334
+ fetchProviders();
335
+ return () => {
336
+ mounted = false;
337
+ };
338
+ }, [baseUrl]);
339
+ return { providers, isLoaded };
387
340
  }
388
341
 
389
342
  // src/components/SignIn.tsx
390
- import { useState as useState3 } from "react";
391
- import { createClient as createClient2 } from "@insforge/sdk";
343
+ import { useState as useState4 } from "react";
344
+ import { createClient as createClient3 } from "@insforge/sdk";
392
345
 
393
346
  // src/components/auth/AuthBranding.tsx
394
347
  import Link from "next/link";
@@ -500,7 +453,7 @@ function AuthFormField({ label, id, className = "", ...props }) {
500
453
  }
501
454
 
502
455
  // src/components/auth/AuthPasswordField.tsx
503
- import { useState as useState2 } from "react";
456
+ import { useState as useState3 } from "react";
504
457
  import { Eye, EyeOff } from "lucide-react";
505
458
 
506
459
  // src/components/auth/AuthPasswordStrengthIndicator.tsx
@@ -556,8 +509,8 @@ function AuthPasswordField({
556
509
  onFocus,
557
510
  ...props
558
511
  }) {
559
- const [showPassword, setShowPassword] = useState2(false);
560
- const [showStrength, setShowStrength] = useState2(false);
512
+ const [showPassword, setShowPassword] = useState3(false);
513
+ const [showStrength, setShowStrength] = useState3(false);
561
514
  const handleFocus = (e) => {
562
515
  if (showStrengthIndicator) {
563
516
  setShowStrength(true);
@@ -977,13 +930,14 @@ function SignIn({
977
930
  onSuccess,
978
931
  onError
979
932
  }) {
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];
933
+ const { signIn, baseUrl } = useInsforge();
934
+ const { providers: oauthProviders } = useOAuthProviders();
935
+ const [email, setEmail] = useState4("");
936
+ const [password, setPassword] = useState4("");
937
+ const [error, setError] = useState4("");
938
+ const [loading, setLoading] = useState4(false);
939
+ const [oauthLoading, setOauthLoading] = useState4(null);
940
+ const insforge = useState4(() => createClient3({ baseUrl }))[0];
987
941
  async function handleSubmit(e) {
988
942
  e.preventDefault();
989
943
  setLoading(true);
@@ -1046,11 +1000,7 @@ function SignIn({
1046
1000
  value: password,
1047
1001
  onChange: (e) => setPassword(e.target.value),
1048
1002
  required: true,
1049
- autoComplete: "current-password",
1050
- forgotPasswordLink: {
1051
- href: "#",
1052
- text: forgotPasswordText
1053
- }
1003
+ autoComplete: "current-password"
1054
1004
  }
1055
1005
  ),
1056
1006
  /* @__PURE__ */ jsx16(
@@ -1080,8 +1030,8 @@ function SignIn({
1080
1030
  }
1081
1031
 
1082
1032
  // src/components/SignUp.tsx
1083
- import { useState as useState4 } from "react";
1084
- import { createClient as createClient3 } from "@insforge/sdk";
1033
+ import { useState as useState5 } from "react";
1034
+ import { createClient as createClient4 } from "@insforge/sdk";
1085
1035
  import { Fragment as Fragment2, jsx as jsx17, jsxs as jsxs14 } from "react/jsx-runtime";
1086
1036
  function SignUp({
1087
1037
  afterSignUpUrl = "/",
@@ -1094,9 +1044,9 @@ function SignUp({
1094
1044
  passwordPlaceholder = "\u2022\u2022\u2022\u2022\u2022\u2022",
1095
1045
  submitButtonText = "Sign Up",
1096
1046
  loadingButtonText = "Creating account...",
1097
- verifyButtonText = "Continue",
1098
- loadingVerifyButtonText = "Verifying...",
1099
- verifiedButtonText = "Verified",
1047
+ // verifyButtonText = "Continue", // Commented out - email verification disabled for now
1048
+ // loadingVerifyButtonText = "Verifying...", // Commented out - email verification disabled for now
1049
+ // verifiedButtonText = "Verified", // Commented out - email verification disabled for now
1100
1050
  signInText = "Already have an account?",
1101
1051
  signInLinkText = "Login Now",
1102
1052
  signInUrl = "/sign-in",
@@ -1104,18 +1054,14 @@ function SignUp({
1104
1054
  onSuccess,
1105
1055
  onError
1106
1056
  }) {
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];
1057
+ const { signUp, baseUrl } = useInsforge();
1058
+ const { providers: oauthProviders } = useOAuthProviders();
1059
+ const [email, setEmail] = useState5("");
1060
+ const [password, setPassword] = useState5("");
1061
+ const [error, setError] = useState5("");
1062
+ const [loading, setLoading] = useState5(false);
1063
+ const [oauthLoading, setOauthLoading] = useState5(null);
1064
+ const insforge = useState5(() => createClient4({ baseUrl }))[0];
1119
1065
  async function handleCredentialsSubmit(e) {
1120
1066
  e.preventDefault();
1121
1067
  setLoading(true);
@@ -1126,53 +1072,20 @@ function SignUp({
1126
1072
  return;
1127
1073
  }
1128
1074
  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);
1075
+ await signUp(email, password);
1150
1076
  if (onSuccess) {
1151
- setVerified(true);
1152
1077
  const userResult = await insforge.auth.getCurrentUser();
1153
1078
  if (userResult.data) onSuccess(userResult.data);
1154
1079
  }
1155
1080
  window.location.href = afterSignUpUrl;
1156
1081
  } catch (err) {
1157
- const errorMessage = err.message || "Invalid verification code";
1082
+ const errorMessage = err.message || "Sign up failed";
1158
1083
  setError(errorMessage);
1159
1084
  if (onError) onError(new Error(errorMessage));
1160
1085
  } finally {
1161
1086
  setLoading(false);
1162
1087
  }
1163
1088
  }
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
1089
  async function handleOAuth(provider) {
1177
1090
  try {
1178
1091
  setOauthLoading(provider);
@@ -1192,105 +1105,64 @@ function SignUp({
1192
1105
  setOauthLoading(null);
1193
1106
  }
1194
1107
  }
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
- ] }),
1108
+ return /* @__PURE__ */ jsxs14(AuthContainer, { style: appearance.container, children: [
1109
+ /* @__PURE__ */ jsx17(AuthHeader, { title, subtitle }),
1110
+ /* @__PURE__ */ jsx17(AuthErrorBanner, { error }),
1111
+ /* @__PURE__ */ jsxs14("form", { onSubmit: handleCredentialsSubmit, className: "insforge-form", children: [
1237
1112
  /* @__PURE__ */ jsx17(
1238
- AuthLink,
1113
+ AuthFormField,
1239
1114
  {
1240
- text: signInText,
1241
- linkText: signInLinkText,
1242
- href: signInUrl
1115
+ id: "email",
1116
+ type: "email",
1117
+ label: emailLabel,
1118
+ placeholder: emailPlaceholder,
1119
+ value: email,
1120
+ onChange: (e) => setEmail(e.target.value),
1121
+ required: true,
1122
+ autoComplete: "email"
1243
1123
  }
1244
1124
  ),
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
1125
  /* @__PURE__ */ jsx17(
1264
- AuthVerificationCodeInput,
1126
+ AuthPasswordField,
1265
1127
  {
1266
- email,
1267
- value: verificationCode,
1268
- onChange: setVerificationCode,
1269
- disabled: loading
1128
+ id: "password",
1129
+ label: passwordLabel,
1130
+ placeholder: passwordPlaceholder,
1131
+ value: password,
1132
+ onChange: (e) => setPassword(e.target.value),
1133
+ required: true,
1134
+ minLength: 8,
1135
+ autoComplete: "new-password",
1136
+ showStrengthIndicator: true
1270
1137
  }
1271
1138
  ),
1272
1139
  /* @__PURE__ */ jsx17(
1273
1140
  AuthSubmitButton,
1274
1141
  {
1275
1142
  isLoading: loading,
1276
- disabled: loading,
1143
+ disabled: loading || oauthLoading !== null,
1277
1144
  style: appearance.button,
1278
- confirmed: verified,
1279
- children: verified ? verifiedButtonText : loading ? loadingVerifyButtonText : verifyButtonText
1145
+ children: loading ? loadingButtonText : submitButtonText
1280
1146
  }
1281
1147
  )
1282
1148
  ] }),
1283
- /* @__PURE__ */ jsxs14("div", { className: "insforge-resend-code", children: [
1284
- "Did not received the code?",
1285
- " ",
1149
+ /* @__PURE__ */ jsx17(
1150
+ AuthLink,
1151
+ {
1152
+ text: signInText,
1153
+ linkText: signInLinkText,
1154
+ href: signInUrl
1155
+ }
1156
+ ),
1157
+ oauthProviders.length > 0 && /* @__PURE__ */ jsxs14(Fragment2, { children: [
1158
+ /* @__PURE__ */ jsx17(AuthDivider, { text: dividerText }),
1286
1159
  /* @__PURE__ */ jsx17(
1287
- "button",
1160
+ AuthOAuthProviders,
1288
1161
  {
1289
- type: "button",
1290
- onClick: handleResendCode,
1291
- disabled: loading,
1292
- className: "insforge-resend-link",
1293
- children: "Click to resend"
1162
+ providers: oauthProviders,
1163
+ onClick: handleOAuth,
1164
+ disabled: loading || oauthLoading !== null,
1165
+ loading: oauthLoading
1294
1166
  }
1295
1167
  )
1296
1168
  ] })
@@ -1298,7 +1170,7 @@ function SignUp({
1298
1170
  }
1299
1171
 
1300
1172
  // src/components/UserButton.tsx
1301
- import { useState as useState5, useRef as useRef3, useEffect as useEffect2 } from "react";
1173
+ import { useState as useState6, useRef as useRef3, useEffect as useEffect3 } from "react";
1302
1174
  import { LogOut } from "lucide-react";
1303
1175
  import { jsx as jsx18, jsxs as jsxs15 } from "react/jsx-runtime";
1304
1176
  function UserButton({
@@ -1307,9 +1179,9 @@ function UserButton({
1307
1179
  appearance = {}
1308
1180
  }) {
1309
1181
  const { user, signOut } = useInsforge();
1310
- const [isOpen, setIsOpen] = useState5(false);
1182
+ const [isOpen, setIsOpen] = useState6(false);
1311
1183
  const dropdownRef = useRef3(null);
1312
- useEffect2(() => {
1184
+ useEffect3(() => {
1313
1185
  function handleClickOutside(event) {
1314
1186
  if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
1315
1187
  setIsOpen(false);
@@ -1374,7 +1246,7 @@ function SignedOut({ children }) {
1374
1246
  }
1375
1247
 
1376
1248
  // src/components/Protect.tsx
1377
- import { useEffect as useEffect3 } from "react";
1249
+ import { useEffect as useEffect4 } from "react";
1378
1250
  import { useRouter } from "next/navigation";
1379
1251
  import { Fragment as Fragment5, jsx as jsx21 } from "react/jsx-runtime";
1380
1252
  function Protect({
@@ -1385,7 +1257,7 @@ function Protect({
1385
1257
  }) {
1386
1258
  const { isSignedIn, isLoaded, user } = useInsforge();
1387
1259
  const router = useRouter();
1388
- useEffect3(() => {
1260
+ useEffect4(() => {
1389
1261
  if (isLoaded && !isSignedIn) {
1390
1262
  router.push(redirectTo);
1391
1263
  } else if (isLoaded && isSignedIn && condition && user) {