return (dispatch) => { // Set up a firebase auth state listener to get the currently logged in user (this will succeed) // if the user is logged in and a session token exists, otherwise we'll have to look for the // result of the oauth redirect) const unsubscribe = firebase.auth().onAuthStateChanged(authUser => { unsubscribe() if (authUser) { return dispatch(handleSuccesfulRedirectAsync(authUser)) } else { // Otherwise, get the result of the redirect firebase.auth().getRedirectResult().then(result => { dispatch(handleRedirectResultAsync(result)) }).catch(err => { dispatch(signInFailure()) // Depending on the error, we'll need to dispatch different messages if (err.code === 'auth/account-exists-with-different-credential') { const { email } = err dispatch(notificationActions.addNotification({ message: `An account has already been registered for ${email}`, kind: 'danger', dismissAfter: 4000, })) } }) } }) }
.then(() => { // Load user Data return firebase.database().ref().child(`users/${userId}`).once('value', snapshot => { const user = util.recordFromSnapshot(snapshot) dispatch(loadUser(user)) }) })
return (dispatch, getState) => { const { user } = getState() const { plays = 0 } = user const db = firebase.database().ref() return db.child(`users/${user.key}`).update({ plays: plays + 1, }) }
return (dispatch) => { firebase.database().ref().child(`users/${id}`).once('value', snapshot => { const connectedUser = util.recordFromSnapshot(snapshot) const kind = hadAccessAlready ? modalTypes.DID_GIVE : modalTypes.DID_RECEIVE hadAccessAlready = true dispatch(modalActions.openModal({ kind, data: { connectedUser, unlocked } })) }) }
const unsubscribe = firebase.auth().onAuthStateChanged(authUser => { unsubscribe() if (authUser) { return dispatch(handleSuccesfulRedirectAsync(authUser)) } else { // Otherwise, get the result of the redirect firebase.auth().getRedirectResult().then(result => { dispatch(handleRedirectResultAsync(result)) }).catch(err => { dispatch(signInFailure()) // Depending on the error, we'll need to dispatch different messages if (err.code === 'auth/account-exists-with-different-credential') { const { email } = err dispatch(notificationActions.addNotification({ message: `An account has already been registered for ${email}`, kind: 'danger', dismissAfter: 4000, })) } }) } })
return (dispatch, getState) => { // Set the initialization time, with which to compare connection times for new conenctions initializationTime = new Date().getTime() const { user } = getState() hadAccessAlready = user.hasAccess alreadyUnlocked = getUnlockedTracks(user) // Register the user access listener firebase.database().ref().child(`users/${userId}/hasAccess`).on('value', snapshot => { const { user } = getState() const access = util.recordFromSnapshot(snapshot) // If the access hasn't changed, don't dispatch the action if (user.hasAccess === access) { return } dispatch(updateAccess(access)) }) // Register the user connection listener firebase.database().ref().child(`users/${userId}/connections`).on('child_added', snapshot => { const timestamp = util.recordFromSnapshot(snapshot) const id = snapshot.key // Since all keys on the object trigger this listener on load, ensure that the child_added // timestamp is not earlier than initialization time. const isEarlier = timestamp < initializationTime if (isEarlier) { return } dispatch(addConnection(id, timestamp)) dispatch(connectSuccess()) // Set the from id to ensure that this share is not prematurely counted for // a track unlock if (!hadAccessAlready) { dispatch(setFromId(id)) } // Now, handle unlocking additional tracks and notifying the user what tracks // have been unlocked const { user } = getState() const unlocked = getUnlockedTracks(user) // Only unlock tracks if the user already had access const newlyUnlocked = difference(unlocked, alreadyUnlocked) if (hadAccessAlready) { alreadyUnlocked = unlocked dispatch(unlockTracks(newlyUnlocked)) } P.delay(2000).then(() => { dispatch(displayConnectionNotificationAsync(id, newlyUnlocked)) dispatch(loadGraphDataAsync()) }) }) }