-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathfirebase.ts
55 lines (49 loc) · 1.58 KB
/
firebase.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { FirebaseApp, initializeApp } from 'firebase/app';
import { getAuth, Auth, connectAuthEmulator } from 'firebase/auth';
import { connectFirestoreEmulator, getFirestore } from "firebase/firestore";
import { connectStorageEmulator, getStorage } from "firebase/storage";
let firebaseApp: FirebaseApp;
const useEmulator = () => import.meta.env.VITE_USE_FIREBASE_EMULATOR;
export const setupFirebase = () => {
try {
firebaseApp = initializeApp({
apiKey: import.meta.env.VITE_FIREBASE_APIKEY,
authDomain: import.meta.env.VITE_FIREBASE_AUTHDOMAIN,
databaseURL: import.meta.env.VITE_FIREBASE_DATABASEURL,
projectId: import.meta.env.VITE_FIREBASE_PROJECTID,
storageBucket: import.meta.env.VITE_FIREBASE_STORAGEBUCKET,
messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGINGSENDERID,
appId: import.meta.env.VITE_FIREBASE_APPID,
});
} catch (error) {
console.error({error})
}
};
let auth: Auth;
let firestore: ReturnType<typeof getFirestore>;
let storage: ReturnType<typeof getStorage>;
export const useAuth = () => {
auth = getAuth(firebaseApp);
if (useEmulator()) {
connectAuthEmulator(auth, 'http://localhost:9099');
}
return auth;
};
export const useFirestore = () => {
if (!firestore) {
firestore = getFirestore();
if (useEmulator()) {
connectFirestoreEmulator(firestore, 'localhost', 8080);
}
}
return firestore;
};
export const useStorage = () => {
if (!storage) {
storage = getStorage();
if (useEmulator()) {
connectStorageEmulator(storage, 'localhost', 9199);
}
}
return storage;
};