Created
August 2, 2019 16:48
-
-
Save ldco2016/401251d7afce1d2132b53450ebe6747a to your computer and use it in GitHub Desktop.
login Actions file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Alert} from 'react-native'; | |
import logger from 'logger'; | |
import * as types from 'auth/constants'; | |
import * as endpoints from 'endpoints'; | |
import * as cache from 'utils/cache'; | |
import {AUTH_STATUS} from 'enums/authStatus'; | |
import * as requester from 'services/Requester'; | |
import {clearSurveys} from 'surveys-ballots/actions'; | |
import {clearEvents} from 'events/actions'; | |
import * as repsActions from 'representatives/actions'; | |
import {clearRegistration} from 'auth/registrationActions'; | |
import {clearFeed} from 'activity-feed/actions'; | |
import {clearActionAlerts} from 'action-alerts/actions'; | |
import * as dataHelpers from 'auth/helpers/data-helpers'; | |
import * as helpers from 'auth/helpers/actions-helpers'; | |
import {verificationEmailChanged} from 'auth/registrationActions'; | |
import {clearMembershipFields} from 'membership/actions'; | |
import { | |
setRelevantStates, | |
clearPreferences, | |
} from 'account-settings/preferencesActions'; | |
import {fetchPrefences, fetchTopics} from 'account-settings/preferencesActions'; | |
import * as appcenter from 'utils/appcenterLogger'; | |
export function setAuthStatus(authStatus) { | |
return async (dispatch, getState) => { | |
try { | |
const {auth: {user}} = getState(); | |
dispatch({ | |
type: types.SET_AUTH_STATUS, | |
payload: { | |
...user, | |
authStatus, | |
}, | |
}); | |
logger(types.SET_AUTH_STATUS); | |
} catch (error) { | |
logger('error in setAuthStatus'); | |
return false; | |
} | |
}; | |
} | |
export function handleLogin({email, password, rememberEmail}) { | |
return async dispatch => { | |
try { | |
dispatch({type: types.LOGIN_STARTED}); | |
logger(types.LOGIN_STARTED); | |
const response = await requester.sendPost(endpoints.USER_LOGIN, { | |
Username: email, | |
Password: password, | |
}); | |
const needsSetup = dataHelpers.needsSetup(response); | |
if (needsSetup) { | |
dispatch({ | |
type: types.EMAIL_VERIFICATION_SUBMIT_ENDED_SUCCESS, | |
payload: response, | |
}); | |
dispatch(verificationEmailChanged(email)); | |
return Promise.resolve({ | |
needsSetup: true, | |
}); | |
} | |
const user = { | |
...response, | |
authStatus: AUTH_STATUS.LOGGED_IN, | |
}; | |
cache.saveUserRef(user); | |
cache.handleEmailCache(email, rememberEmail); | |
helpers.sendOneSignalTags(user, true); | |
appcenter.trackUser('UserLogin', user, true); | |
dispatch({ | |
type: types.LOGIN_SUCCESS, | |
payload: user, | |
}); | |
const deepLinkedEvent = await cache.getDeepLinkedElement('event'); | |
const deepLinkedBallot = await cache.getDeepLinkedElement('survey'); | |
dispatchAfterLoginActions(dispatch, user); | |
logger(types.LOGIN_SUCCESS); | |
return Promise.resolve({ | |
needsSetup: false, | |
deepLinkedEvent, | |
deepLinkedBallot, | |
}); | |
} catch (e) { | |
logger(types.LOGIN_ERROR, e); | |
const error = e && e.Message; | |
dispatch({type: types.LOGIN_ERROR, payload: error}); | |
} | |
}; | |
} | |
export function checkSession(responseError) { | |
return (dispatch, getState) => { | |
const {auth: {user}} = getState(); | |
if ( | |
user.authStatus === AUTH_STATUS.LOGGED_IN && | |
responseError && | |
responseError.Message && | |
(responseError.Message.lastIndexOf('TokenExpired') >= 0 || | |
responseError.Message === 'Individual not found') | |
) { | |
dispatch(handleLogout()); | |
return Alert.alert( | |
'Session Timeout', | |
'For your security, your NFIB session has timed out due to inactivity.', | |
[{text: 'Ok', onPress: () => false}] | |
); | |
} | |
}; | |
} | |
export function handleLogout() { | |
return async (dispatch, getState) => { | |
const {auth: {user}} = getState(); | |
dispatch(clearSurveys()); | |
dispatch(clearEvents()); | |
dispatch(clearRegistration()); | |
dispatch(repsActions.clearRepresentatives()); | |
dispatch(clearFeed()); | |
dispatch(clearActionAlerts()); | |
dispatch(clearMembershipFields()); | |
dispatch(clearPreferences()); | |
dispatch({type: types.RESET_LOGIN_STATE}); | |
cache.clearCache(); | |
helpers.sendOneSignalTags(user, false); | |
appcenter.trackUser('UserLogout', user, false); | |
requester.resetTokenExpired(); | |
}; | |
} | |
export function setCurrentUser({Key}) { | |
return async dispatch => { | |
try { | |
dispatch({type: types.FETCH_USER_DETAILS_STARTED}); | |
const user = await helpers.fetchCurrentUserDetails(Key); | |
dispatch({ | |
type: types.SET_CURRENT_USER, | |
payload: { | |
...user, | |
authStatus: AUTH_STATUS.LOGGED_IN, | |
}, | |
}); | |
dispatchAfterLoginActions(dispatch, user); | |
logger(types.SET_CURRENT_USER); | |
} catch (error) { | |
dispatch({ | |
type: types.FETCH_USER_DETAILS_FINISHED_ERROR, | |
payload: error, | |
}); | |
dispatch(checkSession(error)); | |
logger(types.FETCH_USER_DETAILS_FINISHED_ERROR); | |
} | |
}; | |
} | |
function dispatchAfterLoginActions(dispatch, user) { | |
dispatch(fetchUserOrganizationDetails(user)); | |
dispatch(validateUserInformationForVoterVoice()); | |
dispatch(setRelevantStates()); | |
dispatch(fetchPrefences(user)); | |
dispatch(fetchTopics()); | |
} | |
export function setUserMembership(user) { | |
const isMember = Boolean(user.Organization && user.Organization.Key); | |
return { | |
type: types.SET_USER_MEMBERSHIP, | |
payload: isMember, | |
}; | |
} | |
export function fetchUserOrganizationDetails(user) { | |
return async dispatch => { | |
try { | |
if (user.Organization && user.Organization.Key) { | |
const {Key} = user.Organization; | |
const payload = await helpers.fetchOrganizationDetails(Key); | |
dispatch({ | |
type: types.GET_USER_ORGANIZATION_FINISHED_SUCCESS, | |
payload, | |
}); | |
dispatch(setUserMembership(user)); | |
logger(types.GET_USER_ORGANIZATION_FINISHED_SUCCESS); | |
helpers.sendOneSignalOrganizationTags(payload); | |
cache.saveOrganizationData(payload); | |
} | |
} catch (error) { | |
logger(types.GET_USER_ORGANIZATION_FINISHED_ERROR); | |
dispatch({ | |
type: types.GET_USER_ORGANIZATION_FINISHED_ERROR, | |
payload: error, | |
}); | |
dispatch(checkSession(error)); | |
} | |
}; | |
} | |
export function setUserPosition(coords) { | |
return { | |
type: types.SET_USER_POSITION, | |
payload: coords, | |
}; | |
} | |
export function setEmailField(email) { | |
return { | |
type: types.SET_EMAIL_FIELD, | |
payload: email, | |
}; | |
} | |
export function setPasswordField(password) { | |
return { | |
type: types.SET_PASSWORD_FIELD, | |
payload: password, | |
}; | |
} | |
export function setSecurityTextEntry(current) { | |
return { | |
type: types.SET_SECURITY_TEXT_ENTRY, | |
payload: current, | |
}; | |
} | |
export function setLoginDisabled(status) { | |
return { | |
type: types.SET_LOGIN_DISABLED, | |
payload: status, | |
}; | |
} | |
export function validateUserAddress() { | |
return async (dispatch, getState) => { | |
try { | |
logger(types.VALIDATE_USER_ADDRESS_STARTED); | |
dispatch({type: types.VALIDATE_USER_ADDRESS_STARTED}); | |
const {auth: {user: {PersonalAddress}}} = getState(); | |
const payload = await helpers.validateAddress({ | |
homeAddress: PersonalAddress.AddressLine1, | |
city: PersonalAddress.City, | |
state: PersonalAddress.StateCode, | |
zipCode: PersonalAddress.PostalCode, | |
}); | |
dispatch({type: types.VALIDATE_USER_ADDRESS_SUCCESS, payload}); | |
logger(types.VALIDATE_USER_ADDRESS_SUCCESS); | |
} catch (error) { | |
logger(types.VALIDATE_USER_ADDRESS_ERROR); | |
dispatch({ | |
type: types.VALIDATE_USER_ADDRESS_ERROR, | |
payload: error, | |
}); | |
dispatch(checkSession(error)); | |
} | |
}; | |
} | |
export function validateUserInformationForVoterVoice(userKey = null) { | |
return async (dispatch, getState) => { | |
try { | |
logger(types.VALIDATE_USER_INFORMATION_FOR_VOTER_VOICE_STARTED); | |
dispatch({ | |
type: types.VALIDATE_USER_INFORMATION_FOR_VOTER_VOICE_STARTED, | |
}); | |
const {auth: {user: {Key}}} = getState(); | |
const payload = await helpers.validateUserInformationForVoterVoice( | |
userKey || Key | |
); | |
logger(types.VALIDATE_USER_INFORMATION_FOR_VOTER_VOICE_SUCCESS); | |
dispatch({ | |
type: types.VALIDATE_USER_INFORMATION_FOR_VOTER_VOICE_SUCCESS, | |
payload, | |
}); | |
} catch (error) { | |
dispatch({ | |
type: types.VALIDATE_USER_INFORMATION_FOR_VOTER_VOICE_ERROR, | |
payload: error, | |
}); | |
} | |
}; | |
} | |
export function sendVerificationEmail() { | |
return async (dispatch, getState) => { | |
try { | |
logger(types.SEND_EMAIL_CONFIRMATION_STARTED); | |
dispatch({type: types.SEND_EMAIL_CONFIRMATION_STARTED}); | |
const {auth: {user: {Key}}} = getState(); | |
const payload = await helpers.sendVerificationEmail(Key); | |
logger(types.SEND_EMAIL_CONFIRMATION_SUCCESS); | |
dispatch({ | |
type: types.SEND_EMAIL_CONFIRMATION_SUCCESS, | |
payload, | |
}); | |
} catch (error) { | |
dispatch({ | |
type: types.SEND_EMAIL_CONFIRMATION_ERROR, | |
payload: error, | |
}); | |
} | |
}; | |
} | |
export function toggleRememberEmail() { | |
return {type: types.TOGGLE_REMEMBER_EMAIL}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment