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, })) } }) } }) }
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, })) } }) } })