function setupPresence(dispatch, firebase) {
  // exit if database does not exist on firebase instance
  if (!firebase.database || !firebase.database.ServerValue) {
    return
  }
  const ref = firebase.database().ref()
  const {
    config: { presence, sessions },
    authUid
  } = firebase._
  const amOnline = ref.child('.info/connected')
  const onlineRef = ref
    .child(
      typeof presence === 'function'
        ? presence(firebase.auth().currentUser, firebase)
        : presence
    )
    .child(authUid)
  let sessionsRef =
    typeof sessions === 'function'
      ? sessions(firebase.auth().currentUser, firebase)
      : sessions
  if (sessionsRef) {
    sessionsRef = ref.child(sessions)
  }
  amOnline.on('value', (snapShot) => {
    if (!snapShot.val()) return
    // user is online
    if (sessionsRef) {
      // add session and set disconnect
      dispatch({ type: actionTypes.SESSION_START, payload: authUid })
      // add new session to sessions collection
      const session = sessionsRef.push({
        startedAt: firebase.database.ServerValue.TIMESTAMP,
        user: authUid
      })
      // Support versions of react-native-firebase that do not have setPriority
      // on firebase.database.ThenableReference
      if (typeof session.setPriority === 'function') {
        // set authUid as priority for easy sorting
        session.setPriority(authUid)
      }
      session
        .child('endedAt')
        .onDisconnect()
        .set(firebase.database.ServerValue.TIMESTAMP, () => {
          dispatch({ type: actionTypes.SESSION_END })
        })
    }
    // add correct session id to user
    // remove from presence list
    onlineRef.set(true)
    onlineRef.onDisconnect().remove()
  })
}
Start presence management for a specificed user uid.
Presence collection contains a list of users that are online currently.
Sessions collection contains a record of all user sessions.
This function is called within login functions if enablePresence: true.
@param {Function} dispatch - Action dispatch function
@param {object} firebase - Internal firebase object
@private