@marcwelti/mw-core 0.2.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/README.md +141 -0
- package/dist/context/index.d.mts +95 -0
- package/dist/context/index.d.ts +95 -0
- package/dist/context/index.js +280 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/index.mjs +276 -0
- package/dist/context/index.mjs.map +1 -0
- package/dist/firebase/index.d.mts +176 -0
- package/dist/firebase/index.d.ts +176 -0
- package/dist/firebase/index.js +393 -0
- package/dist/firebase/index.js.map +1 -0
- package/dist/firebase/index.mjs +318 -0
- package/dist/firebase/index.mjs.map +1 -0
- package/dist/hooks/index.d.mts +97 -0
- package/dist/hooks/index.d.ts +97 -0
- package/dist/hooks/index.js +618 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/index.mjs +611 -0
- package/dist/hooks/index.mjs.map +1 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +922 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +843 -0
- package/dist/index.mjs.map +1 -0
- package/dist/server/index.d.mts +216 -0
- package/dist/server/index.d.ts +216 -0
- package/dist/server/index.js +309 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/index.mjs +276 -0
- package/dist/server/index.mjs.map +1 -0
- package/dist/storage-BU_rfYCi.d.mts +43 -0
- package/dist/storage-BU_rfYCi.d.ts +43 -0
- package/dist/types/index.d.mts +108 -0
- package/dist/types/index.d.ts +108 -0
- package/dist/types/index.js +12 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +3 -0
- package/dist/types/index.mjs.map +1 -0
- package/package.json +91 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,922 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var app$1 = require('firebase/app');
|
|
4
|
+
var auth$1 = require('firebase/auth');
|
|
5
|
+
var firestore = require('firebase/firestore');
|
|
6
|
+
var storage$1 = require('firebase/storage');
|
|
7
|
+
var analytics = require('firebase/analytics');
|
|
8
|
+
var react = require('react');
|
|
9
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
10
|
+
|
|
11
|
+
// src/firebase/config.ts
|
|
12
|
+
function getFirebaseConfig() {
|
|
13
|
+
const config = {
|
|
14
|
+
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY || "",
|
|
15
|
+
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN || "",
|
|
16
|
+
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID || "",
|
|
17
|
+
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET || "",
|
|
18
|
+
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID || "",
|
|
19
|
+
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID || "",
|
|
20
|
+
measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID
|
|
21
|
+
};
|
|
22
|
+
const requiredFields = [
|
|
23
|
+
"apiKey",
|
|
24
|
+
"authDomain",
|
|
25
|
+
"projectId",
|
|
26
|
+
"storageBucket",
|
|
27
|
+
"messagingSenderId",
|
|
28
|
+
"appId"
|
|
29
|
+
];
|
|
30
|
+
const missingFields = requiredFields.filter((field) => !config[field]);
|
|
31
|
+
if (missingFields.length > 0) {
|
|
32
|
+
console.warn(
|
|
33
|
+
`[mw-core] Missing Firebase config fields: ${missingFields.join(", ")}. Make sure to set NEXT_PUBLIC_FIREBASE_* environment variables.`
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
return config;
|
|
37
|
+
}
|
|
38
|
+
function initializeFirebase(config) {
|
|
39
|
+
if (app$1.getApps().length > 0) {
|
|
40
|
+
return app$1.getApp();
|
|
41
|
+
}
|
|
42
|
+
const firebaseConfig = config || getFirebaseConfig();
|
|
43
|
+
return app$1.initializeApp(firebaseConfig);
|
|
44
|
+
}
|
|
45
|
+
var _app = null;
|
|
46
|
+
var _auth = null;
|
|
47
|
+
var _db = null;
|
|
48
|
+
var _storage = null;
|
|
49
|
+
function getFirebaseApp() {
|
|
50
|
+
if (!_app) {
|
|
51
|
+
_app = initializeFirebase();
|
|
52
|
+
}
|
|
53
|
+
return _app;
|
|
54
|
+
}
|
|
55
|
+
function getFirebaseAuth() {
|
|
56
|
+
if (!_auth) {
|
|
57
|
+
_auth = auth$1.getAuth(getFirebaseApp());
|
|
58
|
+
}
|
|
59
|
+
return _auth;
|
|
60
|
+
}
|
|
61
|
+
function getFirebaseFirestore() {
|
|
62
|
+
if (!_db) {
|
|
63
|
+
_db = firestore.getFirestore(getFirebaseApp());
|
|
64
|
+
}
|
|
65
|
+
return _db;
|
|
66
|
+
}
|
|
67
|
+
function getFirebaseStorage() {
|
|
68
|
+
if (!_storage) {
|
|
69
|
+
_storage = storage$1.getStorage(getFirebaseApp());
|
|
70
|
+
}
|
|
71
|
+
return _storage;
|
|
72
|
+
}
|
|
73
|
+
var app = typeof window !== "undefined" ? getFirebaseApp() : null;
|
|
74
|
+
var auth = typeof window !== "undefined" ? getFirebaseAuth() : null;
|
|
75
|
+
var db = typeof window !== "undefined" ? getFirebaseFirestore() : null;
|
|
76
|
+
var storage = typeof window !== "undefined" ? getFirebaseStorage() : null;
|
|
77
|
+
async function signInWithEmail(email, password) {
|
|
78
|
+
const auth2 = getFirebaseAuth();
|
|
79
|
+
return auth$1.signInWithEmailAndPassword(auth2, email, password);
|
|
80
|
+
}
|
|
81
|
+
async function signUpWithEmail(email, password, displayName) {
|
|
82
|
+
const auth2 = getFirebaseAuth();
|
|
83
|
+
const credential = await auth$1.createUserWithEmailAndPassword(auth2, email, password);
|
|
84
|
+
if (displayName && credential.user) {
|
|
85
|
+
await auth$1.updateProfile(credential.user, { displayName });
|
|
86
|
+
}
|
|
87
|
+
return credential;
|
|
88
|
+
}
|
|
89
|
+
async function signOut() {
|
|
90
|
+
const auth2 = getFirebaseAuth();
|
|
91
|
+
return auth$1.signOut(auth2);
|
|
92
|
+
}
|
|
93
|
+
async function resetPassword(email) {
|
|
94
|
+
const auth2 = getFirebaseAuth();
|
|
95
|
+
return auth$1.sendPasswordResetEmail(auth2, email);
|
|
96
|
+
}
|
|
97
|
+
async function sendVerificationEmail(user) {
|
|
98
|
+
return auth$1.sendEmailVerification(user);
|
|
99
|
+
}
|
|
100
|
+
async function updateUserProfile(user, profile) {
|
|
101
|
+
return auth$1.updateProfile(user, profile);
|
|
102
|
+
}
|
|
103
|
+
async function signInWithGoogle() {
|
|
104
|
+
const auth2 = getFirebaseAuth();
|
|
105
|
+
const provider = new auth$1.GoogleAuthProvider();
|
|
106
|
+
provider.addScope("email");
|
|
107
|
+
provider.addScope("profile");
|
|
108
|
+
return auth$1.signInWithPopup(auth2, provider);
|
|
109
|
+
}
|
|
110
|
+
async function signInWithGoogleRedirect() {
|
|
111
|
+
const auth2 = getFirebaseAuth();
|
|
112
|
+
const provider = new auth$1.GoogleAuthProvider();
|
|
113
|
+
provider.addScope("email");
|
|
114
|
+
provider.addScope("profile");
|
|
115
|
+
return auth$1.signInWithRedirect(auth2, provider);
|
|
116
|
+
}
|
|
117
|
+
async function getGoogleRedirectResult() {
|
|
118
|
+
const auth2 = getFirebaseAuth();
|
|
119
|
+
return auth$1.getRedirectResult(auth2);
|
|
120
|
+
}
|
|
121
|
+
function subscribeToAuthState(callback) {
|
|
122
|
+
const auth2 = getFirebaseAuth();
|
|
123
|
+
return auth$1.onAuthStateChanged(auth2, callback);
|
|
124
|
+
}
|
|
125
|
+
function getCurrentUser() {
|
|
126
|
+
const auth2 = getFirebaseAuth();
|
|
127
|
+
return auth2.currentUser;
|
|
128
|
+
}
|
|
129
|
+
function waitForAuthState() {
|
|
130
|
+
return new Promise((resolve) => {
|
|
131
|
+
const auth2 = getFirebaseAuth();
|
|
132
|
+
const unsubscribe = auth$1.onAuthStateChanged(auth2, (user) => {
|
|
133
|
+
unsubscribe();
|
|
134
|
+
resolve(user);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
function getDocRef(collectionPath, documentId) {
|
|
139
|
+
const db2 = getFirebaseFirestore();
|
|
140
|
+
return firestore.doc(db2, collectionPath, documentId);
|
|
141
|
+
}
|
|
142
|
+
function getCollectionRef(collectionPath) {
|
|
143
|
+
const db2 = getFirebaseFirestore();
|
|
144
|
+
return firestore.collection(db2, collectionPath);
|
|
145
|
+
}
|
|
146
|
+
async function getDocument(collectionPath, documentId) {
|
|
147
|
+
const docRef = getDocRef(collectionPath, documentId);
|
|
148
|
+
const docSnap = await firestore.getDoc(docRef);
|
|
149
|
+
if (docSnap.exists()) {
|
|
150
|
+
return { id: docSnap.id, ...docSnap.data() };
|
|
151
|
+
}
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
async function getCollection(collectionPath, constraints = []) {
|
|
155
|
+
const collectionRef = getCollectionRef(collectionPath);
|
|
156
|
+
const q = firestore.query(collectionRef, ...constraints);
|
|
157
|
+
const querySnapshot = await firestore.getDocs(q);
|
|
158
|
+
return querySnapshot.docs.map((doc2) => ({
|
|
159
|
+
id: doc2.id,
|
|
160
|
+
...doc2.data()
|
|
161
|
+
}));
|
|
162
|
+
}
|
|
163
|
+
async function addDocument(collectionPath, data) {
|
|
164
|
+
const collectionRef = getCollectionRef(collectionPath);
|
|
165
|
+
const docRef = await firestore.addDoc(collectionRef, {
|
|
166
|
+
...data,
|
|
167
|
+
createdAt: firestore.serverTimestamp(),
|
|
168
|
+
updatedAt: firestore.serverTimestamp()
|
|
169
|
+
});
|
|
170
|
+
return docRef.id;
|
|
171
|
+
}
|
|
172
|
+
async function setDocument(collectionPath, documentId, data, merge = false) {
|
|
173
|
+
const docRef = getDocRef(collectionPath, documentId);
|
|
174
|
+
await firestore.setDoc(
|
|
175
|
+
docRef,
|
|
176
|
+
{
|
|
177
|
+
...data,
|
|
178
|
+
updatedAt: firestore.serverTimestamp(),
|
|
179
|
+
...merge ? {} : { createdAt: firestore.serverTimestamp() }
|
|
180
|
+
},
|
|
181
|
+
{ merge }
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
async function updateDocument(collectionPath, documentId, data) {
|
|
185
|
+
const docRef = getDocRef(collectionPath, documentId);
|
|
186
|
+
await firestore.updateDoc(docRef, {
|
|
187
|
+
...data,
|
|
188
|
+
updatedAt: firestore.serverTimestamp()
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
async function deleteDocument(collectionPath, documentId) {
|
|
192
|
+
const docRef = getDocRef(collectionPath, documentId);
|
|
193
|
+
await firestore.deleteDoc(docRef);
|
|
194
|
+
}
|
|
195
|
+
function subscribeToDocument(collectionPath, documentId, callback) {
|
|
196
|
+
const docRef = getDocRef(collectionPath, documentId);
|
|
197
|
+
return firestore.onSnapshot(docRef, (docSnap) => {
|
|
198
|
+
if (docSnap.exists()) {
|
|
199
|
+
callback({ id: docSnap.id, ...docSnap.data() });
|
|
200
|
+
} else {
|
|
201
|
+
callback(null);
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
function subscribeToCollection(collectionPath, callback, constraints = []) {
|
|
206
|
+
const collectionRef = getCollectionRef(collectionPath);
|
|
207
|
+
const q = firestore.query(collectionRef, ...constraints);
|
|
208
|
+
return firestore.onSnapshot(q, (querySnapshot) => {
|
|
209
|
+
const data = querySnapshot.docs.map((doc2) => ({
|
|
210
|
+
id: doc2.id,
|
|
211
|
+
...doc2.data()
|
|
212
|
+
}));
|
|
213
|
+
callback(data);
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
function getStorageRef(path) {
|
|
217
|
+
const storage2 = getFirebaseStorage();
|
|
218
|
+
return storage$1.ref(storage2, path);
|
|
219
|
+
}
|
|
220
|
+
async function uploadFile(path, file, metadata) {
|
|
221
|
+
const storageRef = getStorageRef(path);
|
|
222
|
+
await storage$1.uploadBytes(storageRef, file, metadata);
|
|
223
|
+
return storage$1.getDownloadURL(storageRef);
|
|
224
|
+
}
|
|
225
|
+
function uploadFileWithProgress(path, file, onProgress, metadata) {
|
|
226
|
+
const storageRef = getStorageRef(path);
|
|
227
|
+
const task = storage$1.uploadBytesResumable(storageRef, file, metadata);
|
|
228
|
+
if (onProgress) {
|
|
229
|
+
task.on("state_changed", (snapshot) => {
|
|
230
|
+
const progress = snapshot.bytesTransferred / snapshot.totalBytes * 100;
|
|
231
|
+
onProgress(progress);
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
const promise = new Promise((resolve, reject) => {
|
|
235
|
+
task.on(
|
|
236
|
+
"state_changed",
|
|
237
|
+
null,
|
|
238
|
+
(error) => reject(error),
|
|
239
|
+
async () => {
|
|
240
|
+
const url = await storage$1.getDownloadURL(task.snapshot.ref);
|
|
241
|
+
resolve(url);
|
|
242
|
+
}
|
|
243
|
+
);
|
|
244
|
+
});
|
|
245
|
+
return { task, promise };
|
|
246
|
+
}
|
|
247
|
+
async function getFileURL(path) {
|
|
248
|
+
const storageRef = getStorageRef(path);
|
|
249
|
+
return storage$1.getDownloadURL(storageRef);
|
|
250
|
+
}
|
|
251
|
+
async function deleteFile(path) {
|
|
252
|
+
const storageRef = getStorageRef(path);
|
|
253
|
+
return storage$1.deleteObject(storageRef);
|
|
254
|
+
}
|
|
255
|
+
async function listFiles(path) {
|
|
256
|
+
const storageRef = getStorageRef(path);
|
|
257
|
+
return storage$1.listAll(storageRef);
|
|
258
|
+
}
|
|
259
|
+
async function getFileMetadata(path) {
|
|
260
|
+
const storageRef = getStorageRef(path);
|
|
261
|
+
return storage$1.getMetadata(storageRef);
|
|
262
|
+
}
|
|
263
|
+
async function updateFileMetadata(path, metadata) {
|
|
264
|
+
const storageRef = getStorageRef(path);
|
|
265
|
+
return storage$1.updateMetadata(storageRef, metadata);
|
|
266
|
+
}
|
|
267
|
+
function generateFilePath(folder, filename, userId) {
|
|
268
|
+
const timestamp = Date.now();
|
|
269
|
+
const extension = filename.split(".").pop() || "";
|
|
270
|
+
const baseName = filename.replace(/\.[^/.]+$/, "");
|
|
271
|
+
const sanitizedName = baseName.replace(/[^a-zA-Z0-9]/g, "_");
|
|
272
|
+
if (userId) {
|
|
273
|
+
return `${folder}/${userId}/${timestamp}_${sanitizedName}.${extension}`;
|
|
274
|
+
}
|
|
275
|
+
return `${folder}/${timestamp}_${sanitizedName}.${extension}`;
|
|
276
|
+
}
|
|
277
|
+
var _analytics = null;
|
|
278
|
+
async function getFirebaseAnalytics() {
|
|
279
|
+
if (typeof window === "undefined") {
|
|
280
|
+
return null;
|
|
281
|
+
}
|
|
282
|
+
if (_analytics) {
|
|
283
|
+
return _analytics;
|
|
284
|
+
}
|
|
285
|
+
const supported = await analytics.isSupported();
|
|
286
|
+
if (!supported) {
|
|
287
|
+
console.warn("[mw-core] Firebase Analytics is not supported in this environment");
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
_analytics = analytics.getAnalytics(getFirebaseApp());
|
|
291
|
+
return _analytics;
|
|
292
|
+
}
|
|
293
|
+
async function trackEvent(eventName, eventParams) {
|
|
294
|
+
const analytics$1 = await getFirebaseAnalytics();
|
|
295
|
+
if (analytics$1) {
|
|
296
|
+
analytics.logEvent(analytics$1, eventName, eventParams);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
async function trackPageView(pagePath, pageTitle) {
|
|
300
|
+
await trackEvent("page_view", {
|
|
301
|
+
page_path: pagePath,
|
|
302
|
+
page_title: pageTitle
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
async function trackUserAction(action, category, label, value) {
|
|
306
|
+
await trackEvent(action, {
|
|
307
|
+
event_category: category,
|
|
308
|
+
event_label: label,
|
|
309
|
+
value
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
async function trackSignUp(method) {
|
|
313
|
+
await trackEvent("sign_up", { method });
|
|
314
|
+
}
|
|
315
|
+
async function trackLogin(method) {
|
|
316
|
+
await trackEvent("login", { method });
|
|
317
|
+
}
|
|
318
|
+
function useAuth() {
|
|
319
|
+
const [user, setUser] = react.useState(null);
|
|
320
|
+
const [loading, setLoading] = react.useState(true);
|
|
321
|
+
const [error, setError] = react.useState(null);
|
|
322
|
+
react.useEffect(() => {
|
|
323
|
+
const unsubscribe = subscribeToAuthState((user2) => {
|
|
324
|
+
setUser(user2);
|
|
325
|
+
setLoading(false);
|
|
326
|
+
});
|
|
327
|
+
return unsubscribe;
|
|
328
|
+
}, []);
|
|
329
|
+
const signIn = react.useCallback(async (email, password) => {
|
|
330
|
+
setError(null);
|
|
331
|
+
try {
|
|
332
|
+
return await signInWithEmail(email, password);
|
|
333
|
+
} catch (err) {
|
|
334
|
+
const error2 = err;
|
|
335
|
+
setError(error2);
|
|
336
|
+
throw error2;
|
|
337
|
+
}
|
|
338
|
+
}, []);
|
|
339
|
+
const signUp = react.useCallback(
|
|
340
|
+
async (email, password, displayName) => {
|
|
341
|
+
setError(null);
|
|
342
|
+
try {
|
|
343
|
+
return await signUpWithEmail(email, password, displayName);
|
|
344
|
+
} catch (err) {
|
|
345
|
+
const error2 = err;
|
|
346
|
+
setError(error2);
|
|
347
|
+
throw error2;
|
|
348
|
+
}
|
|
349
|
+
},
|
|
350
|
+
[]
|
|
351
|
+
);
|
|
352
|
+
const signOut2 = react.useCallback(async () => {
|
|
353
|
+
setError(null);
|
|
354
|
+
try {
|
|
355
|
+
await signOut();
|
|
356
|
+
} catch (err) {
|
|
357
|
+
const error2 = err;
|
|
358
|
+
setError(error2);
|
|
359
|
+
throw error2;
|
|
360
|
+
}
|
|
361
|
+
}, []);
|
|
362
|
+
const signInWithGoogle2 = react.useCallback(async () => {
|
|
363
|
+
setError(null);
|
|
364
|
+
try {
|
|
365
|
+
return await signInWithGoogle();
|
|
366
|
+
} catch (err) {
|
|
367
|
+
const error2 = err;
|
|
368
|
+
setError(error2);
|
|
369
|
+
throw error2;
|
|
370
|
+
}
|
|
371
|
+
}, []);
|
|
372
|
+
const resetPassword2 = react.useCallback(async (email) => {
|
|
373
|
+
setError(null);
|
|
374
|
+
try {
|
|
375
|
+
await resetPassword(email);
|
|
376
|
+
} catch (err) {
|
|
377
|
+
const error2 = err;
|
|
378
|
+
setError(error2);
|
|
379
|
+
throw error2;
|
|
380
|
+
}
|
|
381
|
+
}, []);
|
|
382
|
+
const updateProfile2 = react.useCallback(
|
|
383
|
+
async (profile) => {
|
|
384
|
+
setError(null);
|
|
385
|
+
if (!user) {
|
|
386
|
+
const error2 = new Error("No authenticated user");
|
|
387
|
+
setError(error2);
|
|
388
|
+
throw error2;
|
|
389
|
+
}
|
|
390
|
+
try {
|
|
391
|
+
await updateUserProfile(user, profile);
|
|
392
|
+
} catch (err) {
|
|
393
|
+
const error2 = err;
|
|
394
|
+
setError(error2);
|
|
395
|
+
throw error2;
|
|
396
|
+
}
|
|
397
|
+
},
|
|
398
|
+
[user]
|
|
399
|
+
);
|
|
400
|
+
const sendEmailVerificationFn = react.useCallback(async () => {
|
|
401
|
+
setError(null);
|
|
402
|
+
if (!user) {
|
|
403
|
+
const error2 = new Error("No authenticated user");
|
|
404
|
+
setError(error2);
|
|
405
|
+
throw error2;
|
|
406
|
+
}
|
|
407
|
+
try {
|
|
408
|
+
await sendVerificationEmail(user);
|
|
409
|
+
} catch (err) {
|
|
410
|
+
const error2 = err;
|
|
411
|
+
setError(error2);
|
|
412
|
+
throw error2;
|
|
413
|
+
}
|
|
414
|
+
}, [user]);
|
|
415
|
+
const clearError = react.useCallback(() => {
|
|
416
|
+
setError(null);
|
|
417
|
+
}, []);
|
|
418
|
+
return {
|
|
419
|
+
user,
|
|
420
|
+
loading,
|
|
421
|
+
error,
|
|
422
|
+
signIn,
|
|
423
|
+
signUp,
|
|
424
|
+
signOut: signOut2,
|
|
425
|
+
signInWithGoogle: signInWithGoogle2,
|
|
426
|
+
resetPassword: resetPassword2,
|
|
427
|
+
updateProfile: updateProfile2,
|
|
428
|
+
sendEmailVerification: sendEmailVerificationFn,
|
|
429
|
+
clearError
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
function useDocument(collectionPath, documentId, options = {}) {
|
|
433
|
+
const { subscribe = false } = options;
|
|
434
|
+
const [data, setData] = react.useState(null);
|
|
435
|
+
const [loading, setLoading] = react.useState(true);
|
|
436
|
+
const [error, setError] = react.useState(null);
|
|
437
|
+
react.useEffect(() => {
|
|
438
|
+
if (!documentId) {
|
|
439
|
+
setData(null);
|
|
440
|
+
setLoading(false);
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
setLoading(true);
|
|
444
|
+
setError(null);
|
|
445
|
+
if (subscribe) {
|
|
446
|
+
const unsubscribe = subscribeToDocument(
|
|
447
|
+
collectionPath,
|
|
448
|
+
documentId,
|
|
449
|
+
(doc2) => {
|
|
450
|
+
setData(doc2);
|
|
451
|
+
setLoading(false);
|
|
452
|
+
}
|
|
453
|
+
);
|
|
454
|
+
return unsubscribe;
|
|
455
|
+
} else {
|
|
456
|
+
getDocument(collectionPath, documentId).then((doc2) => {
|
|
457
|
+
setData(doc2);
|
|
458
|
+
setLoading(false);
|
|
459
|
+
}).catch((err) => {
|
|
460
|
+
setError(err);
|
|
461
|
+
setLoading(false);
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
}, [collectionPath, documentId, subscribe]);
|
|
465
|
+
const refresh = react.useCallback(async () => {
|
|
466
|
+
if (!documentId) return;
|
|
467
|
+
setLoading(true);
|
|
468
|
+
try {
|
|
469
|
+
const doc2 = await getDocument(collectionPath, documentId);
|
|
470
|
+
setData(doc2);
|
|
471
|
+
} catch (err) {
|
|
472
|
+
setError(err);
|
|
473
|
+
} finally {
|
|
474
|
+
setLoading(false);
|
|
475
|
+
}
|
|
476
|
+
}, [collectionPath, documentId]);
|
|
477
|
+
return { data, loading, error, refresh };
|
|
478
|
+
}
|
|
479
|
+
function useCollection(collectionPath, constraints = [], options = {}) {
|
|
480
|
+
const { subscribe = false } = options;
|
|
481
|
+
const [data, setData] = react.useState([]);
|
|
482
|
+
const [loading, setLoading] = react.useState(true);
|
|
483
|
+
const [error, setError] = react.useState(null);
|
|
484
|
+
const constraintsKey = JSON.stringify(
|
|
485
|
+
constraints.map((c) => c.type)
|
|
486
|
+
);
|
|
487
|
+
react.useEffect(() => {
|
|
488
|
+
setLoading(true);
|
|
489
|
+
setError(null);
|
|
490
|
+
if (subscribe) {
|
|
491
|
+
const unsubscribe = subscribeToCollection(
|
|
492
|
+
collectionPath,
|
|
493
|
+
(docs) => {
|
|
494
|
+
setData(docs);
|
|
495
|
+
setLoading(false);
|
|
496
|
+
},
|
|
497
|
+
constraints
|
|
498
|
+
);
|
|
499
|
+
return unsubscribe;
|
|
500
|
+
} else {
|
|
501
|
+
getCollection(collectionPath, constraints).then((docs) => {
|
|
502
|
+
setData(docs);
|
|
503
|
+
setLoading(false);
|
|
504
|
+
}).catch((err) => {
|
|
505
|
+
setError(err);
|
|
506
|
+
setLoading(false);
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
}, [collectionPath, constraintsKey, subscribe]);
|
|
510
|
+
const refresh = react.useCallback(async () => {
|
|
511
|
+
setLoading(true);
|
|
512
|
+
try {
|
|
513
|
+
const docs = await getCollection(collectionPath, constraints);
|
|
514
|
+
setData(docs);
|
|
515
|
+
} catch (err) {
|
|
516
|
+
setError(err);
|
|
517
|
+
} finally {
|
|
518
|
+
setLoading(false);
|
|
519
|
+
}
|
|
520
|
+
}, [collectionPath, constraintsKey]);
|
|
521
|
+
return { data, loading, error, refresh };
|
|
522
|
+
}
|
|
523
|
+
function useFirestoreMutation(collectionPath) {
|
|
524
|
+
const [loading, setLoading] = react.useState(false);
|
|
525
|
+
const [error, setError] = react.useState(null);
|
|
526
|
+
const add = react.useCallback(
|
|
527
|
+
async (data) => {
|
|
528
|
+
setLoading(true);
|
|
529
|
+
setError(null);
|
|
530
|
+
try {
|
|
531
|
+
const id = await addDocument(collectionPath, data);
|
|
532
|
+
return id;
|
|
533
|
+
} catch (err) {
|
|
534
|
+
setError(err);
|
|
535
|
+
throw err;
|
|
536
|
+
} finally {
|
|
537
|
+
setLoading(false);
|
|
538
|
+
}
|
|
539
|
+
},
|
|
540
|
+
[collectionPath]
|
|
541
|
+
);
|
|
542
|
+
const set = react.useCallback(
|
|
543
|
+
async (documentId, data, merge = false) => {
|
|
544
|
+
setLoading(true);
|
|
545
|
+
setError(null);
|
|
546
|
+
try {
|
|
547
|
+
await setDocument(collectionPath, documentId, data, merge);
|
|
548
|
+
} catch (err) {
|
|
549
|
+
setError(err);
|
|
550
|
+
throw err;
|
|
551
|
+
} finally {
|
|
552
|
+
setLoading(false);
|
|
553
|
+
}
|
|
554
|
+
},
|
|
555
|
+
[collectionPath]
|
|
556
|
+
);
|
|
557
|
+
const update = react.useCallback(
|
|
558
|
+
async (documentId, data) => {
|
|
559
|
+
setLoading(true);
|
|
560
|
+
setError(null);
|
|
561
|
+
try {
|
|
562
|
+
await updateDocument(collectionPath, documentId, data);
|
|
563
|
+
} catch (err) {
|
|
564
|
+
setError(err);
|
|
565
|
+
throw err;
|
|
566
|
+
} finally {
|
|
567
|
+
setLoading(false);
|
|
568
|
+
}
|
|
569
|
+
},
|
|
570
|
+
[collectionPath]
|
|
571
|
+
);
|
|
572
|
+
const remove = react.useCallback(
|
|
573
|
+
async (documentId) => {
|
|
574
|
+
setLoading(true);
|
|
575
|
+
setError(null);
|
|
576
|
+
try {
|
|
577
|
+
await deleteDocument(collectionPath, documentId);
|
|
578
|
+
} catch (err) {
|
|
579
|
+
setError(err);
|
|
580
|
+
throw err;
|
|
581
|
+
} finally {
|
|
582
|
+
setLoading(false);
|
|
583
|
+
}
|
|
584
|
+
},
|
|
585
|
+
[collectionPath]
|
|
586
|
+
);
|
|
587
|
+
const clearError = react.useCallback(() => {
|
|
588
|
+
setError(null);
|
|
589
|
+
}, []);
|
|
590
|
+
return {
|
|
591
|
+
add,
|
|
592
|
+
set,
|
|
593
|
+
update,
|
|
594
|
+
remove,
|
|
595
|
+
loading,
|
|
596
|
+
error,
|
|
597
|
+
clearError
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
function useStorage() {
|
|
601
|
+
const [uploading, setUploading] = react.useState(false);
|
|
602
|
+
const [progress, setProgress] = react.useState(0);
|
|
603
|
+
const [error, setError] = react.useState(null);
|
|
604
|
+
const upload = react.useCallback(
|
|
605
|
+
async (path, file, metadata) => {
|
|
606
|
+
setError(null);
|
|
607
|
+
setUploading(true);
|
|
608
|
+
setProgress(0);
|
|
609
|
+
try {
|
|
610
|
+
const url = await uploadFile(path, file, metadata);
|
|
611
|
+
setProgress(100);
|
|
612
|
+
return url;
|
|
613
|
+
} catch (err) {
|
|
614
|
+
const error2 = err;
|
|
615
|
+
setError(error2);
|
|
616
|
+
throw error2;
|
|
617
|
+
} finally {
|
|
618
|
+
setUploading(false);
|
|
619
|
+
}
|
|
620
|
+
},
|
|
621
|
+
[]
|
|
622
|
+
);
|
|
623
|
+
const uploadWithProgress = react.useCallback(
|
|
624
|
+
async (path, file, metadata) => {
|
|
625
|
+
setError(null);
|
|
626
|
+
setUploading(true);
|
|
627
|
+
setProgress(0);
|
|
628
|
+
try {
|
|
629
|
+
const { promise } = uploadFileWithProgress(
|
|
630
|
+
path,
|
|
631
|
+
file,
|
|
632
|
+
(p) => setProgress(p),
|
|
633
|
+
metadata
|
|
634
|
+
);
|
|
635
|
+
const url = await promise;
|
|
636
|
+
return url;
|
|
637
|
+
} catch (err) {
|
|
638
|
+
const error2 = err;
|
|
639
|
+
setError(error2);
|
|
640
|
+
throw error2;
|
|
641
|
+
} finally {
|
|
642
|
+
setUploading(false);
|
|
643
|
+
}
|
|
644
|
+
},
|
|
645
|
+
[]
|
|
646
|
+
);
|
|
647
|
+
const remove = react.useCallback(async (path) => {
|
|
648
|
+
setError(null);
|
|
649
|
+
try {
|
|
650
|
+
await deleteFile(path);
|
|
651
|
+
} catch (err) {
|
|
652
|
+
const error2 = err;
|
|
653
|
+
setError(error2);
|
|
654
|
+
throw error2;
|
|
655
|
+
}
|
|
656
|
+
}, []);
|
|
657
|
+
const getURL = react.useCallback(async (path) => {
|
|
658
|
+
setError(null);
|
|
659
|
+
try {
|
|
660
|
+
return await getFileURL(path);
|
|
661
|
+
} catch (err) {
|
|
662
|
+
const error2 = err;
|
|
663
|
+
setError(error2);
|
|
664
|
+
throw error2;
|
|
665
|
+
}
|
|
666
|
+
}, []);
|
|
667
|
+
const clearError = react.useCallback(() => {
|
|
668
|
+
setError(null);
|
|
669
|
+
}, []);
|
|
670
|
+
return {
|
|
671
|
+
upload,
|
|
672
|
+
uploadWithProgress,
|
|
673
|
+
remove,
|
|
674
|
+
getURL,
|
|
675
|
+
uploading,
|
|
676
|
+
progress,
|
|
677
|
+
error,
|
|
678
|
+
clearError
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
var AuthContext = react.createContext(void 0);
|
|
682
|
+
function AuthProvider({
|
|
683
|
+
children,
|
|
684
|
+
onAuthStateChange,
|
|
685
|
+
loadingComponent
|
|
686
|
+
}) {
|
|
687
|
+
const [user, setUser] = react.useState(null);
|
|
688
|
+
const [loading, setLoading] = react.useState(true);
|
|
689
|
+
const [error, setError] = react.useState(null);
|
|
690
|
+
react.useEffect(() => {
|
|
691
|
+
const unsubscribe = subscribeToAuthState((user2) => {
|
|
692
|
+
setUser(user2);
|
|
693
|
+
setLoading(false);
|
|
694
|
+
onAuthStateChange?.(user2);
|
|
695
|
+
});
|
|
696
|
+
return unsubscribe;
|
|
697
|
+
}, [onAuthStateChange]);
|
|
698
|
+
const signIn = react.useCallback(async (email, password) => {
|
|
699
|
+
setError(null);
|
|
700
|
+
try {
|
|
701
|
+
return await signInWithEmail(email, password);
|
|
702
|
+
} catch (err) {
|
|
703
|
+
const error2 = err;
|
|
704
|
+
setError(error2);
|
|
705
|
+
throw error2;
|
|
706
|
+
}
|
|
707
|
+
}, []);
|
|
708
|
+
const signUp = react.useCallback(
|
|
709
|
+
async (email, password, displayName) => {
|
|
710
|
+
setError(null);
|
|
711
|
+
try {
|
|
712
|
+
return await signUpWithEmail(email, password, displayName);
|
|
713
|
+
} catch (err) {
|
|
714
|
+
const error2 = err;
|
|
715
|
+
setError(error2);
|
|
716
|
+
throw error2;
|
|
717
|
+
}
|
|
718
|
+
},
|
|
719
|
+
[]
|
|
720
|
+
);
|
|
721
|
+
const signOut2 = react.useCallback(async () => {
|
|
722
|
+
setError(null);
|
|
723
|
+
try {
|
|
724
|
+
await signOut();
|
|
725
|
+
} catch (err) {
|
|
726
|
+
const error2 = err;
|
|
727
|
+
setError(error2);
|
|
728
|
+
throw error2;
|
|
729
|
+
}
|
|
730
|
+
}, []);
|
|
731
|
+
const signInWithGoogle2 = react.useCallback(async () => {
|
|
732
|
+
setError(null);
|
|
733
|
+
try {
|
|
734
|
+
return await signInWithGoogle();
|
|
735
|
+
} catch (err) {
|
|
736
|
+
const error2 = err;
|
|
737
|
+
setError(error2);
|
|
738
|
+
throw error2;
|
|
739
|
+
}
|
|
740
|
+
}, []);
|
|
741
|
+
const resetPassword2 = react.useCallback(async (email) => {
|
|
742
|
+
setError(null);
|
|
743
|
+
try {
|
|
744
|
+
await resetPassword(email);
|
|
745
|
+
} catch (err) {
|
|
746
|
+
const error2 = err;
|
|
747
|
+
setError(error2);
|
|
748
|
+
throw error2;
|
|
749
|
+
}
|
|
750
|
+
}, []);
|
|
751
|
+
const updateProfile2 = react.useCallback(
|
|
752
|
+
async (profile) => {
|
|
753
|
+
setError(null);
|
|
754
|
+
if (!user) {
|
|
755
|
+
const error2 = new Error("No authenticated user");
|
|
756
|
+
setError(error2);
|
|
757
|
+
throw error2;
|
|
758
|
+
}
|
|
759
|
+
try {
|
|
760
|
+
await updateUserProfile(user, profile);
|
|
761
|
+
} catch (err) {
|
|
762
|
+
const error2 = err;
|
|
763
|
+
setError(error2);
|
|
764
|
+
throw error2;
|
|
765
|
+
}
|
|
766
|
+
},
|
|
767
|
+
[user]
|
|
768
|
+
);
|
|
769
|
+
const sendEmailVerificationFn = react.useCallback(async () => {
|
|
770
|
+
setError(null);
|
|
771
|
+
if (!user) {
|
|
772
|
+
const error2 = new Error("No authenticated user");
|
|
773
|
+
setError(error2);
|
|
774
|
+
throw error2;
|
|
775
|
+
}
|
|
776
|
+
try {
|
|
777
|
+
await sendVerificationEmail(user);
|
|
778
|
+
} catch (err) {
|
|
779
|
+
const error2 = err;
|
|
780
|
+
setError(error2);
|
|
781
|
+
throw error2;
|
|
782
|
+
}
|
|
783
|
+
}, [user]);
|
|
784
|
+
const clearError = react.useCallback(() => {
|
|
785
|
+
setError(null);
|
|
786
|
+
}, []);
|
|
787
|
+
const value = react.useMemo(
|
|
788
|
+
() => ({
|
|
789
|
+
user,
|
|
790
|
+
loading,
|
|
791
|
+
isAuthenticated: !!user,
|
|
792
|
+
error,
|
|
793
|
+
signIn,
|
|
794
|
+
signUp,
|
|
795
|
+
signOut: signOut2,
|
|
796
|
+
signInWithGoogle: signInWithGoogle2,
|
|
797
|
+
resetPassword: resetPassword2,
|
|
798
|
+
updateProfile: updateProfile2,
|
|
799
|
+
sendEmailVerification: sendEmailVerificationFn,
|
|
800
|
+
clearError
|
|
801
|
+
}),
|
|
802
|
+
[
|
|
803
|
+
user,
|
|
804
|
+
loading,
|
|
805
|
+
error,
|
|
806
|
+
signIn,
|
|
807
|
+
signUp,
|
|
808
|
+
signOut2,
|
|
809
|
+
signInWithGoogle2,
|
|
810
|
+
resetPassword2,
|
|
811
|
+
updateProfile2,
|
|
812
|
+
sendEmailVerificationFn,
|
|
813
|
+
clearError
|
|
814
|
+
]
|
|
815
|
+
);
|
|
816
|
+
if (loading && loadingComponent) {
|
|
817
|
+
return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: loadingComponent });
|
|
818
|
+
}
|
|
819
|
+
return /* @__PURE__ */ jsxRuntime.jsx(AuthContext.Provider, { value, children });
|
|
820
|
+
}
|
|
821
|
+
function useAuthContext() {
|
|
822
|
+
const context = react.useContext(AuthContext);
|
|
823
|
+
if (context === void 0) {
|
|
824
|
+
throw new Error("useAuthContext must be used within an AuthProvider");
|
|
825
|
+
}
|
|
826
|
+
return context;
|
|
827
|
+
}
|
|
828
|
+
function withAuth(WrappedComponent, options = {}) {
|
|
829
|
+
const { LoadingComponent, UnauthenticatedComponent } = options;
|
|
830
|
+
return function WithAuthComponent(props) {
|
|
831
|
+
const { user, loading, isAuthenticated } = useAuthContext();
|
|
832
|
+
if (loading) {
|
|
833
|
+
return LoadingComponent ? /* @__PURE__ */ jsxRuntime.jsx(LoadingComponent, {}) : /* @__PURE__ */ jsxRuntime.jsx("div", { children: "Loading..." });
|
|
834
|
+
}
|
|
835
|
+
if (!isAuthenticated) {
|
|
836
|
+
return UnauthenticatedComponent ? /* @__PURE__ */ jsxRuntime.jsx(UnauthenticatedComponent, {}) : /* @__PURE__ */ jsxRuntime.jsx("div", { children: "Please sign in to continue" });
|
|
837
|
+
}
|
|
838
|
+
return /* @__PURE__ */ jsxRuntime.jsx(WrappedComponent, { ...props });
|
|
839
|
+
};
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
Object.defineProperty(exports, "limit", {
|
|
843
|
+
enumerable: true,
|
|
844
|
+
get: function () { return firestore.limit; }
|
|
845
|
+
});
|
|
846
|
+
Object.defineProperty(exports, "orderBy", {
|
|
847
|
+
enumerable: true,
|
|
848
|
+
get: function () { return firestore.orderBy; }
|
|
849
|
+
});
|
|
850
|
+
Object.defineProperty(exports, "query", {
|
|
851
|
+
enumerable: true,
|
|
852
|
+
get: function () { return firestore.query; }
|
|
853
|
+
});
|
|
854
|
+
Object.defineProperty(exports, "serverTimestamp", {
|
|
855
|
+
enumerable: true,
|
|
856
|
+
get: function () { return firestore.serverTimestamp; }
|
|
857
|
+
});
|
|
858
|
+
Object.defineProperty(exports, "startAfter", {
|
|
859
|
+
enumerable: true,
|
|
860
|
+
get: function () { return firestore.startAfter; }
|
|
861
|
+
});
|
|
862
|
+
Object.defineProperty(exports, "where", {
|
|
863
|
+
enumerable: true,
|
|
864
|
+
get: function () { return firestore.where; }
|
|
865
|
+
});
|
|
866
|
+
exports.AuthProvider = AuthProvider;
|
|
867
|
+
exports.addDocument = addDocument;
|
|
868
|
+
exports.app = app;
|
|
869
|
+
exports.auth = auth;
|
|
870
|
+
exports.db = db;
|
|
871
|
+
exports.deleteDocument = deleteDocument;
|
|
872
|
+
exports.deleteFile = deleteFile;
|
|
873
|
+
exports.generateFilePath = generateFilePath;
|
|
874
|
+
exports.getCollection = getCollection;
|
|
875
|
+
exports.getCollectionRef = getCollectionRef;
|
|
876
|
+
exports.getCurrentUser = getCurrentUser;
|
|
877
|
+
exports.getDocRef = getDocRef;
|
|
878
|
+
exports.getDocument = getDocument;
|
|
879
|
+
exports.getFileMetadata = getFileMetadata;
|
|
880
|
+
exports.getFileURL = getFileURL;
|
|
881
|
+
exports.getFirebaseAnalytics = getFirebaseAnalytics;
|
|
882
|
+
exports.getFirebaseApp = getFirebaseApp;
|
|
883
|
+
exports.getFirebaseAuth = getFirebaseAuth;
|
|
884
|
+
exports.getFirebaseConfig = getFirebaseConfig;
|
|
885
|
+
exports.getFirebaseFirestore = getFirebaseFirestore;
|
|
886
|
+
exports.getFirebaseStorage = getFirebaseStorage;
|
|
887
|
+
exports.getGoogleRedirectResult = getGoogleRedirectResult;
|
|
888
|
+
exports.getStorageRef = getStorageRef;
|
|
889
|
+
exports.initializeFirebase = initializeFirebase;
|
|
890
|
+
exports.listFiles = listFiles;
|
|
891
|
+
exports.resetPassword = resetPassword;
|
|
892
|
+
exports.sendVerificationEmail = sendVerificationEmail;
|
|
893
|
+
exports.setDocument = setDocument;
|
|
894
|
+
exports.signInWithEmail = signInWithEmail;
|
|
895
|
+
exports.signInWithGoogle = signInWithGoogle;
|
|
896
|
+
exports.signInWithGoogleRedirect = signInWithGoogleRedirect;
|
|
897
|
+
exports.signOut = signOut;
|
|
898
|
+
exports.signUpWithEmail = signUpWithEmail;
|
|
899
|
+
exports.storage = storage;
|
|
900
|
+
exports.subscribeToAuthState = subscribeToAuthState;
|
|
901
|
+
exports.subscribeToCollection = subscribeToCollection;
|
|
902
|
+
exports.subscribeToDocument = subscribeToDocument;
|
|
903
|
+
exports.trackEvent = trackEvent;
|
|
904
|
+
exports.trackLogin = trackLogin;
|
|
905
|
+
exports.trackPageView = trackPageView;
|
|
906
|
+
exports.trackSignUp = trackSignUp;
|
|
907
|
+
exports.trackUserAction = trackUserAction;
|
|
908
|
+
exports.updateDocument = updateDocument;
|
|
909
|
+
exports.updateFileMetadata = updateFileMetadata;
|
|
910
|
+
exports.updateUserProfile = updateUserProfile;
|
|
911
|
+
exports.uploadFile = uploadFile;
|
|
912
|
+
exports.uploadFileWithProgress = uploadFileWithProgress;
|
|
913
|
+
exports.useAuth = useAuth;
|
|
914
|
+
exports.useAuthContext = useAuthContext;
|
|
915
|
+
exports.useCollection = useCollection;
|
|
916
|
+
exports.useDocument = useDocument;
|
|
917
|
+
exports.useFirestoreMutation = useFirestoreMutation;
|
|
918
|
+
exports.useStorage = useStorage;
|
|
919
|
+
exports.waitForAuthState = waitForAuthState;
|
|
920
|
+
exports.withAuth = withAuth;
|
|
921
|
+
//# sourceMappingURL=index.js.map
|
|
922
|
+
//# sourceMappingURL=index.js.map
|