diff --git a/src/firebase/index.ts b/src/firebase/index.ts index 813505dec..969dbc2ca 100644 --- a/src/firebase/index.ts +++ b/src/firebase/index.ts @@ -8,14 +8,27 @@ import appConfig from "./config"; firebase.initializeApp(appConfig); +// Connect emulators based on env vars +const envConnectEmulators = + process.env.NODE_ENV === "test" || + process.env.REACT_APP_FIREBASE_EMULATOR === "true"; + export const auth = firebase.auth(); +if (envConnectEmulators && !(window as any).firebaseAuthEmulatorStarted) { + auth.useEmulator("http://localhost:9099"); + (window as any).firebaseAuthEmulatorStarted = true; +} export const db = firebase.firestore(); db.settings({ cacheSizeBytes: firebase.firestore.CACHE_SIZE_UNLIMITED, ignoreUndefinedProperties: true, }); -db.enablePersistence({ synchronizeTabs: true }); +if (!(window as any).firebaseDbStarted) { + if (envConnectEmulators) db.useEmulator("localhost", 9299); + else db.enablePersistence({ synchronizeTabs: true }); + (window as any).firebaseDbStarted = true; +} export const bucket = firebase.storage(); export const functions = firebase.functions();