-
-
Save maccman/ef20fcff626f867a41782a4e652d7832 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {firebase, firebaseAuth, firebaseStorage, firestore} from './firebase-packages' | |
const firebaseAuthJson = process.env.NEXT_PUBLIC_FIREBASE_AUTH_JSON | |
const isTest = process.env.NODE_ENV === 'test' | |
if (!firebaseAuthJson) { | |
throw new Error('missing NEXT_PUBLIC_FIREBASE_AUTH_JSON env var') | |
} | |
const firebaseAuthConfig = JSON.parse(firebaseAuthJson) | |
const authEmulatorHost = process.env.NEXT_PUBLIC_FIREBASE_AUTH_EMULATOR_HOST | |
const firestoreEmulatorHost = process.env.NEXT_PUBLIC_FIRESTORE_EMULATOR_HOST | |
export const config = { | |
...firebaseAuthConfig, | |
...(isTest ? {projectId: 'demo'} : {}), | |
// Custom config | |
clientID: 'TEST123', | |
appleScopes: ['email', 'name'], | |
googleScopes: ['email', 'profile'], | |
} | |
const apps = firebase.getApps() | |
const app = apps[0] ?? firebase.initializeApp(config) | |
// Should we enable things like analytics/remote config which require a browser env | |
const browser = typeof window != 'undefined' && !!window.indexedDB | |
// Setup auth | |
export const auth = firebaseAuth.getAuth(app) | |
export type {User as AuthUser} from '@firebase/auth' | |
// Setup storage | |
export const storage = firebaseStorage.getStorage(app) | |
// Do not retry uploads | |
storage.maxUploadRetryTime = 0 | |
// Setup firestore | |
// firestore.setLogLevel('debug') | |
export const db = firestore.initializeFirestore(app, { | |
...(browser && {cacheSizeBytes: firestore.CACHE_SIZE_UNLIMITED}), | |
}) | |
if (isTest) { | |
if (!authEmulatorHost || authEmulatorHost == '') { | |
throw new Error('Must use auth emulator for tests') | |
} | |
if (!firestoreEmulatorHost || firestoreEmulatorHost == '') { | |
throw new Error('Must use firestore emulator for tests') | |
} | |
} | |
if (authEmulatorHost && authEmulatorHost != '') { | |
firebaseAuth.connectAuthEmulator(auth, `http://${authEmulatorHost}`, { | |
disableWarnings: true, | |
}) | |
} | |
if (firestoreEmulatorHost && firestoreEmulatorHost != '') { | |
const [host, port] = firestoreEmulatorHost.split(':') | |
firestore.connectFirestoreEmulator(db, host, Number(port)) | |
} | |
// Only enable persistence client-side | |
if (browser) { | |
firebaseAuth.setPersistence(auth, firebaseAuth.indexedDBLocalPersistence) | |
firestore.enableMultiTabIndexedDbPersistence(db) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment