@thetechfossil/auth2 1.2.5 → 1.2.7

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
@@ -1,5 +1,7 @@
1
1
  "use client";
2
2
  import axios from 'axios';
3
+ import { UpfilesClient, Uploader, ImageManager } from '@thetechfossil/upfiles';
4
+ export { ConnectProjectDialog, ImageManager, ProjectFilesWidget, UpfilesClient, Uploader } from '@thetechfossil/upfiles';
3
5
  import React3, { createContext, forwardRef, useContext, useState, useCallback, useEffect, useRef, useMemo } from 'react';
4
6
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
5
7
  import PhoneInputWithCountry from 'react-phone-number-input';
@@ -116,11 +118,10 @@ var HttpClient = class {
116
118
  }
117
119
  }
118
120
  };
119
-
120
- // src/core/auth-service.ts
121
121
  var AuthService = class {
122
122
  constructor(config) {
123
123
  this.token = null;
124
+ this.upfilesClient = null;
124
125
  this.config = {
125
126
  localStorageKey: "auth_token",
126
127
  csrfEnabled: true,
@@ -128,6 +129,15 @@ var AuthService = class {
128
129
  };
129
130
  this.httpClient = new HttpClient(this.config.baseUrl);
130
131
  this.loadTokenFromStorage();
132
+ if (this.config.upfilesConfig) {
133
+ this.upfilesClient = new UpfilesClient({
134
+ baseUrl: this.config.upfilesConfig.baseUrl,
135
+ apiKey: this.config.upfilesConfig.apiKey,
136
+ apiKeyHeader: this.config.upfilesConfig.apiKeyHeader,
137
+ presignUrl: this.config.upfilesConfig.presignUrl,
138
+ presignPath: this.config.upfilesConfig.presignPath
139
+ });
140
+ }
131
141
  if (typeof window !== "undefined") {
132
142
  const frontendBaseUrl = process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || process.env.NEXT_PUBLIC_APP_URL || window.location.origin;
133
143
  if (frontendBaseUrl) {
@@ -355,6 +365,28 @@ var AuthService = class {
355
365
  }
356
366
  return response;
357
367
  }
368
+ async uploadAndUpdateAvatar(file) {
369
+ if (!this.token) {
370
+ throw new Error("Not authenticated");
371
+ }
372
+ if (!this.upfilesClient) {
373
+ throw new Error("Upfiles configuration is required. Please provide upfilesConfig in AuthConfig.");
374
+ }
375
+ try {
376
+ const folderPath = this.config.upfilesConfig?.folderPath || "avatars/";
377
+ const uploadResult = await this.upfilesClient.upload(file, {
378
+ folderPath,
379
+ fetchThumbnails: true
380
+ });
381
+ const response = await this.updateAvatar(uploadResult.publicUrl);
382
+ return response;
383
+ } catch (error) {
384
+ throw new Error(`Failed to upload avatar: ${error.message || "Unknown error"}`);
385
+ }
386
+ }
387
+ getUpfilesClient() {
388
+ return this.upfilesClient;
389
+ }
358
390
  async requestEmailChange(newEmail) {
359
391
  if (!this.token) {
360
392
  throw new Error("Not authenticated");
@@ -526,7 +558,8 @@ var AuthProvider = ({ children, config }) => {
526
558
  const authConfig = {
527
559
  baseUrl: config?.baseUrl || (typeof window !== "undefined" ? process.env.NEXT_PUBLIC_AUTH_API_URL || process.env.REACT_APP_AUTH_API_URL || "http://localhost:7000" : "http://localhost:7000"),
528
560
  localStorageKey: config?.localStorageKey || "auth_token",
529
- csrfEnabled: config?.csrfEnabled !== void 0 ? config.csrfEnabled : true
561
+ csrfEnabled: config?.csrfEnabled !== void 0 ? config.csrfEnabled : true,
562
+ upfilesConfig: config?.upfilesConfig
530
563
  };
531
564
  const [authService] = useState(() => new AuthService(authConfig));
532
565
  const [user, setUser] = useState(null);
@@ -676,6 +709,18 @@ var AuthProvider = ({ children, config }) => {
676
709
  setLoading(false);
677
710
  }
678
711
  }, [authService]);
712
+ const uploadAndUpdateAvatar = useCallback(async (file) => {
713
+ setLoading(true);
714
+ try {
715
+ const response = await authService.uploadAndUpdateAvatar(file);
716
+ if (response.success && response.user) {
717
+ setUser(response.user);
718
+ }
719
+ return response;
720
+ } finally {
721
+ setLoading(false);
722
+ }
723
+ }, [authService]);
679
724
  const requestEmailChange = useCallback(async (newEmail) => {
680
725
  setLoading(true);
681
726
  try {
@@ -773,6 +818,7 @@ var AuthProvider = ({ children, config }) => {
773
818
  resetPassword,
774
819
  changePassword,
775
820
  updateAvatar,
821
+ uploadAndUpdateAvatar,
776
822
  requestEmailChange,
777
823
  verifyEmailChange,
778
824
  generate2FA,
@@ -908,6 +954,18 @@ var useAuth2 = (config) => {
908
954
  setLoading(false);
909
955
  }
910
956
  }, [authService]);
957
+ const uploadAndUpdateAvatar = useCallback(async (file) => {
958
+ setLoading(true);
959
+ try {
960
+ const response = await authService.uploadAndUpdateAvatar(file);
961
+ if (response.success && response.user) {
962
+ setUser(response.user);
963
+ }
964
+ return response;
965
+ } finally {
966
+ setLoading(false);
967
+ }
968
+ }, [authService]);
911
969
  return {
912
970
  user,
913
971
  isAuthenticated,
@@ -920,7 +978,8 @@ var useAuth2 = (config) => {
920
978
  updateProfile,
921
979
  getProfile,
922
980
  getAllUsers,
923
- getUserById
981
+ getUserById,
982
+ uploadAndUpdateAvatar
924
983
  };
925
984
  };
926
985
 
@@ -4663,6 +4722,122 @@ var UserProfile = ({
4663
4722
  ] })
4664
4723
  ] });
4665
4724
  };
4725
+ var AvatarUploader = ({
4726
+ onUploadComplete,
4727
+ onError,
4728
+ className,
4729
+ buttonClassName,
4730
+ dropzoneClassName,
4731
+ maxFileSize = 5 * 1024 * 1024,
4732
+ // 5MB default
4733
+ accept = ["image/*"],
4734
+ upfilesConfig
4735
+ }) => {
4736
+ const { uploadAndUpdateAvatar } = useAuth();
4737
+ const [uploading, setUploading] = useState(false);
4738
+ const handleUploadComplete = async (files) => {
4739
+ if (files.length === 0)
4740
+ return;
4741
+ setUploading(true);
4742
+ try {
4743
+ const file = files[0];
4744
+ const response = await uploadAndUpdateAvatar(file.file);
4745
+ if (response.success && response.user?.avatar) {
4746
+ onUploadComplete?.(response.user.avatar);
4747
+ } else {
4748
+ throw new Error(response.message || "Failed to update avatar");
4749
+ }
4750
+ } catch (error) {
4751
+ const err = error instanceof Error ? error : new Error("Upload failed");
4752
+ onError?.(err);
4753
+ } finally {
4754
+ setUploading(false);
4755
+ }
4756
+ };
4757
+ const handleError = (error) => {
4758
+ onError?.(error);
4759
+ };
4760
+ return /* @__PURE__ */ jsx("div", { className, children: /* @__PURE__ */ jsx(
4761
+ Uploader,
4762
+ {
4763
+ clientOptions: {
4764
+ baseUrl: upfilesConfig.baseUrl,
4765
+ apiKey: upfilesConfig.apiKey,
4766
+ apiKeyHeader: upfilesConfig.apiKeyHeader || "authorization",
4767
+ presignUrl: upfilesConfig.presignUrl,
4768
+ presignPath: upfilesConfig.presignPath
4769
+ },
4770
+ multiple: false,
4771
+ accept,
4772
+ maxFileSize,
4773
+ maxFiles: 1,
4774
+ onComplete: handleUploadComplete,
4775
+ onError: handleError,
4776
+ buttonClassName,
4777
+ dropzoneClassName,
4778
+ children: uploading ? "Uploading..." : "Upload Avatar"
4779
+ }
4780
+ ) });
4781
+ };
4782
+ var AvatarManager = ({
4783
+ open,
4784
+ onOpenChange,
4785
+ onAvatarUpdated,
4786
+ onError,
4787
+ title = "Select Avatar",
4788
+ description = "Choose an existing image or upload a new one",
4789
+ className,
4790
+ gridClassName,
4791
+ maxFileSize = 5 * 1024 * 1024,
4792
+ // 5MB default
4793
+ mode = "full",
4794
+ showDelete = false,
4795
+ upfilesConfig
4796
+ }) => {
4797
+ const { updateProfile } = useAuth();
4798
+ const [updating, setUpdating] = useState(false);
4799
+ const handleSelect = async (image) => {
4800
+ setUpdating(true);
4801
+ try {
4802
+ const response = await updateProfile({ avatar: image.url });
4803
+ if (response.success && response.user?.avatar) {
4804
+ onAvatarUpdated?.(response.user.avatar);
4805
+ onOpenChange(false);
4806
+ } else {
4807
+ throw new Error(response.message || "Failed to update avatar");
4808
+ }
4809
+ } catch (error) {
4810
+ const err = error instanceof Error ? error : new Error("Failed to update avatar");
4811
+ onError?.(err);
4812
+ } finally {
4813
+ setUpdating(false);
4814
+ }
4815
+ };
4816
+ return /* @__PURE__ */ jsx(
4817
+ ImageManager,
4818
+ {
4819
+ open,
4820
+ onOpenChange,
4821
+ clientOptions: {
4822
+ baseUrl: upfilesConfig.baseUrl,
4823
+ apiKey: upfilesConfig.apiKey,
4824
+ apiKeyHeader: upfilesConfig.apiKeyHeader || "authorization",
4825
+ presignUrl: upfilesConfig.presignUrl,
4826
+ presignPath: upfilesConfig.presignPath
4827
+ },
4828
+ folderPath: upfilesConfig.folderPath || "avatars/",
4829
+ title,
4830
+ description,
4831
+ className,
4832
+ gridClassName,
4833
+ onSelect: handleSelect,
4834
+ maxFileSize,
4835
+ mode,
4836
+ showDelete,
4837
+ fetchThumbnails: true
4838
+ }
4839
+ );
4840
+ };
4666
4841
 
4667
4842
  // src/react/index.ts
4668
4843
  var react_exports = {};
@@ -4670,6 +4845,8 @@ __export(react_exports, {
4670
4845
  AuthFlow: () => AuthFlow,
4671
4846
  AuthProvider: () => AuthProvider,
4672
4847
  AuthThemeProvider: () => AuthThemeProvider,
4848
+ AvatarManager: () => AvatarManager,
4849
+ AvatarUploader: () => AvatarUploader,
4673
4850
  ChangePassword: () => ChangePassword,
4674
4851
  EmailVerificationPage: () => EmailVerificationPage,
4675
4852
  ForgotPassword: () => ForgotPassword,
@@ -4775,6 +4952,6 @@ var AuthClient = class extends AuthService {
4775
4952
  }
4776
4953
  };
4777
4954
 
4778
- export { AuthFlow, AuthProvider, AuthService, 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 };
4955
+ 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 };
4779
4956
  //# sourceMappingURL=out.js.map
4780
4957
  //# sourceMappingURL=index.mjs.map