@thetechfossil/auth2 1.2.14 → 1.2.16

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.
@@ -1,5 +1,5 @@
1
1
  "use client";
2
- import React2, { createContext, forwardRef, useContext, useState, useMemo, useEffect, useRef, useCallback } from 'react';
2
+ import React, { createContext, forwardRef, useContext, useState, useMemo, useEffect, useRef, useCallback } from 'react';
3
3
  import axios from 'axios';
4
4
  import { ImageManager, UpfilesClient } from '@thetechfossil/upfiles';
5
5
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
@@ -7,9 +7,8 @@ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
7
7
  var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
8
8
  get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
9
9
  }) : x)(function(x) {
10
- if (typeof require !== "undefined")
11
- return require.apply(this, arguments);
12
- throw new Error('Dynamic require of "' + x + '" is not supported');
10
+ if (typeof require !== "undefined") return require.apply(this, arguments);
11
+ throw Error('Dynamic require of "' + x + '" is not supported');
13
12
  });
14
13
  var HttpClient = class {
15
14
  constructor(baseUrl, defaultHeaders = {}) {
@@ -63,6 +62,11 @@ var HttpClient = class {
63
62
  return Promise.reject(refreshError);
64
63
  }
65
64
  }
65
+ if (error.response && error.response.data && error.response.data.message) {
66
+ const customError = new Error(error.response.data.message);
67
+ customError.response = error.response;
68
+ return Promise.reject(customError);
69
+ }
66
70
  return Promise.reject(error);
67
71
  }
68
72
  );
@@ -182,8 +186,7 @@ var AuthService = class {
182
186
  return this.token;
183
187
  }
184
188
  getCurrentUser() {
185
- if (!this.token)
186
- return null;
189
+ if (!this.token) return null;
187
190
  try {
188
191
  const payload = JSON.parse(atob(this.token.split(".")[1]));
189
192
  return payload.user || null;
@@ -194,8 +197,7 @@ var AuthService = class {
194
197
  }
195
198
  // CSRF Token Management
196
199
  async refreshCsrfToken() {
197
- if (!this.config.csrfEnabled)
198
- return;
200
+ if (!this.config.csrfEnabled) return;
199
201
  try {
200
202
  const response = await this.httpClient.get("/api/v1/auth/csrf-token");
201
203
  if (response.csrfToken) {
@@ -646,7 +648,7 @@ var useAuth = (config) => {
646
648
  uploadAndUpdateAvatar
647
649
  };
648
650
  };
649
- var ThemeContext = createContext({ theme: "light", mounted: false });
651
+ var ThemeContext = React.createContext({ theme: "light", mounted: false });
650
652
  function useAuthTheme() {
651
653
  return useContext(ThemeContext);
652
654
  }
@@ -700,7 +702,7 @@ try {
700
702
  } catch (error) {
701
703
  console.warn("react-phone-number-input not available, using fallback");
702
704
  }
703
- var CustomPhoneInput = React2.forwardRef((props, ref) => /* @__PURE__ */ jsx(
705
+ var CustomPhoneInput = React.forwardRef((props, ref) => /* @__PURE__ */ jsx(
704
706
  "input",
705
707
  {
706
708
  ...props,
@@ -1422,26 +1424,18 @@ var RegisterForm = ({
1422
1424
  const [confirmPassword, setConfirmPassword] = useState("");
1423
1425
  const [isLoading, setIsLoading] = useState(false);
1424
1426
  const [error, setError] = useState(null);
1425
- useState(false);
1426
- useState(false);
1427
+ const [showPassword, setShowPassword] = useState(false);
1428
+ const [showConfirmPassword, setShowConfirmPassword] = useState(false);
1427
1429
  const getPasswordStrength = (pwd) => {
1428
- if (!pwd)
1429
- return { strength: "weak", score: 0, label: "" };
1430
+ if (!pwd) return { strength: "weak", score: 0, label: "" };
1430
1431
  let score = 0;
1431
- if (pwd.length >= 6)
1432
- score++;
1433
- if (pwd.length >= 8)
1434
- score++;
1435
- if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd))
1436
- score++;
1437
- if (/\d/.test(pwd))
1438
- score++;
1439
- if (/[^a-zA-Z\d]/.test(pwd))
1440
- score++;
1441
- if (score <= 2)
1442
- return { strength: "weak", score, label: "Weak" };
1443
- if (score <= 3)
1444
- return { strength: "medium", score, label: "Medium" };
1432
+ if (pwd.length >= 6) score++;
1433
+ if (pwd.length >= 8) score++;
1434
+ if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd)) score++;
1435
+ if (/\d/.test(pwd)) score++;
1436
+ if (/[^a-zA-Z\d]/.test(pwd)) score++;
1437
+ if (score <= 2) return { strength: "weak", score, label: "Weak" };
1438
+ if (score <= 3) return { strength: "medium", score, label: "Medium" };
1445
1439
  return { strength: "strong", score, label: "Strong" };
1446
1440
  };
1447
1441
  getPasswordStrength(password);
@@ -1493,10 +1487,8 @@ var RegisterForm = ({
1493
1487
  password,
1494
1488
  frontendBaseUrl: typeof window !== "undefined" ? process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || window.location.origin : void 0
1495
1489
  };
1496
- if (email)
1497
- registerData.email = email;
1498
- if (phoneNumber)
1499
- registerData.phoneNumber = phoneNumber;
1490
+ if (email) registerData.email = email;
1491
+ if (phoneNumber) registerData.phoneNumber = phoneNumber;
1500
1492
  const response = await register(registerData);
1501
1493
  if (response.success) {
1502
1494
  onRegisterSuccess?.();
@@ -1926,8 +1918,7 @@ var OtpForm = ({
1926
1918
  }
1927
1919
  };
1928
1920
  const handleResendOtp = async () => {
1929
- if (resendCooldown > 0 || resendLoading)
1930
- return;
1921
+ if (resendCooldown > 0 || resendLoading) return;
1931
1922
  setResendLoading(true);
1932
1923
  setError(null);
1933
1924
  try {
@@ -2793,23 +2784,15 @@ var SignUp = ({ redirectUrl, appearance }) => {
2793
2784
  }
2794
2785
  }, [isSignedIn, redirectUrl]);
2795
2786
  const getPasswordStrength = (pwd, colors2) => {
2796
- if (!pwd)
2797
- return { strength: "weak", color: colors2.borderSecondary };
2787
+ if (!pwd) return { strength: "weak", color: colors2.borderSecondary };
2798
2788
  let score = 0;
2799
- if (pwd.length >= 6)
2800
- score++;
2801
- if (pwd.length >= 8)
2802
- score++;
2803
- if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd))
2804
- score++;
2805
- if (/\d/.test(pwd))
2806
- score++;
2807
- if (/[^a-zA-Z\d]/.test(pwd))
2808
- score++;
2809
- if (score <= 2)
2810
- return { strength: "weak", color: colors2.errorText };
2811
- if (score <= 3)
2812
- return { strength: "medium", color: colors2.warningText || "#fa4" };
2789
+ if (pwd.length >= 6) score++;
2790
+ if (pwd.length >= 8) score++;
2791
+ if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd)) score++;
2792
+ if (/\d/.test(pwd)) score++;
2793
+ if (/[^a-zA-Z\d]/.test(pwd)) score++;
2794
+ if (score <= 2) return { strength: "weak", color: colors2.errorText };
2795
+ if (score <= 3) return { strength: "medium", color: colors2.warningText || "#fa4" };
2813
2796
  return { strength: "strong", color: colors2.successText };
2814
2797
  };
2815
2798
  const passwordStrength = getPasswordStrength(password, colors);
@@ -2830,10 +2813,8 @@ var SignUp = ({ redirectUrl, appearance }) => {
2830
2813
  }
2831
2814
  try {
2832
2815
  const signUpData = { name, password };
2833
- if (email)
2834
- signUpData.email = email;
2835
- if (phoneNumber)
2836
- signUpData.phoneNumber = phoneNumber;
2816
+ if (email) signUpData.email = email;
2817
+ if (phoneNumber) signUpData.phoneNumber = phoneNumber;
2837
2818
  const response = await signUp(signUpData);
2838
2819
  if (response.success) {
2839
2820
  setSuccess("Registration successful! Please check your email to verify your account.");
@@ -3174,8 +3155,7 @@ var UserButton = ({ showName = false, appearance }) => {
3174
3155
  document.addEventListener("mousedown", handleClickOutside);
3175
3156
  return () => document.removeEventListener("mousedown", handleClickOutside);
3176
3157
  }, []);
3177
- if (!user)
3178
- return null;
3158
+ if (!user) return null;
3179
3159
  const getInitials = (name) => {
3180
3160
  return name.split(" ").map((n) => n[0]).join("").toUpperCase().slice(0, 2);
3181
3161
  };
@@ -3209,7 +3189,19 @@ var UserButton = ({ showName = false, appearance }) => {
3209
3189
  e.currentTarget.style.backgroundColor = "transparent";
3210
3190
  },
3211
3191
  children: [
3212
- /* @__PURE__ */ jsx("div", { style: {
3192
+ user.avatar ? /* @__PURE__ */ jsx(
3193
+ "img",
3194
+ {
3195
+ src: user.avatar,
3196
+ alt: user.name,
3197
+ style: {
3198
+ width: "36px",
3199
+ height: "36px",
3200
+ borderRadius: "50%",
3201
+ objectFit: "cover"
3202
+ }
3203
+ }
3204
+ ) : /* @__PURE__ */ jsx("div", { style: {
3213
3205
  width: "36px",
3214
3206
  height: "36px",
3215
3207
  borderRadius: "50%",
@@ -3246,7 +3238,19 @@ var UserButton = ({ showName = false, appearance }) => {
3246
3238
  alignItems: "center",
3247
3239
  gap: "12px"
3248
3240
  }, children: [
3249
- /* @__PURE__ */ jsx("div", { style: {
3241
+ user.avatar ? /* @__PURE__ */ jsx(
3242
+ "img",
3243
+ {
3244
+ src: user.avatar,
3245
+ alt: user.name,
3246
+ style: {
3247
+ width: "48px",
3248
+ height: "48px",
3249
+ borderRadius: "50%",
3250
+ objectFit: "cover"
3251
+ }
3252
+ }
3253
+ ) : /* @__PURE__ */ jsx("div", { style: {
3250
3254
  width: "48px",
3251
3255
  height: "48px",
3252
3256
  borderRadius: "50%",
@@ -3652,23 +3656,15 @@ var ResetPassword = ({ token, appearance }) => {
3652
3656
  }
3653
3657
  }, [resetToken]);
3654
3658
  const getPasswordStrength = (pwd) => {
3655
- if (!pwd)
3656
- return { strength: "weak", color: "#ddd" };
3659
+ if (!pwd) return { strength: "weak", color: "#ddd" };
3657
3660
  let score = 0;
3658
- if (pwd.length >= 6)
3659
- score++;
3660
- if (pwd.length >= 8)
3661
- score++;
3662
- if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd))
3663
- score++;
3664
- if (/\d/.test(pwd))
3665
- score++;
3666
- if (/[^a-zA-Z\d]/.test(pwd))
3667
- score++;
3668
- if (score <= 2)
3669
- return { strength: "weak", color: "#f44" };
3670
- if (score <= 3)
3671
- return { strength: "medium", color: "#fa4" };
3661
+ if (pwd.length >= 6) score++;
3662
+ if (pwd.length >= 8) score++;
3663
+ if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd)) score++;
3664
+ if (/\d/.test(pwd)) score++;
3665
+ if (/[^a-zA-Z\d]/.test(pwd)) score++;
3666
+ if (score <= 2) return { strength: "weak", color: "#f44" };
3667
+ if (score <= 3) return { strength: "medium", color: "#fa4" };
3672
3668
  return { strength: "strong", color: "#4f4" };
3673
3669
  };
3674
3670
  const passwordStrength = getPasswordStrength(password);
@@ -3906,23 +3902,15 @@ var ChangePassword = ({ onSuccess, appearance }) => {
3906
3902
  const [error, setError] = useState(null);
3907
3903
  const [success, setSuccess] = useState(false);
3908
3904
  const getPasswordStrength = (pwd) => {
3909
- if (!pwd)
3910
- return { strength: "weak", color: "#ddd" };
3905
+ if (!pwd) return { strength: "weak", color: "#ddd" };
3911
3906
  let score = 0;
3912
- if (pwd.length >= 6)
3913
- score++;
3914
- if (pwd.length >= 8)
3915
- score++;
3916
- if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd))
3917
- score++;
3918
- if (/\d/.test(pwd))
3919
- score++;
3920
- if (/[^a-zA-Z\d]/.test(pwd))
3921
- score++;
3922
- if (score <= 2)
3923
- return { strength: "weak", color: "#f44" };
3924
- if (score <= 3)
3925
- return { strength: "medium", color: "#fa4" };
3907
+ if (pwd.length >= 6) score++;
3908
+ if (pwd.length >= 8) score++;
3909
+ if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd)) score++;
3910
+ if (/\d/.test(pwd)) score++;
3911
+ if (/[^a-zA-Z\d]/.test(pwd)) score++;
3912
+ if (score <= 2) return { strength: "weak", color: "#f44" };
3913
+ if (score <= 3) return { strength: "medium", color: "#fa4" };
3926
3914
  return { strength: "strong", color: "#4f4" };
3927
3915
  };
3928
3916
  const passwordStrength = getPasswordStrength(newPassword);
@@ -4158,26 +4146,16 @@ var AvatarUploader = ({
4158
4146
  const handleSelect = async (image) => {
4159
4147
  setUploading(true);
4160
4148
  try {
4161
- const proxyUrl = `${upfilesConfig.baseUrl}/api/download?fileKey=${encodeURIComponent(image.key)}`;
4162
- const response = await fetch(proxyUrl, {
4163
- headers: upfilesConfig.apiKey ? {
4164
- [upfilesConfig.apiKeyHeader || "authorization"]: upfilesConfig.apiKey.startsWith("upk_") ? `Bearer ${upfilesConfig.apiKey}` : upfilesConfig.apiKey
4165
- } : {}
4166
- });
4167
- if (!response.ok) {
4168
- throw new Error("Failed to fetch image");
4169
- }
4170
- const blob = await response.blob();
4171
- const file = new File([blob], image.originalName, { type: image.contentType });
4172
- const result = await uploadAndUpdateAvatar(file);
4173
- if (result.success && result.user?.avatar) {
4174
- onUploadComplete?.(result.user.avatar);
4149
+ const { updateProfile } = useAuth2();
4150
+ const response = await updateProfile({ avatar: image.url });
4151
+ if (response.success && response.user?.avatar) {
4152
+ onUploadComplete?.(response.user.avatar);
4175
4153
  setOpen(false);
4176
4154
  } else {
4177
- throw new Error(result.message || "Failed to update avatar");
4155
+ throw new Error(response.message || "Failed to update avatar");
4178
4156
  }
4179
4157
  } catch (error) {
4180
- const err = error instanceof Error ? error : new Error("Upload failed");
4158
+ const err = error instanceof Error ? error : new Error("Failed to update avatar");
4181
4159
  onError?.(err);
4182
4160
  } finally {
4183
4161
  setUploading(false);
@@ -4290,8 +4268,7 @@ var UserProfile = ({
4290
4268
  setIsLoading(false);
4291
4269
  }
4292
4270
  };
4293
- if (!user)
4294
- return null;
4271
+ if (!user) return null;
4295
4272
  return /* @__PURE__ */ jsxs("div", { style: { maxWidth: "700px", margin: "0 auto", padding: "20px" }, children: [
4296
4273
  /* @__PURE__ */ jsx("h2", { style: { marginBottom: "24px", fontSize: "24px", fontWeight: 600, color: colors.textPrimary }, children: "Profile Settings" }),
4297
4274
  error && /* @__PURE__ */ jsx("div", { style: {
@@ -4525,8 +4502,14 @@ var AvatarManager = ({
4525
4502
  gridClassName,
4526
4503
  maxFileSize = 5 * 1024 * 1024,
4527
4504
  // 5MB default
4505
+ maxFiles = 10,
4528
4506
  mode = "full",
4529
4507
  showDelete = false,
4508
+ autoRecordToDb = true,
4509
+ fetchThumbnails = true,
4510
+ projectId,
4511
+ deleteUrl,
4512
+ onDelete,
4530
4513
  upfilesConfig
4531
4514
  }) => {
4532
4515
  const { updateProfile } = useAuth2();
@@ -4558,22 +4541,29 @@ var AvatarManager = ({
4558
4541
  apiKey: upfilesConfig.apiKey,
4559
4542
  apiKeyHeader: upfilesConfig.apiKeyHeader || "authorization",
4560
4543
  presignUrl: upfilesConfig.presignUrl,
4561
- presignPath: upfilesConfig.presignPath
4544
+ presignPath: upfilesConfig.presignPath,
4545
+ headers: upfilesConfig.headers,
4546
+ withCredentials: upfilesConfig.withCredentials
4562
4547
  },
4563
- folderPath: upfilesConfig.folderPath || "avatars/",
4548
+ projectId,
4549
+ folderPath: upfilesConfig.folderPath || "/",
4564
4550
  title,
4565
4551
  description,
4566
4552
  className,
4567
4553
  gridClassName,
4568
4554
  onSelect: handleSelect,
4555
+ onDelete,
4556
+ deleteUrl,
4557
+ autoRecordToDb,
4558
+ fetchThumbnails,
4569
4559
  maxFileSize,
4560
+ maxFiles,
4570
4561
  mode,
4571
- showDelete,
4572
- fetchThumbnails: true
4562
+ showDelete
4573
4563
  }
4574
4564
  );
4575
4565
  };
4576
4566
 
4577
4567
  export { AuthFlow, AvatarManager, AvatarUploader, ChangePassword, EmailVerificationPage, ForgotPassword, LoginForm, OtpForm, PhoneInput, ProtectedRoute, PublicRoute, RegisterForm, ResetPassword, SignIn, SignOut, SignUp, UserButton, UserProfile, VerifyEmail };
4578
- //# sourceMappingURL=out.js.map
4568
+ //# sourceMappingURL=index.components.mjs.map
4579
4569
  //# sourceMappingURL=index.components.mjs.map