From 580c67c9f4c506bcc3bb0b73cbeefd4b53e8487d Mon Sep 17 00:00:00 2001 From: Sidney Alcantara Date: Mon, 2 May 2022 12:43:55 +1000 Subject: [PATCH] connect emulators based on env vars --- src/firebase/index.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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();