p5.js-web-editor/client/modules/User/actions.js

163 lines
4.1 KiB
JavaScript
Raw Normal View History

2016-06-23 22:29:55 +00:00
import { browserHistory } from 'react-router';
import axios from 'axios';
import * as ActionTypes from '../../constants';
import { showErrorModal, justOpenedProject } from '../IDE/actions/ide';
2016-06-20 17:29:32 +00:00
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
2016-06-23 22:29:55 +00:00
export function authError(error) {
return {
type: ActionTypes.AUTH_ERROR,
payload: error
};
}
export function signUpUser(previousPath, formValues) {
2016-06-23 22:29:55 +00:00
return (dispatch) => {
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
2016-06-14 18:46:40 +00:00
});
dispatch(justOpenedProject());
browserHistory.push(previousPath);
})
.catch(response => dispatch(authError(response.data.error)));
2016-06-23 22:29:55 +00:00
};
}
2016-06-14 20:48:16 +00:00
export function loginUser(formValues) {
return axios.post(`${ROOT_URL}/login`, formValues, { withCredentials: true });
}
export function loginUserSuccess(user) {
return {
type: ActionTypes.AUTH_USER,
user
};
}
export function loginUserFailure(error) {
return {
type: ActionTypes.AUTH_ERROR,
error
};
}
export function validateAndLoginUser(previousPath, formProps, dispatch) {
return new Promise((resolve, reject) => {
loginUser(formProps)
.then((response) => {
2016-06-14 20:48:16 +00:00
dispatch({ type: ActionTypes.AUTH_USER,
user: response.data
2016-06-14 20:48:16 +00:00
});
2016-11-04 21:27:39 +00:00
dispatch({
type: ActionTypes.SET_PREFERENCES,
preferences: response.data.preferences
});
dispatch(justOpenedProject());
browserHistory.push(previousPath);
resolve();
2016-06-14 20:48:16 +00:00
})
.catch((response) => {
2016-09-30 02:02:06 +00:00
reject({ password: response.data.message, _error: 'Login failed!' });
});
});
2016-06-14 20:48:16 +00:00
}
export function getUser() {
2016-06-23 22:29:55 +00:00
return (dispatch) => {
axios.get(`${ROOT_URL}/session`, { withCredentials: true })
.then((response) => {
2016-06-23 22:29:55 +00:00
dispatch({
type: ActionTypes.AUTH_USER,
user: response.data
});
dispatch({
type: ActionTypes.SET_PREFERENCES,
preferences: response.data.preferences
});
})
.catch((response) => {
dispatch(authError(response.data.error));
});
};
2016-06-23 22:29:55 +00:00
}
2016-08-28 00:46:20 +00:00
2017-01-17 20:34:32 +00:00
export function validateSession() {
return (dispatch, getState) => {
axios.get(`${ROOT_URL}/session`, { withCredentials: true })
.then((response) => {
2017-01-17 20:34:32 +00:00
const state = getState();
if (state.user.username !== response.data.username) {
dispatch(showErrorModal('staleSession'));
2017-01-17 20:34:32 +00:00
}
})
.catch((response) => {
2017-01-17 20:34:32 +00:00
if (response.status === 404) {
dispatch(showErrorModal('staleSession'));
2017-01-17 20:34:32 +00:00
}
});
2017-01-18 21:50:02 +00:00
};
2017-01-17 20:34:32 +00:00
}
2016-08-28 00:46:20 +00:00
export function logoutUser() {
return (dispatch) => {
axios.get(`${ROOT_URL}/logout`, { withCredentials: true })
.then(() => {
dispatch({
type: ActionTypes.UNAUTH_USER
});
})
.catch(response => dispatch(authError(response.data.error)));
};
}
export function initiateResetPassword(formValues) {
2016-10-12 18:25:24 +00:00
return (dispatch) => {
2016-10-19 15:34:08 +00:00
dispatch({
type: ActionTypes.RESET_PASSWORD_INITIATE
});
axios.post(`${ROOT_URL}/reset-password`, formValues, { withCredentials: true })
.then(() => {
2016-10-19 15:34:08 +00:00
// do nothing
2016-10-12 18:25:24 +00:00
})
.catch(response => dispatch({
type: ActionTypes.ERROR,
message: response.data
}));
};
2016-10-12 18:25:24 +00:00
}
export function resetPasswordReset() {
return {
type: ActionTypes.RESET_PASSWORD_RESET
};
}
export function validateResetPasswordToken(token) {
return (dispatch) => {
axios.get(`${ROOT_URL}/reset-password/${token}`)
.then(() => {
// do nothing if the token is valid
})
.catch(() => dispatch({
type: ActionTypes.INVALID_RESET_PASSWORD_TOKEN
}));
};
}
export function updatePassword(token, formValues) {
return (dispatch) => {
axios.post(`${ROOT_URL}/reset-password/${token}`, formValues)
.then((response) => {
dispatch(loginUserSuccess(response.data));
browserHistory.push('/');
})
.catch(() => dispatch({
type: ActionTypes.INVALID_RESET_PASSWORD_TOKEN
}));
};
}