@thetechfossil/auth2 1.2.15 → 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,7 +1,7 @@
1
1
  "use client";
2
2
  import axios from 'axios';
3
3
  import { UpfilesClient, ImageManager } from '@thetechfossil/upfiles';
4
- import React3, { createContext, forwardRef, useContext, useState, useCallback, useEffect, 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, {
@@ -62,6 +62,11 @@ var HttpClient = class {
62
62
  return Promise.reject(refreshError);
63
63
  }
64
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
+ }
65
70
  return Promise.reject(error);
66
71
  }
67
72
  );
@@ -641,7 +646,7 @@ var useAuth = (config) => {
641
646
  uploadAndUpdateAvatar
642
647
  };
643
648
  };
644
- var ThemeContext = createContext({ theme: "light", mounted: false });
649
+ var ThemeContext = React.createContext({ theme: "light", mounted: false });
645
650
  function AuthThemeProvider({ children }) {
646
651
  const [theme, setTheme] = useState("light");
647
652
  const [mounted, setMounted] = useState(false);
@@ -708,11 +713,17 @@ var AuthProvider = ({ children, config }) => {
708
713
  const authenticated = authService.isAuthenticated();
709
714
  if (authenticated) {
710
715
  try {
711
- const currentUser = authService.getCurrentUser();
712
- setUser(currentUser);
716
+ const freshUser = await authService.getProfile();
717
+ setUser(freshUser);
713
718
  } catch (error) {
714
- console.error("Failed to get current user:", error);
715
- 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
+ }
716
727
  }
717
728
  } else {
718
729
  setUser(null);
@@ -1028,7 +1039,7 @@ try {
1028
1039
  } catch (error) {
1029
1040
  console.warn("react-phone-number-input not available, using fallback");
1030
1041
  }
1031
- var CustomPhoneInput = React3.forwardRef((props, ref) => /* @__PURE__ */ jsx(
1042
+ var CustomPhoneInput = React.forwardRef((props, ref) => /* @__PURE__ */ jsx(
1032
1043
  "input",
1033
1044
  {
1034
1045
  ...props,
@@ -3507,7 +3518,19 @@ var UserButton = ({ showName = false, appearance }) => {
3507
3518
  e.currentTarget.style.backgroundColor = "transparent";
3508
3519
  },
3509
3520
  children: [
3510
- /* @__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: {
3511
3534
  width: "36px",
3512
3535
  height: "36px",
3513
3536
  borderRadius: "50%",
@@ -3544,7 +3567,19 @@ var UserButton = ({ showName = false, appearance }) => {
3544
3567
  alignItems: "center",
3545
3568
  gap: "12px"
3546
3569
  }, children: [
3547
- /* @__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: {
3548
3583
  width: "48px",
3549
3584
  height: "48px",
3550
3585
  borderRadius: "50%",
@@ -4440,26 +4475,16 @@ var AvatarUploader = ({
4440
4475
  const handleSelect = async (image) => {
4441
4476
  setUploading(true);
4442
4477
  try {
4443
- const proxyUrl = `${upfilesConfig.baseUrl}/api/download?fileKey=${encodeURIComponent(image.key)}`;
4444
- const response = await fetch(proxyUrl, {
4445
- headers: upfilesConfig.apiKey ? {
4446
- [upfilesConfig.apiKeyHeader || "authorization"]: upfilesConfig.apiKey.startsWith("upk_") ? `Bearer ${upfilesConfig.apiKey}` : upfilesConfig.apiKey
4447
- } : {}
4448
- });
4449
- if (!response.ok) {
4450
- throw new Error("Failed to fetch image");
4451
- }
4452
- const blob = await response.blob();
4453
- const file = new File([blob], image.originalName, { type: image.contentType });
4454
- const result = await uploadAndUpdateAvatar(file);
4455
- if (result.success && result.user?.avatar) {
4456
- 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);
4457
4482
  setOpen(false);
4458
4483
  } else {
4459
- throw new Error(result.message || "Failed to update avatar");
4484
+ throw new Error(response.message || "Failed to update avatar");
4460
4485
  }
4461
4486
  } catch (error) {
4462
- const err = error instanceof Error ? error : new Error("Upload failed");
4487
+ const err = error instanceof Error ? error : new Error("Failed to update avatar");
4463
4488
  onError?.(err);
4464
4489
  } finally {
4465
4490
  setUploading(false);
@@ -4806,8 +4831,14 @@ var AvatarManager = ({
4806
4831
  gridClassName,
4807
4832
  maxFileSize = 5 * 1024 * 1024,
4808
4833
  // 5MB default
4834
+ maxFiles = 10,
4809
4835
  mode = "full",
4810
4836
  showDelete = false,
4837
+ autoRecordToDb = true,
4838
+ fetchThumbnails = true,
4839
+ projectId,
4840
+ deleteUrl,
4841
+ onDelete,
4811
4842
  upfilesConfig
4812
4843
  }) => {
4813
4844
  const { updateProfile } = useAuth2();
@@ -4839,18 +4870,25 @@ var AvatarManager = ({
4839
4870
  apiKey: upfilesConfig.apiKey,
4840
4871
  apiKeyHeader: upfilesConfig.apiKeyHeader || "authorization",
4841
4872
  presignUrl: upfilesConfig.presignUrl,
4842
- presignPath: upfilesConfig.presignPath
4873
+ presignPath: upfilesConfig.presignPath,
4874
+ headers: upfilesConfig.headers,
4875
+ withCredentials: upfilesConfig.withCredentials
4843
4876
  },
4844
- folderPath: upfilesConfig.folderPath || "avatars/",
4877
+ projectId,
4878
+ folderPath: upfilesConfig.folderPath || "/",
4845
4879
  title,
4846
4880
  description,
4847
4881
  className,
4848
4882
  gridClassName,
4849
4883
  onSelect: handleSelect,
4884
+ onDelete,
4885
+ deleteUrl,
4886
+ autoRecordToDb,
4887
+ fetchThumbnails,
4850
4888
  maxFileSize,
4889
+ maxFiles,
4851
4890
  mode,
4852
- showDelete,
4853
- fetchThumbnails: true
4891
+ showDelete
4854
4892
  }
4855
4893
  );
4856
4894
  };