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