|
| 1 | +import { Actions } from 'react-native-router-flux'; |
| 2 | +import API from 'src/services/api'; |
| 3 | +import DebugConfig from 'src/config/debug'; |
| 4 | +import FixtureAPI from 'src/services/fixtureApi'; |
| 5 | + |
| 6 | +import { |
| 7 | + LOGIN_USER_SUCCESS, |
| 8 | + LOGIN_USER_FAILURE, |
| 9 | + FETCH_USER_FAILURE, |
| 10 | + SIGNUP_USER_FAILURE, |
| 11 | + SIGNOUT_USER_SUCCESS, |
| 12 | +} from 'src/state/types'; |
| 13 | + |
| 14 | + |
| 15 | +const api = DebugConfig.useFixtures ? FixtureAPI : API.create(); |
| 16 | + |
| 17 | +const responseFailure = (dispatch, type, response) => { |
| 18 | + let message = 'An error occured! Please try again.'; |
| 19 | + if (response.problem === 'TIMEOUT_ERROR') { |
| 20 | + message = 'A timeout error occured! Check your internet connection and then try again.'; |
| 21 | + } else if (response.problem === 'NETWORK_ERROR') { |
| 22 | + message = 'A network error occured! Check your internet connection and then try again.'; |
| 23 | + } else if (response.problem === 'CONNECTION_ERROR') { |
| 24 | + message = 'A connection error occured! Service is currently unavailable. Please try again later.'; |
| 25 | + } else if (response.data) { |
| 26 | + dispatch({ type, payload: response.data }); |
| 27 | + } |
| 28 | + |
| 29 | + dispatch({ type, payload: { message } }); |
| 30 | +}; |
| 31 | + |
| 32 | +export const isUserAuthenticated = (dispatch, token) => { |
| 33 | + api |
| 34 | + .validateToken(token) |
| 35 | + .then((res) => { |
| 36 | + if (res.ok) { |
| 37 | + fetchUserInfo(dispatch, token); |
| 38 | + } else { |
| 39 | + const payload = {}; |
| 40 | + dispatch({ type: FETCH_USER_FAILURE, payload }); |
| 41 | + } |
| 42 | + }); |
| 43 | +}; |
| 44 | + |
| 45 | +export const signUpUser = (dispatch, data) => { |
| 46 | + const { |
| 47 | + email, username, firstName, lastName, password, |
| 48 | + } = data; |
| 49 | + |
| 50 | + api |
| 51 | + .signUpUser(email, username, firstName, lastName, password) |
| 52 | + .then((res) => { |
| 53 | + if (res.status === 201) { |
| 54 | + signInUser(dispatch, email, password); |
| 55 | + } else { |
| 56 | + responseFailure(dispatch, SIGNUP_USER_FAILURE, res); |
| 57 | + } |
| 58 | + }) |
| 59 | + .catch((error) => { |
| 60 | + dispatch({ type: SIGNUP_USER_FAILURE, payload: error }); |
| 61 | + }); |
| 62 | +}; |
| 63 | + |
| 64 | +export const signInUser = (dispatch, email, password) => { |
| 65 | + api |
| 66 | + .loginUser(email, password) |
| 67 | + .then((res) => { |
| 68 | + if (res.status === 200) { |
| 69 | + fetchUserInfo(dispatch, res.data.token); |
| 70 | + } else { |
| 71 | + responseFailure(dispatch, LOGIN_USER_FAILURE, res); |
| 72 | + } |
| 73 | + }); |
| 74 | +}; |
| 75 | + |
| 76 | +export const fetchUserInfo = (dispatch, token) => { |
| 77 | + api.setHeader('Authorization', `Bearer ${token}`); |
| 78 | + api |
| 79 | + .getUser('edit') |
| 80 | + .then((res) => { |
| 81 | + if (res.status === 200) { |
| 82 | + dispatch({ type: LOGIN_USER_SUCCESS, payload: { token, user: res.data } }); |
| 83 | + Actions.drawer({ type: 'reset' }); |
| 84 | + } else { |
| 85 | + responseFailure(dispatch, FETCH_USER_FAILURE, res); |
| 86 | + } |
| 87 | + }); |
| 88 | +}; |
| 89 | + |
| 90 | +export const signOutUser = (dispatch) => { |
| 91 | + // delete the token |
| 92 | + |
| 93 | + dispatch({ type: SIGNOUT_USER_SUCCESS }); |
| 94 | + Actions.auth({ type: 'reset' }); |
| 95 | +}; |
0 commit comments