2016-06-24 00:29:55 +02:00
|
|
|
import { browserHistory } from 'react-router';
|
|
|
|
import axios from 'axios';
|
2017-02-22 20:29:35 +01:00
|
|
|
import * as ActionTypes from '../../constants';
|
2017-01-24 21:29:25 +01:00
|
|
|
import { showErrorModal, justOpenedProject } from '../IDE/actions/ide';
|
2019-04-17 21:30:50 +02:00
|
|
|
import { showToast, setToastText } from '../IDE/actions/toast';
|
|
|
|
|
2016-06-09 22:28:21 +02:00
|
|
|
|
2018-08-24 23:41:23 +02:00
|
|
|
const __process = (typeof global !== 'undefined' ? global : window).process;
|
|
|
|
const ROOT_URL = __process.env.API_URL;
|
2016-06-09 22:28:21 +02:00
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
export function authError(error) {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.AUTH_ERROR,
|
|
|
|
payload: error
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-10 22:13:00 +01:00
|
|
|
export function signUpUser(previousPath, formValues) {
|
2016-06-24 00:29:55 +02:00
|
|
|
return (dispatch) => {
|
|
|
|
axios.post(`${ROOT_URL}/signup`, formValues, { withCredentials: true })
|
2017-02-22 20:29:35 +01:00
|
|
|
.then((response) => {
|
2018-05-05 02:22:39 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.AUTH_USER,
|
2017-02-22 20:29:35 +01:00
|
|
|
user: response.data
|
2016-06-14 20:46:40 +02:00
|
|
|
});
|
2017-01-24 19:04:51 +01:00
|
|
|
dispatch(justOpenedProject());
|
2016-11-10 22:13:00 +01:00
|
|
|
browserHistory.push(previousPath);
|
2016-06-09 22:28:21 +02:00
|
|
|
})
|
|
|
|
.catch(response => dispatch(authError(response.data.error)));
|
2016-06-24 00:29:55 +02:00
|
|
|
};
|
2016-06-10 00:41:40 +02:00
|
|
|
}
|
|
|
|
|
2016-06-14 22:48:16 +02:00
|
|
|
export function loginUser(formValues) {
|
2016-09-02 23:31:07 +02:00
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-10 22:13:00 +01:00
|
|
|
export function validateAndLoginUser(previousPath, formProps, dispatch) {
|
2016-09-02 23:31:07 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
loginUser(formProps)
|
2017-02-22 20:29:35 +01:00
|
|
|
.then((response) => {
|
2018-05-05 02:22:39 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.AUTH_USER,
|
2017-02-22 20:29:35 +01:00
|
|
|
user: response.data
|
2016-06-14 22:48:16 +02:00
|
|
|
});
|
2016-11-04 22:27:39 +01:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.SET_PREFERENCES,
|
|
|
|
preferences: response.data.preferences
|
|
|
|
});
|
2017-01-24 19:04:51 +01:00
|
|
|
dispatch(justOpenedProject());
|
2016-11-10 22:13:00 +01:00
|
|
|
browserHistory.push(previousPath);
|
2016-09-02 23:31:07 +02:00
|
|
|
resolve();
|
2016-06-14 22:48:16 +02:00
|
|
|
})
|
2017-02-22 20:29:35 +01:00
|
|
|
.catch((response) => {
|
2018-05-09 03:35:18 +02:00
|
|
|
reject({ password: response.data.message, _error: 'Login failed!' }); // eslint-disable-line
|
2016-09-02 23:31:07 +02:00
|
|
|
});
|
|
|
|
});
|
2016-06-14 22:48:16 +02:00
|
|
|
}
|
2016-06-10 00:41:40 +02:00
|
|
|
|
2016-06-15 01:11:42 +02:00
|
|
|
export function getUser() {
|
2016-06-24 00:29:55 +02:00
|
|
|
return (dispatch) => {
|
|
|
|
axios.get(`${ROOT_URL}/session`, { withCredentials: true })
|
2017-02-22 20:29:35 +01:00
|
|
|
.then((response) => {
|
2016-06-24 00:29:55 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.AUTH_USER,
|
2016-06-15 01:11:42 +02:00
|
|
|
user: response.data
|
|
|
|
});
|
2016-08-09 23:50:45 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.SET_PREFERENCES,
|
|
|
|
preferences: response.data.preferences
|
|
|
|
});
|
2016-06-15 01:11:42 +02:00
|
|
|
})
|
2017-02-22 20:29:35 +01:00
|
|
|
.catch((response) => {
|
2016-08-10 00:45:59 +02:00
|
|
|
dispatch(authError(response.data.error));
|
|
|
|
});
|
2016-06-10 00:41:40 +02:00
|
|
|
};
|
2016-06-24 00:29:55 +02:00
|
|
|
}
|
2016-08-28 02:46:20 +02:00
|
|
|
|
2017-01-17 21:34:32 +01:00
|
|
|
export function validateSession() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
axios.get(`${ROOT_URL}/session`, { withCredentials: true })
|
2017-02-22 20:29:35 +01:00
|
|
|
.then((response) => {
|
2017-01-17 21:34:32 +01:00
|
|
|
const state = getState();
|
|
|
|
if (state.user.username !== response.data.username) {
|
2017-01-24 21:29:25 +01:00
|
|
|
dispatch(showErrorModal('staleSession'));
|
2017-01-17 21:34:32 +01:00
|
|
|
}
|
|
|
|
})
|
2017-02-22 20:29:35 +01:00
|
|
|
.catch((response) => {
|
2017-01-17 21:34:32 +01:00
|
|
|
if (response.status === 404) {
|
2017-01-24 21:29:25 +01:00
|
|
|
dispatch(showErrorModal('staleSession'));
|
2017-01-17 21:34:32 +01:00
|
|
|
}
|
|
|
|
});
|
2017-01-18 22:50:02 +01:00
|
|
|
};
|
2017-01-17 21:34:32 +01:00
|
|
|
}
|
|
|
|
|
2016-08-28 02:46:20 +02: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)));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-12 23:19:43 +02:00
|
|
|
export function initiateResetPassword(formValues) {
|
2016-10-12 20:25:24 +02:00
|
|
|
return (dispatch) => {
|
2016-10-19 17:34:08 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.RESET_PASSWORD_INITIATE
|
|
|
|
});
|
2016-10-12 23:19:43 +02:00
|
|
|
axios.post(`${ROOT_URL}/reset-password`, formValues, { withCredentials: true })
|
|
|
|
.then(() => {
|
2016-10-19 17:34:08 +02:00
|
|
|
// do nothing
|
2016-10-12 20:25:24 +02:00
|
|
|
})
|
2016-10-12 23:19:43 +02:00
|
|
|
.catch(response => dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
message: response.data
|
|
|
|
}));
|
|
|
|
};
|
2016-10-12 20:25:24 +02:00
|
|
|
}
|
|
|
|
|
2017-06-26 18:48:28 +02:00
|
|
|
export function initiateVerification() {
|
|
|
|
return (dispatch) => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.EMAIL_VERIFICATION_INITIATE
|
|
|
|
});
|
|
|
|
axios.post(`${ROOT_URL}/verify/send`, {}, { withCredentials: true })
|
|
|
|
.then(() => {
|
|
|
|
// do nothing
|
|
|
|
})
|
|
|
|
.catch(response => dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
message: response.data
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function verifyEmailConfirmation(token) {
|
|
|
|
return (dispatch) => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.EMAIL_VERIFICATION_VERIFY,
|
|
|
|
state: 'checking',
|
|
|
|
});
|
|
|
|
return axios.get(`${ROOT_URL}/verify?t=${token}`, {}, { withCredentials: true })
|
|
|
|
.then(response => dispatch({
|
|
|
|
type: ActionTypes.EMAIL_VERIFICATION_VERIFIED,
|
|
|
|
message: response.data,
|
|
|
|
}))
|
|
|
|
.catch(response => dispatch({
|
|
|
|
type: ActionTypes.EMAIL_VERIFICATION_INVALID,
|
|
|
|
message: response.data
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-12 23:19:43 +02:00
|
|
|
export function resetPasswordReset() {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.RESET_PASSWORD_RESET
|
|
|
|
};
|
|
|
|
}
|
2016-10-18 22:07:25 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
}
|
2017-03-16 23:25:12 +01:00
|
|
|
|
|
|
|
export function updateSettingsSuccess(user) {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.SETTINGS_UPDATED,
|
|
|
|
user
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function updateSettings(formValues) {
|
|
|
|
return dispatch =>
|
|
|
|
axios.put(`${ROOT_URL}/account`, formValues, { withCredentials: true })
|
|
|
|
.then((response) => {
|
|
|
|
dispatch(updateSettingsSuccess(response.data));
|
|
|
|
browserHistory.push('/');
|
2019-04-17 21:30:50 +02:00
|
|
|
dispatch(showToast(5500));
|
|
|
|
dispatch(setToastText('Settings saved.'));
|
2017-03-16 23:25:12 +01:00
|
|
|
})
|
2018-05-09 03:35:18 +02:00
|
|
|
.catch(response => Promise.reject(new Error(response.data.error)));
|
2017-03-16 23:25:12 +01:00
|
|
|
}
|
2018-10-16 00:22:56 +02:00
|
|
|
|
2019-05-14 19:50:33 +02:00
|
|
|
export function createApiKeySuccess(user) {
|
2019-05-14 11:25:14 +02:00
|
|
|
return {
|
|
|
|
type: ActionTypes.API_KEY_CREATED,
|
2019-05-14 19:50:33 +02:00
|
|
|
user
|
2019-05-14 11:25:14 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createApiKey(label) {
|
|
|
|
return dispatch =>
|
|
|
|
axios.post(`${ROOT_URL}/account/api-keys`, { label }, { withCredentials: true })
|
|
|
|
.then((response) => {
|
2019-05-14 19:50:33 +02:00
|
|
|
dispatch(createApiKeySuccess(response.data));
|
2019-05-14 11:25:14 +02:00
|
|
|
})
|
|
|
|
.catch(response => Promise.reject(new Error(response.data.error)));
|
2018-10-16 00:22:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function removeApiKey(keyId) {
|
|
|
|
return dispatch =>
|
|
|
|
axios.delete(`${ROOT_URL}/account/api-keys/${keyId}`, { withCredentials: true })
|
|
|
|
.then((response) => {
|
|
|
|
dispatch({
|
2019-05-15 16:39:53 +02:00
|
|
|
type: ActionTypes.API_KEY_REMOVED,
|
2018-10-16 00:22:56 +02:00
|
|
|
user: response.data
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(response => Promise.reject(new Error(response.data.error)));
|
|
|
|
}
|