@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,15 +1,14 @@
1
1
  "use client";
2
2
  import axios from 'axios';
3
3
  import { UpfilesClient, ImageManager } from '@thetechfossil/upfiles';
4
- import React3, { createContext, forwardRef, useState, useCallback, useEffect, useContext, useMemo, useRef } from 'react';
4
+ import React, { createContext, forwardRef, useContext, useState, useCallback, useEffect, useMemo, useRef } from 'react';
5
5
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
6
6
 
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) {
@@ -644,7 +646,7 @@ var useAuth = (config) => {
644
646
  uploadAndUpdateAvatar
645
647
  };
646
648
  };
647
- var ThemeContext = createContext({ theme: "light", mounted: false });
649
+ var ThemeContext = React.createContext({ theme: "light", mounted: false });
648
650
  function AuthThemeProvider({ children }) {
649
651
  const [theme, setTheme] = useState("light");
650
652
  const [mounted, setMounted] = useState(false);
@@ -711,11 +713,17 @@ var AuthProvider = ({ children, config }) => {
711
713
  const authenticated = authService.isAuthenticated();
712
714
  if (authenticated) {
713
715
  try {
714
- const currentUser = authService.getCurrentUser();
715
- setUser(currentUser);
716
+ const freshUser = await authService.getProfile();
717
+ setUser(freshUser);
716
718
  } catch (error) {
717
- console.error("Failed to get current user:", error);
718
- setUser(null);
719
+ console.error("Failed to fetch fresh user profile, falling back to token:", error);
720
+ try {
721
+ const currentUser = authService.getCurrentUser();
722
+ setUser(currentUser);
723
+ } catch (fallbackError) {
724
+ console.error("Failed to get current user from token:", fallbackError);
725
+ setUser(null);
726
+ }
719
727
  }
720
728
  } else {
721
729
  setUser(null);
@@ -1031,7 +1039,7 @@ try {
1031
1039
  } catch (error) {
1032
1040
  console.warn("react-phone-number-input not available, using fallback");
1033
1041
  }
1034
- var CustomPhoneInput = React3.forwardRef((props, ref) => /* @__PURE__ */ jsx(
1042
+ var CustomPhoneInput = React.forwardRef((props, ref) => /* @__PURE__ */ jsx(
1035
1043
  "input",
1036
1044
  {
1037
1045
  ...props,
@@ -1753,26 +1761,18 @@ var RegisterForm = ({
1753
1761
  const [confirmPassword, setConfirmPassword] = useState("");
1754
1762
  const [isLoading, setIsLoading] = useState(false);
1755
1763
  const [error, setError] = useState(null);
1756
- useState(false);
1757
- useState(false);
1764
+ const [showPassword, setShowPassword] = useState(false);
1765
+ const [showConfirmPassword, setShowConfirmPassword] = useState(false);
1758
1766
  const getPasswordStrength = (pwd) => {
1759
- if (!pwd)
1760
- return { strength: "weak", score: 0, label: "" };
1767
+ if (!pwd) return { strength: "weak", score: 0, label: "" };
1761
1768
  let score = 0;
1762
- if (pwd.length >= 6)
1763
- score++;
1764
- if (pwd.length >= 8)
1765
- score++;
1766
- if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd))
1767
- score++;
1768
- if (/\d/.test(pwd))
1769
- score++;
1770
- if (/[^a-zA-Z\d]/.test(pwd))
1771
- score++;
1772
- if (score <= 2)
1773
- return { strength: "weak", score, label: "Weak" };
1774
- if (score <= 3)
1775
- return { strength: "medium", score, label: "Medium" };
1769
+ if (pwd.length >= 6) score++;
1770
+ if (pwd.length >= 8) score++;
1771
+ if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd)) score++;
1772
+ if (/\d/.test(pwd)) score++;
1773
+ if (/[^a-zA-Z\d]/.test(pwd)) score++;
1774
+ if (score <= 2) return { strength: "weak", score, label: "Weak" };
1775
+ if (score <= 3) return { strength: "medium", score, label: "Medium" };
1776
1776
  return { strength: "strong", score, label: "Strong" };
1777
1777
  };
1778
1778
  getPasswordStrength(password);
@@ -1824,10 +1824,8 @@ var RegisterForm = ({
1824
1824
  password,
1825
1825
  frontendBaseUrl: typeof window !== "undefined" ? process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || window.location.origin : void 0
1826
1826
  };
1827
- if (email)
1828
- registerData.email = email;
1829
- if (phoneNumber)
1830
- registerData.phoneNumber = phoneNumber;
1827
+ if (email) registerData.email = email;
1828
+ if (phoneNumber) registerData.phoneNumber = phoneNumber;
1831
1829
  const response = await register(registerData);
1832
1830
  if (response.success) {
1833
1831
  onRegisterSuccess?.();
@@ -2257,8 +2255,7 @@ var OtpForm = ({
2257
2255
  }
2258
2256
  };
2259
2257
  const handleResendOtp = async () => {
2260
- if (resendCooldown > 0 || resendLoading)
2261
- return;
2258
+ if (resendCooldown > 0 || resendLoading) return;
2262
2259
  setResendLoading(true);
2263
2260
  setError(null);
2264
2261
  try {
@@ -3116,23 +3113,15 @@ var SignUp = ({ redirectUrl, appearance }) => {
3116
3113
  }
3117
3114
  }, [isSignedIn, redirectUrl]);
3118
3115
  const getPasswordStrength = (pwd, colors2) => {
3119
- if (!pwd)
3120
- return { strength: "weak", color: colors2.borderSecondary };
3116
+ if (!pwd) return { strength: "weak", color: colors2.borderSecondary };
3121
3117
  let score = 0;
3122
- if (pwd.length >= 6)
3123
- score++;
3124
- if (pwd.length >= 8)
3125
- score++;
3126
- if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd))
3127
- score++;
3128
- if (/\d/.test(pwd))
3129
- score++;
3130
- if (/[^a-zA-Z\d]/.test(pwd))
3131
- score++;
3132
- if (score <= 2)
3133
- return { strength: "weak", color: colors2.errorText };
3134
- if (score <= 3)
3135
- return { strength: "medium", color: colors2.warningText || "#fa4" };
3118
+ if (pwd.length >= 6) score++;
3119
+ if (pwd.length >= 8) score++;
3120
+ if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd)) score++;
3121
+ if (/\d/.test(pwd)) score++;
3122
+ if (/[^a-zA-Z\d]/.test(pwd)) score++;
3123
+ if (score <= 2) return { strength: "weak", color: colors2.errorText };
3124
+ if (score <= 3) return { strength: "medium", color: colors2.warningText || "#fa4" };
3136
3125
  return { strength: "strong", color: colors2.successText };
3137
3126
  };
3138
3127
  const passwordStrength = getPasswordStrength(password, colors);
@@ -3153,10 +3142,8 @@ var SignUp = ({ redirectUrl, appearance }) => {
3153
3142
  }
3154
3143
  try {
3155
3144
  const signUpData = { name, password };
3156
- if (email)
3157
- signUpData.email = email;
3158
- if (phoneNumber)
3159
- signUpData.phoneNumber = phoneNumber;
3145
+ if (email) signUpData.email = email;
3146
+ if (phoneNumber) signUpData.phoneNumber = phoneNumber;
3160
3147
  const response = await signUp(signUpData);
3161
3148
  if (response.success) {
3162
3149
  setSuccess("Registration successful! Please check your email to verify your account.");
@@ -3497,8 +3484,7 @@ var UserButton = ({ showName = false, appearance }) => {
3497
3484
  document.addEventListener("mousedown", handleClickOutside);
3498
3485
  return () => document.removeEventListener("mousedown", handleClickOutside);
3499
3486
  }, []);
3500
- if (!user)
3501
- return null;
3487
+ if (!user) return null;
3502
3488
  const getInitials = (name) => {
3503
3489
  return name.split(" ").map((n) => n[0]).join("").toUpperCase().slice(0, 2);
3504
3490
  };
@@ -3532,7 +3518,19 @@ var UserButton = ({ showName = false, appearance }) => {
3532
3518
  e.currentTarget.style.backgroundColor = "transparent";
3533
3519
  },
3534
3520
  children: [
3535
- /* @__PURE__ */ jsx("div", { style: {
3521
+ user.avatar ? /* @__PURE__ */ jsx(
3522
+ "img",
3523
+ {
3524
+ src: user.avatar,
3525
+ alt: user.name,
3526
+ style: {
3527
+ width: "36px",
3528
+ height: "36px",
3529
+ borderRadius: "50%",
3530
+ objectFit: "cover"
3531
+ }
3532
+ }
3533
+ ) : /* @__PURE__ */ jsx("div", { style: {
3536
3534
  width: "36px",
3537
3535
  height: "36px",
3538
3536
  borderRadius: "50%",
@@ -3569,7 +3567,19 @@ var UserButton = ({ showName = false, appearance }) => {
3569
3567
  alignItems: "center",
3570
3568
  gap: "12px"
3571
3569
  }, children: [
3572
- /* @__PURE__ */ jsx("div", { style: {
3570
+ user.avatar ? /* @__PURE__ */ jsx(
3571
+ "img",
3572
+ {
3573
+ src: user.avatar,
3574
+ alt: user.name,
3575
+ style: {
3576
+ width: "48px",
3577
+ height: "48px",
3578
+ borderRadius: "50%",
3579
+ objectFit: "cover"
3580
+ }
3581
+ }
3582
+ ) : /* @__PURE__ */ jsx("div", { style: {
3573
3583
  width: "48px",
3574
3584
  height: "48px",
3575
3585
  borderRadius: "50%",
@@ -3975,23 +3985,15 @@ var ResetPassword = ({ token, appearance }) => {
3975
3985
  }
3976
3986
  }, [resetToken]);
3977
3987
  const getPasswordStrength = (pwd) => {
3978
- if (!pwd)
3979
- return { strength: "weak", color: "#ddd" };
3988
+ if (!pwd) return { strength: "weak", color: "#ddd" };
3980
3989
  let score = 0;
3981
- if (pwd.length >= 6)
3982
- score++;
3983
- if (pwd.length >= 8)
3984
- score++;
3985
- if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd))
3986
- score++;
3987
- if (/\d/.test(pwd))
3988
- score++;
3989
- if (/[^a-zA-Z\d]/.test(pwd))
3990
- score++;
3991
- if (score <= 2)
3992
- return { strength: "weak", color: "#f44" };
3993
- if (score <= 3)
3994
- return { strength: "medium", color: "#fa4" };
3990
+ if (pwd.length >= 6) score++;
3991
+ if (pwd.length >= 8) score++;
3992
+ if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd)) score++;
3993
+ if (/\d/.test(pwd)) score++;
3994
+ if (/[^a-zA-Z\d]/.test(pwd)) score++;
3995
+ if (score <= 2) return { strength: "weak", color: "#f44" };
3996
+ if (score <= 3) return { strength: "medium", color: "#fa4" };
3995
3997
  return { strength: "strong", color: "#4f4" };
3996
3998
  };
3997
3999
  const passwordStrength = getPasswordStrength(password);
@@ -4229,23 +4231,15 @@ var ChangePassword = ({ onSuccess, appearance }) => {
4229
4231
  const [error, setError] = useState(null);
4230
4232
  const [success, setSuccess] = useState(false);
4231
4233
  const getPasswordStrength = (pwd) => {
4232
- if (!pwd)
4233
- return { strength: "weak", color: "#ddd" };
4234
+ if (!pwd) return { strength: "weak", color: "#ddd" };
4234
4235
  let score = 0;
4235
- if (pwd.length >= 6)
4236
- score++;
4237
- if (pwd.length >= 8)
4238
- score++;
4239
- if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd))
4240
- score++;
4241
- if (/\d/.test(pwd))
4242
- score++;
4243
- if (/[^a-zA-Z\d]/.test(pwd))
4244
- score++;
4245
- if (score <= 2)
4246
- return { strength: "weak", color: "#f44" };
4247
- if (score <= 3)
4248
- return { strength: "medium", color: "#fa4" };
4236
+ if (pwd.length >= 6) score++;
4237
+ if (pwd.length >= 8) score++;
4238
+ if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd)) score++;
4239
+ if (/\d/.test(pwd)) score++;
4240
+ if (/[^a-zA-Z\d]/.test(pwd)) score++;
4241
+ if (score <= 2) return { strength: "weak", color: "#f44" };
4242
+ if (score <= 3) return { strength: "medium", color: "#fa4" };
4249
4243
  return { strength: "strong", color: "#4f4" };
4250
4244
  };
4251
4245
  const passwordStrength = getPasswordStrength(newPassword);
@@ -4481,26 +4475,16 @@ var AvatarUploader = ({
4481
4475
  const handleSelect = async (image) => {
4482
4476
  setUploading(true);
4483
4477
  try {
4484
- const proxyUrl = `${upfilesConfig.baseUrl}/api/download?fileKey=${encodeURIComponent(image.key)}`;
4485
- const response = await fetch(proxyUrl, {
4486
- headers: upfilesConfig.apiKey ? {
4487
- [upfilesConfig.apiKeyHeader || "authorization"]: upfilesConfig.apiKey.startsWith("upk_") ? `Bearer ${upfilesConfig.apiKey}` : upfilesConfig.apiKey
4488
- } : {}
4489
- });
4490
- if (!response.ok) {
4491
- throw new Error("Failed to fetch image");
4492
- }
4493
- const blob = await response.blob();
4494
- const file = new File([blob], image.originalName, { type: image.contentType });
4495
- const result = await uploadAndUpdateAvatar(file);
4496
- if (result.success && result.user?.avatar) {
4497
- onUploadComplete?.(result.user.avatar);
4478
+ const { updateProfile } = useAuth2();
4479
+ const response = await updateProfile({ avatar: image.url });
4480
+ if (response.success && response.user?.avatar) {
4481
+ onUploadComplete?.(response.user.avatar);
4498
4482
  setOpen(false);
4499
4483
  } else {
4500
- throw new Error(result.message || "Failed to update avatar");
4484
+ throw new Error(response.message || "Failed to update avatar");
4501
4485
  }
4502
4486
  } catch (error) {
4503
- const err = error instanceof Error ? error : new Error("Upload failed");
4487
+ const err = error instanceof Error ? error : new Error("Failed to update avatar");
4504
4488
  onError?.(err);
4505
4489
  } finally {
4506
4490
  setUploading(false);
@@ -4613,8 +4597,7 @@ var UserProfile = ({
4613
4597
  setIsLoading(false);
4614
4598
  }
4615
4599
  };
4616
- if (!user)
4617
- return null;
4600
+ if (!user) return null;
4618
4601
  return /* @__PURE__ */ jsxs("div", { style: { maxWidth: "700px", margin: "0 auto", padding: "20px" }, children: [
4619
4602
  /* @__PURE__ */ jsx("h2", { style: { marginBottom: "24px", fontSize: "24px", fontWeight: 600, color: colors.textPrimary }, children: "Profile Settings" }),
4620
4603
  error && /* @__PURE__ */ jsx("div", { style: {
@@ -4848,8 +4831,14 @@ var AvatarManager = ({
4848
4831
  gridClassName,
4849
4832
  maxFileSize = 5 * 1024 * 1024,
4850
4833
  // 5MB default
4834
+ maxFiles = 10,
4851
4835
  mode = "full",
4852
4836
  showDelete = false,
4837
+ autoRecordToDb = true,
4838
+ fetchThumbnails = true,
4839
+ projectId,
4840
+ deleteUrl,
4841
+ onDelete,
4853
4842
  upfilesConfig
4854
4843
  }) => {
4855
4844
  const { updateProfile } = useAuth2();
@@ -4881,18 +4870,25 @@ var AvatarManager = ({
4881
4870
  apiKey: upfilesConfig.apiKey,
4882
4871
  apiKeyHeader: upfilesConfig.apiKeyHeader || "authorization",
4883
4872
  presignUrl: upfilesConfig.presignUrl,
4884
- presignPath: upfilesConfig.presignPath
4873
+ presignPath: upfilesConfig.presignPath,
4874
+ headers: upfilesConfig.headers,
4875
+ withCredentials: upfilesConfig.withCredentials
4885
4876
  },
4886
- folderPath: upfilesConfig.folderPath || "avatars/",
4877
+ projectId,
4878
+ folderPath: upfilesConfig.folderPath || "/",
4887
4879
  title,
4888
4880
  description,
4889
4881
  className,
4890
4882
  gridClassName,
4891
4883
  onSelect: handleSelect,
4884
+ onDelete,
4885
+ deleteUrl,
4886
+ autoRecordToDb,
4887
+ fetchThumbnails,
4892
4888
  maxFileSize,
4889
+ maxFiles,
4893
4890
  mode,
4894
- showDelete,
4895
- fetchThumbnails: true
4891
+ showDelete
4896
4892
  }
4897
4893
  );
4898
4894
  };
@@ -5089,5 +5085,5 @@ var useNextAuth = (config) => {
5089
5085
  };
5090
5086
 
5091
5087
  export { AuthFlow, AuthProvider, AuthService, AuthThemeProvider, AvatarManager, AvatarUploader, ChangePassword, EmailVerificationPage, ForgotPassword, HttpClient, LoginForm, OtpForm, PhoneInput, ProtectedRoute, PublicRoute, RegisterForm, ResetPassword, SignIn, SignOut, SignUp, UserButton, UserProfile, VerifyEmail, useAuth2 as useAuth, useAuth as useAuthLegacy, useAuthTheme, useNextAuth };
5092
- //# sourceMappingURL=out.js.map
5088
+ //# sourceMappingURL=index.next.mjs.map
5093
5089
  //# sourceMappingURL=index.next.mjs.map