import * as firebase from "firebase/app" ; import "firebase/auth" ; import "firebase/firestore" ; import "firebase/database" ; import { FirebaseCollections , dataConverter } from "../sharedTypes" ; import * as log4js from "log4js" ; const logger = log4js . getLogger ( `firebase` ) ; const config = { apiKey : "AIzaSyCNZOxJQX8x5-z2iWyMLCkKZ6sQjkaGZR8" , authDomain : "disco-cube.firebaseapp.com" , databaseURL : "https://disco-cube.firebaseio.com" , projectId : "disco-cube" , storageBucket : "disco-cube.appspot.com" , messagingSenderId : "239577858848" , appId : "1:239577858848:web:f84d626c0604a452698d45" , measurementId : "G-D9SX9GEWJY" , } ; export const initFirebase = ( ) => firebase . initializeApp ( config ) ; export const signInToFirebase = ( email , password ) => { logger . info ( `signing into firebase` , { email } ) ; return firebase . auth ( ) . signInWithEmailAndPassword ( email , password ) ; } ; export const signOutOfFirebase = ( ) => firebase . auth ( ) . signOut ( ) ; export const listenForFirebaseAuthStateChange = ( handler ) => firebase . auth ( ) . onAuthStateChanged ( handler ) ; export const listenForFirebaseSnapshots = < T extends keyof FirebaseCollections > ( collection , handler ) => { const currentUser = firebase . auth ( ) . currentUser ; if ( ! currentUser ) throw new Error ( `user must be authenticated` ) ; firebase . firestore ( ) . collection ( collection ) . withConverter ( dataConverter ) . doc ( currentUser . uid ) . onSnapshot ( ( x ) => handler ( x . data ( ) as any ) ) ; } ; export const setFirebaseState = < T extends keyof FirebaseCollections > ( collection , state ) => { const currentUser = firebase . auth ( ) . currentUser ; if ( ! currentUser ) throw new Error ( `user must be authenticated` ) ; return firebase . firestore ( ) . collection ( collection ) . withConverter ( dataConverter ) . doc ( currentUser . uid ) . set ( state ) ; } ; export const updateFirebaseState = < T extends keyof FirebaseCollections > ( collection , partial ) => { const currentUser = firebase . auth ( ) . currentUser ; if ( ! currentUser ) throw new Error ( `user must be authenticated` ) ; return firebase . firestore ( ) . collection ( collection ) . withConverter ( dataConverter ) . doc ( currentUser . uid ) . update ( partial ) ; } ; export const startReportingPresenceToFirebase = ( ) => { logger . debug ( `starting to report presenese` ) ; const currentUser = firebase . auth ( ) . currentUser ; if ( ! currentUser ) throw new Error ( `user must be authenticated` ) ; var uid = currentUser . uid ; var userStatusDatabaseRef = firebase . database ( ) . ref ( "/status/" + uid ) ; var isOfflineForDatabase = { status : "offline" , statusChangedAt : firebase . database . ServerValue . TIMESTAMP , } ; var isOnlineForDatabase = { status : "online" , statusChangedAt : firebase . database . ServerValue . TIMESTAMP , } ; firebase . database ( ) . ref ( ".info/connected" ) . on ( "value" , function ( snapshot ) { logger . debug ( `database connected` , snapshot . val ( ) ) ; if ( snapshot . val ( ) == false ) { return ; } userStatusDatabaseRef . onDisconnect ( ) . set ( isOfflineForDatabase ) . then ( function ( ) { logger . debug ( `fully online` ) ; userStatusDatabaseRef . set ( isOnlineForDatabase ) ; } ) ; } ) ; } ;