@umituz/web-firebase 3.2.3 → 3.3.0
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/package.json +5 -2
- package/src/application/use-cases/auth/sign-in-with-google.use-case.ts +13 -11
- package/src/domain/interfaces/file.repository.interface.ts +28 -3
- package/src/domain/interfaces/user.repository.interface.ts +12 -40
- package/src/domains/firestore/services/firestore.service.ts +5 -1
- package/src/domains/storage/services/storage.service.ts +5 -1
- package/src/index.ts +2 -11
- package/src/infrastructure/firebase/auth.adapter.ts +7 -3
- package/src/infrastructure/firebase/client.ts +98 -44
- package/src/infrastructure/firebase/firestore.adapter.ts +5 -1
- package/src/infrastructure/firebase/storage.adapter.ts +5 -1
- package/src/presentation/hooks/useAuth.ts +0 -392
- package/src/presentation/hooks/useFirebaseAuth.ts +0 -122
- package/src/presentation/hooks/useFirestore.ts +0 -125
- package/src/presentation/hooks/useStorage.ts +0 -141
- package/src/presentation/index.ts +0 -10
- package/src/presentation/providers/FirebaseProvider.tsx +0 -90
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Storage Hook
|
|
3
|
-
* @description React hook for file storage operations
|
|
4
|
-
* Migrated from: /Users/umituz/Desktop/github/umituz/apps/web/app-growth-factory/src/domains/firebase/hooks/useStorage.ts
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { useState, useCallback } from 'react'
|
|
8
|
-
import type { IFileRepository } from '../../domain/interfaces/file.repository.interface'
|
|
9
|
-
import type { UploadProgress, UploadResult } from '../../domain/entities/file.entity'
|
|
10
|
-
|
|
11
|
-
export interface UseStorageOptions {
|
|
12
|
-
fileRepository: IFileRepository
|
|
13
|
-
userId: string
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function useStorage({ fileRepository, userId }: UseStorageOptions) {
|
|
17
|
-
const [uploading, setUploading] = useState(false)
|
|
18
|
-
const [progress, setProgress] = useState(0)
|
|
19
|
-
const [error, setError] = useState<Error | null>(null)
|
|
20
|
-
|
|
21
|
-
const uploadFile = useCallback(
|
|
22
|
-
async (path: string, file: File): Promise<UploadResult> => {
|
|
23
|
-
setUploading(true)
|
|
24
|
-
setProgress(0)
|
|
25
|
-
setError(null)
|
|
26
|
-
|
|
27
|
-
try {
|
|
28
|
-
const result = await fileRepository.uploadFile(userId, path, file, {
|
|
29
|
-
onProgress: (progress: UploadProgress) => {
|
|
30
|
-
setProgress(progress.progress)
|
|
31
|
-
},
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
setProgress(100)
|
|
35
|
-
return result
|
|
36
|
-
} catch (err) {
|
|
37
|
-
setError(err as Error)
|
|
38
|
-
throw err
|
|
39
|
-
} finally {
|
|
40
|
-
setUploading(false)
|
|
41
|
-
}
|
|
42
|
-
},
|
|
43
|
-
[fileRepository, userId]
|
|
44
|
-
)
|
|
45
|
-
|
|
46
|
-
const uploadImage = useCallback(
|
|
47
|
-
async (file: File): Promise<UploadResult> => {
|
|
48
|
-
return uploadFile('images', file)
|
|
49
|
-
},
|
|
50
|
-
[uploadFile]
|
|
51
|
-
)
|
|
52
|
-
|
|
53
|
-
const uploadVideo = useCallback(
|
|
54
|
-
async (file: File): Promise<UploadResult> => {
|
|
55
|
-
return uploadFile('videos', file)
|
|
56
|
-
},
|
|
57
|
-
[uploadFile]
|
|
58
|
-
)
|
|
59
|
-
|
|
60
|
-
const uploadDocument = useCallback(
|
|
61
|
-
async (file: File): Promise<UploadResult> => {
|
|
62
|
-
return uploadFile('documents', file)
|
|
63
|
-
},
|
|
64
|
-
[uploadFile]
|
|
65
|
-
)
|
|
66
|
-
|
|
67
|
-
const uploadProfilePicture = useCallback(
|
|
68
|
-
async (file: File): Promise<UploadResult> => {
|
|
69
|
-
setUploading(true)
|
|
70
|
-
setError(null)
|
|
71
|
-
|
|
72
|
-
try {
|
|
73
|
-
const result = await fileRepository.uploadProfilePicture(userId, file)
|
|
74
|
-
return result
|
|
75
|
-
} catch (err) {
|
|
76
|
-
setError(err as Error)
|
|
77
|
-
throw err
|
|
78
|
-
} finally {
|
|
79
|
-
setUploading(false)
|
|
80
|
-
}
|
|
81
|
-
},
|
|
82
|
-
[fileRepository, userId]
|
|
83
|
-
)
|
|
84
|
-
|
|
85
|
-
const deleteFile = useCallback(
|
|
86
|
-
async (path: string): Promise<void> => {
|
|
87
|
-
setUploading(true)
|
|
88
|
-
setError(null)
|
|
89
|
-
|
|
90
|
-
try {
|
|
91
|
-
await fileRepository.deleteFile(path)
|
|
92
|
-
} catch (err) {
|
|
93
|
-
setError(err as Error)
|
|
94
|
-
throw err
|
|
95
|
-
} finally {
|
|
96
|
-
setUploading(false)
|
|
97
|
-
}
|
|
98
|
-
},
|
|
99
|
-
[fileRepository]
|
|
100
|
-
)
|
|
101
|
-
|
|
102
|
-
const listUserImages = useCallback(
|
|
103
|
-
async (): Promise<string[]> => {
|
|
104
|
-
setError(null)
|
|
105
|
-
try {
|
|
106
|
-
return await fileRepository.listUserImages(userId)
|
|
107
|
-
} catch (err) {
|
|
108
|
-
setError(err as Error)
|
|
109
|
-
throw err
|
|
110
|
-
}
|
|
111
|
-
},
|
|
112
|
-
[fileRepository, userId]
|
|
113
|
-
)
|
|
114
|
-
|
|
115
|
-
const listUserVideos = useCallback(
|
|
116
|
-
async (): Promise<string[]> => {
|
|
117
|
-
setError(null)
|
|
118
|
-
try {
|
|
119
|
-
return await fileRepository.listUserVideos(userId)
|
|
120
|
-
} catch (err) {
|
|
121
|
-
setError(err as Error)
|
|
122
|
-
throw err
|
|
123
|
-
}
|
|
124
|
-
},
|
|
125
|
-
[fileRepository, userId]
|
|
126
|
-
)
|
|
127
|
-
|
|
128
|
-
return {
|
|
129
|
-
uploadFile,
|
|
130
|
-
uploadImage,
|
|
131
|
-
uploadVideo,
|
|
132
|
-
uploadDocument,
|
|
133
|
-
uploadProfilePicture,
|
|
134
|
-
deleteFile,
|
|
135
|
-
listUserImages,
|
|
136
|
-
listUserVideos,
|
|
137
|
-
uploading,
|
|
138
|
-
progress,
|
|
139
|
-
error,
|
|
140
|
-
}
|
|
141
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Presentation Layer
|
|
3
|
-
* Subpath: @umituz/web-firebase/presentation
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
export * from './hooks/useAuth';
|
|
7
|
-
export * from './hooks/useFirebaseAuth';
|
|
8
|
-
export * from './hooks/useFirestore';
|
|
9
|
-
export * from './hooks/useStorage';
|
|
10
|
-
export * from './providers/FirebaseProvider';
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* FirebaseProvider
|
|
3
|
-
* @description React Context Provider for Firebase with auth state tracking
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { createContext, useContext, useState, useEffect, type ReactNode } from 'react'
|
|
7
|
-
import type { User } from 'firebase/auth'
|
|
8
|
-
import type { FirebaseInstances } from '../../infrastructure/firebase/client'
|
|
9
|
-
import { getFirebaseInstances } from '../../infrastructure/firebase/client'
|
|
10
|
-
import type { AuthUser } from '../../domains/auth/entities'
|
|
11
|
-
import { authService } from '../../domains/auth/services'
|
|
12
|
-
|
|
13
|
-
export interface FirebaseContextValue {
|
|
14
|
-
instances: FirebaseInstances
|
|
15
|
-
isInitialized: boolean
|
|
16
|
-
user: User | null
|
|
17
|
-
authUser: AuthUser | null
|
|
18
|
-
loading: boolean
|
|
19
|
-
error: Error | null
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const FirebaseContext = createContext<FirebaseContextValue | null>(null)
|
|
23
|
-
|
|
24
|
-
export interface FirebaseProviderProps {
|
|
25
|
-
children: ReactNode
|
|
26
|
-
config?: {
|
|
27
|
-
apiKey: string
|
|
28
|
-
authDomain: string
|
|
29
|
-
projectId: string
|
|
30
|
-
storageBucket: string
|
|
31
|
-
messagingSenderId: string
|
|
32
|
-
appId: string
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function FirebaseProvider({ children, config }: FirebaseProviderProps) {
|
|
37
|
-
const [instances, setInstances] = useState<FirebaseInstances | null>(null)
|
|
38
|
-
const [user, setUser] = useState<User | null>(null)
|
|
39
|
-
const [authUser, setAuthUser] = useState<AuthUser | null>(null)
|
|
40
|
-
const [loading, setLoading] = useState(true)
|
|
41
|
-
const [error, setError] = useState<Error | null>(null)
|
|
42
|
-
|
|
43
|
-
useEffect(() => {
|
|
44
|
-
try {
|
|
45
|
-
// Initialize Firebase (singleton - won't re-initialize)
|
|
46
|
-
const firebaseInstances = getFirebaseInstances()
|
|
47
|
-
setInstances(firebaseInstances)
|
|
48
|
-
|
|
49
|
-
// Set up auth state listener
|
|
50
|
-
const unsubscribe = authService.onAuthStateChanged(
|
|
51
|
-
(newAuthUser) => {
|
|
52
|
-
setAuthUser(newAuthUser)
|
|
53
|
-
setUser(newAuthUser ? (firebaseInstances.auth.currentUser || null) : null)
|
|
54
|
-
setLoading(false)
|
|
55
|
-
setError(null)
|
|
56
|
-
},
|
|
57
|
-
(err) => {
|
|
58
|
-
setError(err)
|
|
59
|
-
setLoading(false)
|
|
60
|
-
}
|
|
61
|
-
)
|
|
62
|
-
|
|
63
|
-
return () => {
|
|
64
|
-
if (unsubscribe) unsubscribe()
|
|
65
|
-
}
|
|
66
|
-
} catch (err) {
|
|
67
|
-
setError(err as Error)
|
|
68
|
-
setLoading(false)
|
|
69
|
-
}
|
|
70
|
-
}, [config])
|
|
71
|
-
|
|
72
|
-
const value: FirebaseContextValue = {
|
|
73
|
-
instances: instances!,
|
|
74
|
-
isInitialized: !!instances,
|
|
75
|
-
user,
|
|
76
|
-
authUser,
|
|
77
|
-
loading,
|
|
78
|
-
error,
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return <FirebaseContext.Provider value={value}>{children}</FirebaseContext.Provider>
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function useFirebaseContext(): FirebaseContextValue {
|
|
85
|
-
const context = useContext(FirebaseContext)
|
|
86
|
-
if (!context) {
|
|
87
|
-
throw new Error('useFirebaseContext must be used within FirebaseProvider')
|
|
88
|
-
}
|
|
89
|
-
return context
|
|
90
|
-
}
|