11import { initializeApp } from 'firebase/app'
22import { initializeAnalytics , logEvent } from 'firebase/analytics'
3- import { equalTo , getDatabase , orderByChild , query , ref , startAfter } from 'firebase/database'
3+ import { connectDatabaseEmulator , equalTo , getDatabase , orderByChild , query , ref , startAfter } from 'firebase/database'
4+ import { connectAuthEmulator , getAuth } from 'firebase/auth'
5+ import { isDefined } from '@togglecorp/fujs'
46
57const firebaseConfig = {
68 apiKey : import . meta. env . VITE_FIREBASE_API_KEY ,
@@ -25,6 +27,27 @@ export const logAnalyticsEvent = (eventName, eventParams = {}) => {
2527// used for the database refs
2628export const db = getDatabase ( firebaseApp )
2729
30+ const shouldUseEmulator = isDefined ( import . meta. env . VITE_FIREBASE_DB_EMULATOR_HOST )
31+ && isDefined ( import . meta. env . VITE_FIREBASE_DB_EMULATOR_PORT ) ;
32+
33+ if ( shouldUseEmulator ) {
34+ connectDatabaseEmulator (
35+ db ,
36+ import . meta. env . VITE_FIREBASE_DB_EMULATOR_HOST ,
37+ import . meta. env . VITE_FIREBASE_DB_EMULATOR_PORT ,
38+ ) ;
39+ }
40+
41+ export function getFirebaseAuth ( ) {
42+ const auth = getAuth ( firebaseApp ) ;
43+
44+ if ( shouldUseEmulator && isDefined ( import . meta. env . VITE_FIREBASE_AUTH_EMULATOR_URL ) ) {
45+ connectAuthEmulator ( auth , import . meta. env . VITE_FIREBASE_AUTH_EMULATOR_URL ) ;
46+ }
47+
48+ return auth ;
49+ }
50+
2851// export reusable database references
2952export const dbRef = ref ( db )
3053export const getProjectRef = ( projectId : string ) => ref ( db , `v2/projects/${ projectId } ` )
0 commit comments