@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.
package/dist/index.mjs CHANGED
@@ -2,16 +2,15 @@
2
2
  import axios from 'axios';
3
3
  import { UpfilesClient, ImageManager } from '@thetechfossil/upfiles';
4
4
  export { ConnectProjectDialog, ImageManager, ProjectFilesWidget, UpfilesClient, Uploader } from '@thetechfossil/upfiles';
5
- import React3, { createContext, forwardRef, useContext, useState, useCallback, useEffect, useRef, useMemo } from 'react';
5
+ import React, { createContext, forwardRef, useContext, useState, useCallback, useEffect, useRef, useMemo } from 'react';
6
6
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
7
7
 
8
8
  var __defProp = Object.defineProperty;
9
9
  var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
10
10
  get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
11
11
  }) : x)(function(x) {
12
- if (typeof require !== "undefined")
13
- return require.apply(this, arguments);
14
- throw new Error('Dynamic require of "' + x + '" is not supported');
12
+ if (typeof require !== "undefined") return require.apply(this, arguments);
13
+ throw Error('Dynamic require of "' + x + '" is not supported');
15
14
  });
16
15
  var __export = (target, all) => {
17
16
  for (var name in all)
@@ -69,6 +68,11 @@ var HttpClient = class {
69
68
  return Promise.reject(refreshError);
70
69
  }
71
70
  }
71
+ if (error.response && error.response.data && error.response.data.message) {
72
+ const customError = new Error(error.response.data.message);
73
+ customError.response = error.response;
74
+ return Promise.reject(customError);
75
+ }
72
76
  return Promise.reject(error);
73
77
  }
74
78
  );
@@ -188,8 +192,7 @@ var AuthService = class {
188
192
  return this.token;
189
193
  }
190
194
  getCurrentUser() {
191
- if (!this.token)
192
- return null;
195
+ if (!this.token) return null;
193
196
  try {
194
197
  const payload = JSON.parse(atob(this.token.split(".")[1]));
195
198
  return payload.user || null;
@@ -200,8 +203,7 @@ var AuthService = class {
200
203
  }
201
204
  // CSRF Token Management
202
205
  async refreshCsrfToken() {
203
- if (!this.config.csrfEnabled)
204
- return;
206
+ if (!this.config.csrfEnabled) return;
205
207
  try {
206
208
  const response = await this.httpClient.get("/api/v1/auth/csrf-token");
207
209
  if (response.csrfToken) {
@@ -507,7 +509,7 @@ var AuthService = class {
507
509
  return response;
508
510
  }
509
511
  };
510
- var ThemeContext = createContext({ theme: "light", mounted: false });
512
+ var ThemeContext = React.createContext({ theme: "light", mounted: false });
511
513
  function AuthThemeProvider({ children }) {
512
514
  const [theme, setTheme] = useState("light");
513
515
  const [mounted, setMounted] = useState(false);
@@ -574,11 +576,17 @@ var AuthProvider = ({ children, config }) => {
574
576
  const authenticated = authService.isAuthenticated();
575
577
  if (authenticated) {
576
578
  try {
577
- const currentUser = authService.getCurrentUser();
578
- setUser(currentUser);
579
+ const freshUser = await authService.getProfile();
580
+ setUser(freshUser);
579
581
  } catch (error) {
580
- console.error("Failed to get current user:", error);
581
- setUser(null);
582
+ console.error("Failed to fetch fresh user profile, falling back to token:", error);
583
+ try {
584
+ const currentUser = authService.getCurrentUser();
585
+ setUser(currentUser);
586
+ } catch (fallbackError) {
587
+ console.error("Failed to get current user from token:", fallbackError);
588
+ setUser(null);
589
+ }
582
590
  }
583
591
  } else {
584
592
  setUser(null);
@@ -1037,7 +1045,7 @@ try {
1037
1045
  } catch (error) {
1038
1046
  console.warn("react-phone-number-input not available, using fallback");
1039
1047
  }
1040
- var CustomPhoneInput = React3.forwardRef((props, ref) => /* @__PURE__ */ jsx(
1048
+ var CustomPhoneInput = React.forwardRef((props, ref) => /* @__PURE__ */ jsx(
1041
1049
  "input",
1042
1050
  {
1043
1051
  ...props,
@@ -1759,26 +1767,18 @@ var RegisterForm = ({
1759
1767
  const [confirmPassword, setConfirmPassword] = useState("");
1760
1768
  const [isLoading, setIsLoading] = useState(false);
1761
1769
  const [error, setError] = useState(null);
1762
- useState(false);
1763
- useState(false);
1770
+ const [showPassword, setShowPassword] = useState(false);
1771
+ const [showConfirmPassword, setShowConfirmPassword] = useState(false);
1764
1772
  const getPasswordStrength = (pwd) => {
1765
- if (!pwd)
1766
- return { strength: "weak", score: 0, label: "" };
1773
+ if (!pwd) return { strength: "weak", score: 0, label: "" };
1767
1774
  let score = 0;
1768
- if (pwd.length >= 6)
1769
- score++;
1770
- if (pwd.length >= 8)
1771
- score++;
1772
- if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd))
1773
- score++;
1774
- if (/\d/.test(pwd))
1775
- score++;
1776
- if (/[^a-zA-Z\d]/.test(pwd))
1777
- score++;
1778
- if (score <= 2)
1779
- return { strength: "weak", score, label: "Weak" };
1780
- if (score <= 3)
1781
- return { strength: "medium", score, label: "Medium" };
1775
+ if (pwd.length >= 6) score++;
1776
+ if (pwd.length >= 8) score++;
1777
+ if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd)) score++;
1778
+ if (/\d/.test(pwd)) score++;
1779
+ if (/[^a-zA-Z\d]/.test(pwd)) score++;
1780
+ if (score <= 2) return { strength: "weak", score, label: "Weak" };
1781
+ if (score <= 3) return { strength: "medium", score, label: "Medium" };
1782
1782
  return { strength: "strong", score, label: "Strong" };
1783
1783
  };
1784
1784
  getPasswordStrength(password);
@@ -1830,10 +1830,8 @@ var RegisterForm = ({
1830
1830
  password,
1831
1831
  frontendBaseUrl: typeof window !== "undefined" ? process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || window.location.origin : void 0
1832
1832
  };
1833
- if (email)
1834
- registerData.email = email;
1835
- if (phoneNumber)
1836
- registerData.phoneNumber = phoneNumber;
1833
+ if (email) registerData.email = email;
1834
+ if (phoneNumber) registerData.phoneNumber = phoneNumber;
1837
1835
  const response = await register(registerData);
1838
1836
  if (response.success) {
1839
1837
  onRegisterSuccess?.();
@@ -2263,8 +2261,7 @@ var OtpForm = ({
2263
2261
  }
2264
2262
  };
2265
2263
  const handleResendOtp = async () => {
2266
- if (resendCooldown > 0 || resendLoading)
2267
- return;
2264
+ if (resendCooldown > 0 || resendLoading) return;
2268
2265
  setResendLoading(true);
2269
2266
  setError(null);
2270
2267
  try {
@@ -3122,23 +3119,15 @@ var SignUp = ({ redirectUrl, appearance }) => {
3122
3119
  }
3123
3120
  }, [isSignedIn, redirectUrl]);
3124
3121
  const getPasswordStrength = (pwd, colors2) => {
3125
- if (!pwd)
3126
- return { strength: "weak", color: colors2.borderSecondary };
3122
+ if (!pwd) return { strength: "weak", color: colors2.borderSecondary };
3127
3123
  let score = 0;
3128
- if (pwd.length >= 6)
3129
- score++;
3130
- if (pwd.length >= 8)
3131
- score++;
3132
- if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd))
3133
- score++;
3134
- if (/\d/.test(pwd))
3135
- score++;
3136
- if (/[^a-zA-Z\d]/.test(pwd))
3137
- score++;
3138
- if (score <= 2)
3139
- return { strength: "weak", color: colors2.errorText };
3140
- if (score <= 3)
3141
- return { strength: "medium", color: colors2.warningText || "#fa4" };
3124
+ if (pwd.length >= 6) score++;
3125
+ if (pwd.length >= 8) score++;
3126
+ if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd)) score++;
3127
+ if (/\d/.test(pwd)) score++;
3128
+ if (/[^a-zA-Z\d]/.test(pwd)) score++;
3129
+ if (score <= 2) return { strength: "weak", color: colors2.errorText };
3130
+ if (score <= 3) return { strength: "medium", color: colors2.warningText || "#fa4" };
3142
3131
  return { strength: "strong", color: colors2.successText };
3143
3132
  };
3144
3133
  const passwordStrength = getPasswordStrength(password, colors);
@@ -3159,10 +3148,8 @@ var SignUp = ({ redirectUrl, appearance }) => {
3159
3148
  }
3160
3149
  try {
3161
3150
  const signUpData = { name, password };
3162
- if (email)
3163
- signUpData.email = email;
3164
- if (phoneNumber)
3165
- signUpData.phoneNumber = phoneNumber;
3151
+ if (email) signUpData.email = email;
3152
+ if (phoneNumber) signUpData.phoneNumber = phoneNumber;
3166
3153
  const response = await signUp(signUpData);
3167
3154
  if (response.success) {
3168
3155
  setSuccess("Registration successful! Please check your email to verify your account.");
@@ -3503,8 +3490,7 @@ var UserButton = ({ showName = false, appearance }) => {
3503
3490
  document.addEventListener("mousedown", handleClickOutside);
3504
3491
  return () => document.removeEventListener("mousedown", handleClickOutside);
3505
3492
  }, []);
3506
- if (!user)
3507
- return null;
3493
+ if (!user) return null;
3508
3494
  const getInitials = (name) => {
3509
3495
  return name.split(" ").map((n) => n[0]).join("").toUpperCase().slice(0, 2);
3510
3496
  };
@@ -3538,7 +3524,19 @@ var UserButton = ({ showName = false, appearance }) => {
3538
3524
  e.currentTarget.style.backgroundColor = "transparent";
3539
3525
  },
3540
3526
  children: [
3541
- /* @__PURE__ */ jsx("div", { style: {
3527
+ user.avatar ? /* @__PURE__ */ jsx(
3528
+ "img",
3529
+ {
3530
+ src: user.avatar,
3531
+ alt: user.name,
3532
+ style: {
3533
+ width: "36px",
3534
+ height: "36px",
3535
+ borderRadius: "50%",
3536
+ objectFit: "cover"
3537
+ }
3538
+ }
3539
+ ) : /* @__PURE__ */ jsx("div", { style: {
3542
3540
  width: "36px",
3543
3541
  height: "36px",
3544
3542
  borderRadius: "50%",
@@ -3575,7 +3573,19 @@ var UserButton = ({ showName = false, appearance }) => {
3575
3573
  alignItems: "center",
3576
3574
  gap: "12px"
3577
3575
  }, children: [
3578
- /* @__PURE__ */ jsx("div", { style: {
3576
+ user.avatar ? /* @__PURE__ */ jsx(
3577
+ "img",
3578
+ {
3579
+ src: user.avatar,
3580
+ alt: user.name,
3581
+ style: {
3582
+ width: "48px",
3583
+ height: "48px",
3584
+ borderRadius: "50%",
3585
+ objectFit: "cover"
3586
+ }
3587
+ }
3588
+ ) : /* @__PURE__ */ jsx("div", { style: {
3579
3589
  width: "48px",
3580
3590
  height: "48px",
3581
3591
  borderRadius: "50%",
@@ -3981,23 +3991,15 @@ var ResetPassword = ({ token, appearance }) => {
3981
3991
  }
3982
3992
  }, [resetToken]);
3983
3993
  const getPasswordStrength = (pwd) => {
3984
- if (!pwd)
3985
- return { strength: "weak", color: "#ddd" };
3994
+ if (!pwd) return { strength: "weak", color: "#ddd" };
3986
3995
  let score = 0;
3987
- if (pwd.length >= 6)
3988
- score++;
3989
- if (pwd.length >= 8)
3990
- score++;
3991
- if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd))
3992
- score++;
3993
- if (/\d/.test(pwd))
3994
- score++;
3995
- if (/[^a-zA-Z\d]/.test(pwd))
3996
- score++;
3997
- if (score <= 2)
3998
- return { strength: "weak", color: "#f44" };
3999
- if (score <= 3)
4000
- return { strength: "medium", color: "#fa4" };
3996
+ if (pwd.length >= 6) score++;
3997
+ if (pwd.length >= 8) score++;
3998
+ if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd)) score++;
3999
+ if (/\d/.test(pwd)) score++;
4000
+ if (/[^a-zA-Z\d]/.test(pwd)) score++;
4001
+ if (score <= 2) return { strength: "weak", color: "#f44" };
4002
+ if (score <= 3) return { strength: "medium", color: "#fa4" };
4001
4003
  return { strength: "strong", color: "#4f4" };
4002
4004
  };
4003
4005
  const passwordStrength = getPasswordStrength(password);
@@ -4235,23 +4237,15 @@ var ChangePassword = ({ onSuccess, appearance }) => {
4235
4237
  const [error, setError] = useState(null);
4236
4238
  const [success, setSuccess] = useState(false);
4237
4239
  const getPasswordStrength = (pwd) => {
4238
- if (!pwd)
4239
- return { strength: "weak", color: "#ddd" };
4240
+ if (!pwd) return { strength: "weak", color: "#ddd" };
4240
4241
  let score = 0;
4241
- if (pwd.length >= 6)
4242
- score++;
4243
- if (pwd.length >= 8)
4244
- score++;
4245
- if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd))
4246
- score++;
4247
- if (/\d/.test(pwd))
4248
- score++;
4249
- if (/[^a-zA-Z\d]/.test(pwd))
4250
- score++;
4251
- if (score <= 2)
4252
- return { strength: "weak", color: "#f44" };
4253
- if (score <= 3)
4254
- return { strength: "medium", color: "#fa4" };
4242
+ if (pwd.length >= 6) score++;
4243
+ if (pwd.length >= 8) score++;
4244
+ if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd)) score++;
4245
+ if (/\d/.test(pwd)) score++;
4246
+ if (/[^a-zA-Z\d]/.test(pwd)) score++;
4247
+ if (score <= 2) return { strength: "weak", color: "#f44" };
4248
+ if (score <= 3) return { strength: "medium", color: "#fa4" };
4255
4249
  return { strength: "strong", color: "#4f4" };
4256
4250
  };
4257
4251
  const passwordStrength = getPasswordStrength(newPassword);
@@ -4487,26 +4481,16 @@ var AvatarUploader = ({
4487
4481
  const handleSelect = async (image) => {
4488
4482
  setUploading(true);
4489
4483
  try {
4490
- const proxyUrl = `${upfilesConfig.baseUrl}/api/download?fileKey=${encodeURIComponent(image.key)}`;
4491
- const response = await fetch(proxyUrl, {
4492
- headers: upfilesConfig.apiKey ? {
4493
- [upfilesConfig.apiKeyHeader || "authorization"]: upfilesConfig.apiKey.startsWith("upk_") ? `Bearer ${upfilesConfig.apiKey}` : upfilesConfig.apiKey
4494
- } : {}
4495
- });
4496
- if (!response.ok) {
4497
- throw new Error("Failed to fetch image");
4498
- }
4499
- const blob = await response.blob();
4500
- const file = new File([blob], image.originalName, { type: image.contentType });
4501
- const result = await uploadAndUpdateAvatar(file);
4502
- if (result.success && result.user?.avatar) {
4503
- onUploadComplete?.(result.user.avatar);
4484
+ const { updateProfile } = useAuth();
4485
+ const response = await updateProfile({ avatar: image.url });
4486
+ if (response.success && response.user?.avatar) {
4487
+ onUploadComplete?.(response.user.avatar);
4504
4488
  setOpen(false);
4505
4489
  } else {
4506
- throw new Error(result.message || "Failed to update avatar");
4490
+ throw new Error(response.message || "Failed to update avatar");
4507
4491
  }
4508
4492
  } catch (error) {
4509
- const err = error instanceof Error ? error : new Error("Upload failed");
4493
+ const err = error instanceof Error ? error : new Error("Failed to update avatar");
4510
4494
  onError?.(err);
4511
4495
  } finally {
4512
4496
  setUploading(false);
@@ -4619,8 +4603,7 @@ var UserProfile = ({
4619
4603
  setIsLoading(false);
4620
4604
  }
4621
4605
  };
4622
- if (!user)
4623
- return null;
4606
+ if (!user) return null;
4624
4607
  return /* @__PURE__ */ jsxs("div", { style: { maxWidth: "700px", margin: "0 auto", padding: "20px" }, children: [
4625
4608
  /* @__PURE__ */ jsx("h2", { style: { marginBottom: "24px", fontSize: "24px", fontWeight: 600, color: colors.textPrimary }, children: "Profile Settings" }),
4626
4609
  error && /* @__PURE__ */ jsx("div", { style: {
@@ -4854,8 +4837,14 @@ var AvatarManager = ({
4854
4837
  gridClassName,
4855
4838
  maxFileSize = 5 * 1024 * 1024,
4856
4839
  // 5MB default
4840
+ maxFiles = 10,
4857
4841
  mode = "full",
4858
4842
  showDelete = false,
4843
+ autoRecordToDb = true,
4844
+ fetchThumbnails = true,
4845
+ projectId,
4846
+ deleteUrl,
4847
+ onDelete,
4859
4848
  upfilesConfig
4860
4849
  }) => {
4861
4850
  const { updateProfile } = useAuth();
@@ -4887,18 +4876,25 @@ var AvatarManager = ({
4887
4876
  apiKey: upfilesConfig.apiKey,
4888
4877
  apiKeyHeader: upfilesConfig.apiKeyHeader || "authorization",
4889
4878
  presignUrl: upfilesConfig.presignUrl,
4890
- presignPath: upfilesConfig.presignPath
4879
+ presignPath: upfilesConfig.presignPath,
4880
+ headers: upfilesConfig.headers,
4881
+ withCredentials: upfilesConfig.withCredentials
4891
4882
  },
4892
- folderPath: upfilesConfig.folderPath || "avatars/",
4883
+ projectId,
4884
+ folderPath: upfilesConfig.folderPath || "/",
4893
4885
  title,
4894
4886
  description,
4895
4887
  className,
4896
4888
  gridClassName,
4897
4889
  onSelect: handleSelect,
4890
+ onDelete,
4891
+ deleteUrl,
4892
+ autoRecordToDb,
4893
+ fetchThumbnails,
4898
4894
  maxFileSize,
4895
+ maxFiles,
4899
4896
  mode,
4900
- showDelete,
4901
- fetchThumbnails: true
4897
+ showDelete
4902
4898
  }
4903
4899
  );
4904
4900
  };
@@ -5017,5 +5013,5 @@ var AuthClient = class extends AuthService {
5017
5013
  };
5018
5014
 
5019
5015
  export { AuthFlow, AuthProvider, AuthService, AvatarManager, AvatarUploader, ChangePassword, EmailVerificationPage, ForgotPassword, HttpClient, LoginForm, OtpForm, ProtectedRoute, PublicRoute, RegisterForm, ResetPassword, SignIn, SignOut, SignUp, UserButton, UserProfile, VerifyEmail, node_exports as node, react_exports as react, useAuth };
5020
- //# sourceMappingURL=out.js.map
5016
+ //# sourceMappingURL=index.mjs.map
5021
5017
  //# sourceMappingURL=index.mjs.map