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