2016-06-23 22:29:55 +00:00
|
|
|
import * as ActionTypes from '../../constants';
|
|
|
|
import { browserHistory } from 'react-router';
|
|
|
|
import axios from 'axios';
|
2016-06-09 20:28:21 +00:00
|
|
|
|
|
|
|
|
2016-06-20 17:29:32 +00:00
|
|
|
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
|
2016-06-09 20:28:21 +00:00
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
export function authError(error) {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.AUTH_ERROR,
|
|
|
|
payload: error
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-09 20:28:21 +00:00
|
|
|
export function signUpUser(formValues) {
|
2016-06-23 22:29:55 +00:00
|
|
|
return (dispatch) => {
|
|
|
|
axios.post(`${ROOT_URL}/signup`, formValues, { withCredentials: true })
|
2016-06-09 20:28:21 +00:00
|
|
|
.then(response => {
|
2016-06-14 18:46:40 +00:00
|
|
|
dispatch({ type: ActionTypes.AUTH_USER,
|
|
|
|
user: response.data
|
|
|
|
});
|
2016-06-09 20:28:21 +00:00
|
|
|
browserHistory.push('/');
|
|
|
|
})
|
|
|
|
.catch(response => dispatch(authError(response.data.error)));
|
2016-06-23 22:29:55 +00:00
|
|
|
};
|
2016-06-09 22:41:40 +00:00
|
|
|
}
|
|
|
|
|
2016-06-14 20:48:16 +00:00
|
|
|
export function loginUser(formValues) {
|
2016-06-23 22:29:55 +00:00
|
|
|
return (dispatch) => {
|
|
|
|
axios.post(`${ROOT_URL}/login`, formValues, { withCredentials: true })
|
2016-06-14 20:48:16 +00:00
|
|
|
.then(response => {
|
|
|
|
dispatch({ type: ActionTypes.AUTH_USER,
|
|
|
|
user: response.data
|
|
|
|
});
|
|
|
|
browserHistory.push('/');
|
|
|
|
})
|
|
|
|
.catch(response => dispatch(authError(response.data.error)));
|
2016-06-23 22:29:55 +00:00
|
|
|
};
|
2016-06-14 20:48:16 +00:00
|
|
|
}
|
2016-06-09 22:41:40 +00:00
|
|
|
|
2016-06-14 23:11:42 +00:00
|
|
|
export function getUser() {
|
2016-06-23 22:29:55 +00:00
|
|
|
return (dispatch) => {
|
|
|
|
axios.get(`${ROOT_URL}/session`, { withCredentials: true })
|
2016-06-14 23:11:42 +00:00
|
|
|
.then(response => {
|
2016-06-23 22:29:55 +00:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.AUTH_USER,
|
2016-06-14 23:11:42 +00:00
|
|
|
user: response.data
|
|
|
|
});
|
2016-08-09 21:50:45 +00:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.SET_PREFERENCES,
|
|
|
|
preferences: response.data.preferences
|
|
|
|
});
|
2016-06-14 23:11:42 +00:00
|
|
|
})
|
2016-08-09 22:45:59 +00:00
|
|
|
.catch(response => {
|
|
|
|
dispatch(authError(response.data.error));
|
|
|
|
});
|
2016-06-09 22:41:40 +00:00
|
|
|
};
|
2016-06-23 22:29:55 +00:00
|
|
|
}
|