2021-12-09 22:05:55 +00:00
import { getFirestore } from '@firebase/firestore'
2022-01-21 23:21:46 +00:00
import { initializeApp , getApps , getApp } from 'firebase/app'
2022-07-14 16:44:52 +00:00
import { getStorage } from 'firebase/storage'
2022-05-09 13:04:36 +00:00
import { FIREBASE_CONFIG } from 'common/envs/constants'
2022-04-25 15:46:35 +00:00
import { connectFirestoreEmulator } from 'firebase/firestore'
import { connectFunctionsEmulator , getFunctions } from 'firebase/functions'
2021-12-24 21:06:01 +00:00
2021-12-08 16:42:19 +00:00
// Initialize Firebase
2022-03-09 02:43:30 +00:00
export const app = getApps ( ) . length ? getApp ( ) : initializeApp ( FIREBASE_CONFIG )
2022-04-25 15:46:35 +00:00
export const db = getFirestore ( )
export const functions = getFunctions ( )
2022-07-14 16:44:52 +00:00
export const storage = getStorage ( )
2022-01-21 23:21:46 +00:00
2022-05-26 21:41:24 +00:00
declare global {
/* eslint-disable-next-line no-var */
var EMULATORS_STARTED : boolean
}
2022-04-25 15:46:35 +00:00
function startEmulators() {
// I don't like this but this is the only way to reconnect to the emulators without error, see: https://stackoverflow.com/questions/65066963/firebase-firestore-emulator-error-host-has-been-set-in-both-settings-and-usee
2022-05-26 21:41:24 +00:00
if ( ! global . EMULATORS_STARTED ) {
global . EMULATORS_STARTED = true
2022-04-25 15:46:35 +00:00
connectFirestoreEmulator ( db , 'localhost' , 8080 )
connectFunctionsEmulator ( functions , 'localhost' , 5001 )
}
}
if ( process . env . NEXT_PUBLIC_FIREBASE_EMULATE ) {
startEmulators ( )
}