@@ -4,7 +4,8 @@ import { populate } from '../helpers'
44import {
55 getLoginMethodAndParams ,
66 updateProfileOnRTDB ,
7- updateProfileOnFirestore
7+ updateProfileOnFirestore ,
8+ setupPresence
89} from '../utils/auth'
910import { promisesForPopulate , getPopulateObjs } from '../utils/populate'
1011
@@ -293,68 +294,6 @@ export const createUserProfile = (dispatch, firebase, userData, profile) => {
293294 } )
294295}
295296
296- /**
297- * @description Start presence management for a specificed user uid.
298- * Presence collection contains a list of users that are online currently.
299- * Sessions collection contains a record of all user sessions.
300- * This function is called within login functions if enablePresence: true.
301- * @param {Function } dispatch - Action dispatch function
302- * @param {Object } firebase - Internal firebase object
303- * @return {Promise }
304- * @private
305- */
306- const setupPresence = ( dispatch , firebase ) => {
307- // exit if database does not exist on firebase instance
308- if ( ! firebase . database || ! firebase . database . ServerValue ) {
309- return
310- }
311- const ref = firebase . database ( ) . ref ( )
312- const { config : { presence, sessions } , authUid } = firebase . _
313- const amOnline = ref . child ( '.info/connected' )
314- const onlineRef = ref
315- . child (
316- isFunction ( presence )
317- ? presence ( firebase . auth ( ) . currentUser , firebase )
318- : presence
319- )
320- . child ( authUid )
321- let sessionsRef = isFunction ( sessions )
322- ? sessions ( firebase . auth ( ) . currentUser , firebase )
323- : sessions
324- if ( sessionsRef ) {
325- sessionsRef = ref . child ( sessions )
326- }
327- amOnline . on ( 'value' , snapShot => {
328- if ( ! snapShot . val ( ) ) return
329- // user is online
330- if ( sessionsRef ) {
331- // add session and set disconnect
332- dispatch ( { type : actionTypes . SESSION_START , payload : authUid } )
333- // add new session to sessions collection
334- const session = sessionsRef . push ( {
335- startedAt : firebase . database . ServerValue . TIMESTAMP ,
336- user : authUid
337- } )
338- // Support versions of react-native-firebase that do not have setPriority
339- // on firebase.database.ThenableReference
340- if ( isFunction ( session . setPriority ) ) {
341- // set authUid as priority for easy sorting
342- session . setPriority ( authUid )
343- }
344- session
345- . child ( 'endedAt' )
346- . onDisconnect ( )
347- . set ( firebase . database . ServerValue . TIMESTAMP , ( ) => {
348- dispatch ( { type : actionTypes . SESSION_END } )
349- } )
350- }
351- // add correct session id to user
352- // remove from presence list
353- onlineRef . set ( true )
354- onlineRef . onDisconnect ( ) . remove ( )
355- } )
356- }
357-
358297/**
359298 * Auth state change handler. Handles response from firebase's onAuthStateChanged
360299 * @param {Function } dispatch - Action dispatch function
0 commit comments