p5.js-web-editor/shared/redux/actions/user.js

51 lines
1.4 KiB
JavaScript
Raw Normal View History

import * as ActionTypes from '../constants/constants'
import { browserHistory } from 'react-router'
import axios from 'axios'
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000' : '/';
export function signUpUser(formValues) {
return function(dispatch) {
2016-06-13 23:29:33 +00:00
axios.post(`${ROOT_URL}/signup`, formValues, {withCredentials: true})
.then(response => {
2016-06-14 18:46:40 +00:00
dispatch({ type: ActionTypes.AUTH_USER,
user: response.data
});
browserHistory.push('/');
})
.catch(response => dispatch(authError(response.data.error)));
}
}
2016-06-14 20:48:16 +00: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)));
}
}
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)));
}
}
export function authError(error) {
return {
type: ActionTypes.AUTH_ERROR,
payload: error
};
}