@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.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { UpfilesClient } from '@thetechfossil/upfiles';
2
+ export { ConnectProjectDialog, ImageManager, ProjectFilesWidget, UpfilesClient, Uploader } from '@thetechfossil/upfiles';
1
3
  import React, { ReactNode } from 'react';
2
4
  import * as react_jsx_runtime from 'react/jsx-runtime';
3
5
 
@@ -64,6 +66,15 @@ interface AuthConfig {
64
66
  localStorageKey?: string;
65
67
  token?: string;
66
68
  csrfEnabled?: boolean;
69
+ upfilesConfig?: UpfilesConfig;
70
+ }
71
+ interface UpfilesConfig {
72
+ baseUrl: string;
73
+ apiKey?: string;
74
+ apiKeyHeader?: 'authorization' | 'x-api-key' | 'x-up-api-key';
75
+ presignUrl?: string;
76
+ presignPath?: string;
77
+ folderPath?: string;
67
78
  }
68
79
  interface Session {
69
80
  id: string;
@@ -109,6 +120,7 @@ interface UseAuthReturn {
109
120
  refreshCsrfToken: () => Promise<void>;
110
121
  changePassword: (oldPassword: string, newPassword: string) => Promise<AuthResponse>;
111
122
  updateAvatar: (avatar: string) => Promise<AuthResponse>;
123
+ uploadAndUpdateAvatar: (file: File) => Promise<AuthResponse>;
112
124
  requestEmailChange: (newEmail: string) => Promise<AuthResponse>;
113
125
  verifyEmailChange: (token: string) => Promise<AuthResponse>;
114
126
  generate2FA: () => Promise<MFASetup>;
@@ -127,6 +139,7 @@ declare class AuthService {
127
139
  private httpClient;
128
140
  private config;
129
141
  private token;
142
+ private upfilesClient;
130
143
  constructor(config: AuthConfig);
131
144
  private loadTokenFromStorage;
132
145
  private saveTokenToStorage;
@@ -152,6 +165,8 @@ declare class AuthService {
152
165
  resetPassword(token: string, password: string): Promise<AuthResponse>;
153
166
  changePassword(oldPassword: string, newPassword: string): Promise<AuthResponse>;
154
167
  updateAvatar(avatar: string): Promise<AuthResponse>;
168
+ uploadAndUpdateAvatar(file: File): Promise<AuthResponse>;
169
+ getUpfilesClient(): UpfilesClient | null;
155
170
  requestEmailChange(newEmail: string): Promise<AuthResponse>;
156
171
  verifyEmailChange(token: string): Promise<AuthResponse>;
157
172
  generate2FA(): Promise<{
@@ -219,6 +234,7 @@ interface AuthContextValue {
219
234
  resetPassword: (token: string, password: string) => Promise<AuthResponse>;
220
235
  changePassword: (oldPassword: string, newPassword: string) => Promise<AuthResponse>;
221
236
  updateAvatar: (avatar: string) => Promise<AuthResponse>;
237
+ uploadAndUpdateAvatar: (file: File) => Promise<AuthResponse>;
222
238
  requestEmailChange: (newEmail: string) => Promise<AuthResponse>;
223
239
  verifyEmailChange: (token: string) => Promise<AuthResponse>;
224
240
  generate2FA: () => Promise<any>;
@@ -412,6 +428,48 @@ interface PhoneInputProps {
412
428
  }
413
429
  declare const PhoneInput: React.FC<PhoneInputProps>;
414
430
 
431
+ interface AvatarUploaderProps {
432
+ onUploadComplete?: (avatarUrl: string) => void;
433
+ onError?: (error: Error) => void;
434
+ className?: string;
435
+ buttonClassName?: string;
436
+ dropzoneClassName?: string;
437
+ maxFileSize?: number;
438
+ accept?: string[];
439
+ upfilesConfig: {
440
+ baseUrl: string;
441
+ apiKey?: string;
442
+ apiKeyHeader?: 'authorization' | 'x-api-key' | 'x-up-api-key';
443
+ presignUrl?: string;
444
+ presignPath?: string;
445
+ folderPath?: string;
446
+ };
447
+ }
448
+ declare const AvatarUploader: React.FC<AvatarUploaderProps>;
449
+
450
+ interface AvatarManagerProps {
451
+ open: boolean;
452
+ onOpenChange: (open: boolean) => void;
453
+ onAvatarUpdated?: (avatarUrl: string) => void;
454
+ onError?: (error: Error) => void;
455
+ title?: string;
456
+ description?: string;
457
+ className?: string;
458
+ gridClassName?: string;
459
+ maxFileSize?: number;
460
+ mode?: 'full' | 'browse' | 'upload';
461
+ showDelete?: boolean;
462
+ upfilesConfig: {
463
+ baseUrl: string;
464
+ apiKey?: string;
465
+ apiKeyHeader?: 'authorization' | 'x-api-key' | 'x-up-api-key';
466
+ presignUrl?: string;
467
+ presignPath?: string;
468
+ folderPath?: string;
469
+ };
470
+ }
471
+ declare const AvatarManager: React.FC<AvatarManagerProps>;
472
+
415
473
  interface UseAuthReturnBase {
416
474
  user: User | null;
417
475
  isAuthenticated: boolean;
@@ -425,6 +483,7 @@ interface UseAuthReturnBase {
425
483
  getProfile: () => Promise<User>;
426
484
  getAllUsers: () => Promise<User[]>;
427
485
  getUserById: (id: string) => Promise<User>;
486
+ uploadAndUpdateAvatar: (file: File) => Promise<AuthResponse>;
428
487
  }
429
488
  declare const useAuth: (config: AuthConfig) => UseAuthReturnBase;
430
489
 
@@ -441,6 +500,10 @@ declare function useAuthTheme(): ThemeContextType;
441
500
  declare const index$1_AuthFlow: typeof AuthFlow;
442
501
  declare const index$1_AuthProvider: typeof AuthProvider;
443
502
  declare const index$1_AuthThemeProvider: typeof AuthThemeProvider;
503
+ declare const index$1_AvatarManager: typeof AvatarManager;
504
+ type index$1_AvatarManagerProps = AvatarManagerProps;
505
+ declare const index$1_AvatarUploader: typeof AvatarUploader;
506
+ type index$1_AvatarUploaderProps = AvatarUploaderProps;
444
507
  declare const index$1_ChangePassword: typeof ChangePassword;
445
508
  declare const index$1_EmailVerificationPage: typeof EmailVerificationPage;
446
509
  declare const index$1_ForgotPassword: typeof ForgotPassword;
@@ -464,6 +527,10 @@ declare namespace index$1 {
464
527
  index$1_AuthFlow as AuthFlow,
465
528
  index$1_AuthProvider as AuthProvider,
466
529
  index$1_AuthThemeProvider as AuthThemeProvider,
530
+ index$1_AvatarManager as AvatarManager,
531
+ index$1_AvatarManagerProps as AvatarManagerProps,
532
+ index$1_AvatarUploader as AvatarUploader,
533
+ index$1_AvatarUploaderProps as AvatarUploaderProps,
467
534
  index$1_ChangePassword as ChangePassword,
468
535
  index$1_EmailVerificationPage as EmailVerificationPage,
469
536
  index$1_ForgotPassword as ForgotPassword,
@@ -507,4 +574,4 @@ declare namespace index {
507
574
  };
508
575
  }
509
576
 
510
- export { AuditLog, AuthConfig, AuthFlow, AuthProvider, AuthResponse, AuthService, ChangePassword, CsrfTokenResponse, EmailVerificationPage, ForgotPassword, HttpClient, LinkedAccount, LoginData, LoginForm, MFASetup, OAuthConfig, OAuthProvider, OtpForm, ProtectedRoute, PublicRoute, RegisterData, RegisterForm, ResetPassword, Session, SignIn, SignOut, SignUp, UpdateUserData, UseAuthReturn, User, UserButton, UserProfile, VerifyData, VerifyEmail, index as node, index$1 as react, useAuth$1 as useAuth };
577
+ export { AuditLog, AuthConfig, AuthFlow, AuthProvider, AuthResponse, AuthService, AvatarManager, AvatarUploader, ChangePassword, CsrfTokenResponse, EmailVerificationPage, ForgotPassword, HttpClient, LinkedAccount, LoginData, LoginForm, MFASetup, OAuthConfig, OAuthProvider, OtpForm, ProtectedRoute, PublicRoute, RegisterData, RegisterForm, ResetPassword, Session, SignIn, SignOut, SignUp, UpdateUserData, UpfilesConfig, UseAuthReturn, User, UserButton, UserProfile, VerifyData, VerifyEmail, index as node, index$1 as react, useAuth$1 as useAuth };
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
  'use strict';
3
3
 
4
4
  var axios = require('axios');
5
+ var upfiles = require('@thetechfossil/upfiles');
5
6
  var React3 = require('react');
6
7
  var jsxRuntime = require('react/jsx-runtime');
7
8
  var PhoneInputWithCountry = require('react-phone-number-input');
@@ -124,11 +125,10 @@ var HttpClient = class {
124
125
  }
125
126
  }
126
127
  };
127
-
128
- // src/core/auth-service.ts
129
128
  var AuthService = class {
130
129
  constructor(config) {
131
130
  this.token = null;
131
+ this.upfilesClient = null;
132
132
  this.config = {
133
133
  localStorageKey: "auth_token",
134
134
  csrfEnabled: true,
@@ -136,6 +136,15 @@ var AuthService = class {
136
136
  };
137
137
  this.httpClient = new HttpClient(this.config.baseUrl);
138
138
  this.loadTokenFromStorage();
139
+ if (this.config.upfilesConfig) {
140
+ this.upfilesClient = new upfiles.UpfilesClient({
141
+ baseUrl: this.config.upfilesConfig.baseUrl,
142
+ apiKey: this.config.upfilesConfig.apiKey,
143
+ apiKeyHeader: this.config.upfilesConfig.apiKeyHeader,
144
+ presignUrl: this.config.upfilesConfig.presignUrl,
145
+ presignPath: this.config.upfilesConfig.presignPath
146
+ });
147
+ }
139
148
  if (typeof window !== "undefined") {
140
149
  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;
141
150
  if (frontendBaseUrl) {
@@ -363,6 +372,28 @@ var AuthService = class {
363
372
  }
364
373
  return response;
365
374
  }
375
+ async uploadAndUpdateAvatar(file) {
376
+ if (!this.token) {
377
+ throw new Error("Not authenticated");
378
+ }
379
+ if (!this.upfilesClient) {
380
+ throw new Error("Upfiles configuration is required. Please provide upfilesConfig in AuthConfig.");
381
+ }
382
+ try {
383
+ const folderPath = this.config.upfilesConfig?.folderPath || "avatars/";
384
+ const uploadResult = await this.upfilesClient.upload(file, {
385
+ folderPath,
386
+ fetchThumbnails: true
387
+ });
388
+ const response = await this.updateAvatar(uploadResult.publicUrl);
389
+ return response;
390
+ } catch (error) {
391
+ throw new Error(`Failed to upload avatar: ${error.message || "Unknown error"}`);
392
+ }
393
+ }
394
+ getUpfilesClient() {
395
+ return this.upfilesClient;
396
+ }
366
397
  async requestEmailChange(newEmail) {
367
398
  if (!this.token) {
368
399
  throw new Error("Not authenticated");
@@ -534,7 +565,8 @@ var AuthProvider = ({ children, config }) => {
534
565
  const authConfig = {
535
566
  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"),
536
567
  localStorageKey: config?.localStorageKey || "auth_token",
537
- csrfEnabled: config?.csrfEnabled !== void 0 ? config.csrfEnabled : true
568
+ csrfEnabled: config?.csrfEnabled !== void 0 ? config.csrfEnabled : true,
569
+ upfilesConfig: config?.upfilesConfig
538
570
  };
539
571
  const [authService] = React3.useState(() => new AuthService(authConfig));
540
572
  const [user, setUser] = React3.useState(null);
@@ -684,6 +716,18 @@ var AuthProvider = ({ children, config }) => {
684
716
  setLoading(false);
685
717
  }
686
718
  }, [authService]);
719
+ const uploadAndUpdateAvatar = React3.useCallback(async (file) => {
720
+ setLoading(true);
721
+ try {
722
+ const response = await authService.uploadAndUpdateAvatar(file);
723
+ if (response.success && response.user) {
724
+ setUser(response.user);
725
+ }
726
+ return response;
727
+ } finally {
728
+ setLoading(false);
729
+ }
730
+ }, [authService]);
687
731
  const requestEmailChange = React3.useCallback(async (newEmail) => {
688
732
  setLoading(true);
689
733
  try {
@@ -781,6 +825,7 @@ var AuthProvider = ({ children, config }) => {
781
825
  resetPassword,
782
826
  changePassword,
783
827
  updateAvatar,
828
+ uploadAndUpdateAvatar,
784
829
  requestEmailChange,
785
830
  verifyEmailChange,
786
831
  generate2FA,
@@ -916,6 +961,18 @@ var useAuth2 = (config) => {
916
961
  setLoading(false);
917
962
  }
918
963
  }, [authService]);
964
+ const uploadAndUpdateAvatar = React3.useCallback(async (file) => {
965
+ setLoading(true);
966
+ try {
967
+ const response = await authService.uploadAndUpdateAvatar(file);
968
+ if (response.success && response.user) {
969
+ setUser(response.user);
970
+ }
971
+ return response;
972
+ } finally {
973
+ setLoading(false);
974
+ }
975
+ }, [authService]);
919
976
  return {
920
977
  user,
921
978
  isAuthenticated,
@@ -928,7 +985,8 @@ var useAuth2 = (config) => {
928
985
  updateProfile,
929
986
  getProfile,
930
987
  getAllUsers,
931
- getUserById
988
+ getUserById,
989
+ uploadAndUpdateAvatar
932
990
  };
933
991
  };
934
992
 
@@ -4671,6 +4729,122 @@ var UserProfile = ({
4671
4729
  ] })
4672
4730
  ] });
4673
4731
  };
4732
+ var AvatarUploader = ({
4733
+ onUploadComplete,
4734
+ onError,
4735
+ className,
4736
+ buttonClassName,
4737
+ dropzoneClassName,
4738
+ maxFileSize = 5 * 1024 * 1024,
4739
+ // 5MB default
4740
+ accept = ["image/*"],
4741
+ upfilesConfig
4742
+ }) => {
4743
+ const { uploadAndUpdateAvatar } = useAuth();
4744
+ const [uploading, setUploading] = React3.useState(false);
4745
+ const handleUploadComplete = async (files) => {
4746
+ if (files.length === 0)
4747
+ return;
4748
+ setUploading(true);
4749
+ try {
4750
+ const file = files[0];
4751
+ const response = await uploadAndUpdateAvatar(file.file);
4752
+ if (response.success && response.user?.avatar) {
4753
+ onUploadComplete?.(response.user.avatar);
4754
+ } else {
4755
+ throw new Error(response.message || "Failed to update avatar");
4756
+ }
4757
+ } catch (error) {
4758
+ const err = error instanceof Error ? error : new Error("Upload failed");
4759
+ onError?.(err);
4760
+ } finally {
4761
+ setUploading(false);
4762
+ }
4763
+ };
4764
+ const handleError = (error) => {
4765
+ onError?.(error);
4766
+ };
4767
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className, children: /* @__PURE__ */ jsxRuntime.jsx(
4768
+ upfiles.Uploader,
4769
+ {
4770
+ clientOptions: {
4771
+ baseUrl: upfilesConfig.baseUrl,
4772
+ apiKey: upfilesConfig.apiKey,
4773
+ apiKeyHeader: upfilesConfig.apiKeyHeader || "authorization",
4774
+ presignUrl: upfilesConfig.presignUrl,
4775
+ presignPath: upfilesConfig.presignPath
4776
+ },
4777
+ multiple: false,
4778
+ accept,
4779
+ maxFileSize,
4780
+ maxFiles: 1,
4781
+ onComplete: handleUploadComplete,
4782
+ onError: handleError,
4783
+ buttonClassName,
4784
+ dropzoneClassName,
4785
+ children: uploading ? "Uploading..." : "Upload Avatar"
4786
+ }
4787
+ ) });
4788
+ };
4789
+ var AvatarManager = ({
4790
+ open,
4791
+ onOpenChange,
4792
+ onAvatarUpdated,
4793
+ onError,
4794
+ title = "Select Avatar",
4795
+ description = "Choose an existing image or upload a new one",
4796
+ className,
4797
+ gridClassName,
4798
+ maxFileSize = 5 * 1024 * 1024,
4799
+ // 5MB default
4800
+ mode = "full",
4801
+ showDelete = false,
4802
+ upfilesConfig
4803
+ }) => {
4804
+ const { updateProfile } = useAuth();
4805
+ const [updating, setUpdating] = React3.useState(false);
4806
+ const handleSelect = async (image) => {
4807
+ setUpdating(true);
4808
+ try {
4809
+ const response = await updateProfile({ avatar: image.url });
4810
+ if (response.success && response.user?.avatar) {
4811
+ onAvatarUpdated?.(response.user.avatar);
4812
+ onOpenChange(false);
4813
+ } else {
4814
+ throw new Error(response.message || "Failed to update avatar");
4815
+ }
4816
+ } catch (error) {
4817
+ const err = error instanceof Error ? error : new Error("Failed to update avatar");
4818
+ onError?.(err);
4819
+ } finally {
4820
+ setUpdating(false);
4821
+ }
4822
+ };
4823
+ return /* @__PURE__ */ jsxRuntime.jsx(
4824
+ upfiles.ImageManager,
4825
+ {
4826
+ open,
4827
+ onOpenChange,
4828
+ clientOptions: {
4829
+ baseUrl: upfilesConfig.baseUrl,
4830
+ apiKey: upfilesConfig.apiKey,
4831
+ apiKeyHeader: upfilesConfig.apiKeyHeader || "authorization",
4832
+ presignUrl: upfilesConfig.presignUrl,
4833
+ presignPath: upfilesConfig.presignPath
4834
+ },
4835
+ folderPath: upfilesConfig.folderPath || "avatars/",
4836
+ title,
4837
+ description,
4838
+ className,
4839
+ gridClassName,
4840
+ onSelect: handleSelect,
4841
+ maxFileSize,
4842
+ mode,
4843
+ showDelete,
4844
+ fetchThumbnails: true
4845
+ }
4846
+ );
4847
+ };
4674
4848
 
4675
4849
  // src/react/index.ts
4676
4850
  var react_exports = {};
@@ -4678,6 +4852,8 @@ __export(react_exports, {
4678
4852
  AuthFlow: () => AuthFlow,
4679
4853
  AuthProvider: () => AuthProvider,
4680
4854
  AuthThemeProvider: () => AuthThemeProvider,
4855
+ AvatarManager: () => AvatarManager,
4856
+ AvatarUploader: () => AvatarUploader,
4681
4857
  ChangePassword: () => ChangePassword,
4682
4858
  EmailVerificationPage: () => EmailVerificationPage,
4683
4859
  ForgotPassword: () => ForgotPassword,
@@ -4783,9 +4959,31 @@ var AuthClient = class extends AuthService {
4783
4959
  }
4784
4960
  };
4785
4961
 
4962
+ Object.defineProperty(exports, 'ConnectProjectDialog', {
4963
+ enumerable: true,
4964
+ get: function () { return upfiles.ConnectProjectDialog; }
4965
+ });
4966
+ Object.defineProperty(exports, 'ImageManager', {
4967
+ enumerable: true,
4968
+ get: function () { return upfiles.ImageManager; }
4969
+ });
4970
+ Object.defineProperty(exports, 'ProjectFilesWidget', {
4971
+ enumerable: true,
4972
+ get: function () { return upfiles.ProjectFilesWidget; }
4973
+ });
4974
+ Object.defineProperty(exports, 'UpfilesClient', {
4975
+ enumerable: true,
4976
+ get: function () { return upfiles.UpfilesClient; }
4977
+ });
4978
+ Object.defineProperty(exports, 'Uploader', {
4979
+ enumerable: true,
4980
+ get: function () { return upfiles.Uploader; }
4981
+ });
4786
4982
  exports.AuthFlow = AuthFlow;
4787
4983
  exports.AuthProvider = AuthProvider;
4788
4984
  exports.AuthService = AuthService;
4985
+ exports.AvatarManager = AvatarManager;
4986
+ exports.AvatarUploader = AvatarUploader;
4789
4987
  exports.ChangePassword = ChangePassword;
4790
4988
  exports.EmailVerificationPage = EmailVerificationPage;
4791
4989
  exports.ForgotPassword = ForgotPassword;