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

250 lines
6.5 KiB
JavaScript
Raw Normal View History

2016-06-23 22:29:55 +00:00
import { browserHistory } from 'react-router';
import axios from 'axios';
import crypto from 'crypto';
import * as ActionTypes from '../../constants';
import { showErrorModal, justOpenedProject } from '../IDE/actions/ide';
2019-04-17 19:30:50 +00:00
import { showToast, setToastText } from '../IDE/actions/toast';
const __process = (typeof global !== 'undefined' ? global : window).process;
const ROOT_URL = __process.env.API_URL;
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) => {
2018-05-05 00:22:39 +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) => {
2018-05-05 00:22:39 +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) => {
2018-05-09 01:35:18 +00:00
reject({ password: response.data.message, _error: 'Login failed!' }); // eslint-disable-line
});
});
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
}
Email verification (#369) * Re-introduce Email Verification code Revert "Revert "Email verification"" This reverts commit d154d8bff259350523a0f139e844db96c43d2ee1. * Uses MJML to generate Reset Password email * Sends Password Reset and Email Confirmation emails using MJML template * Sends verified status along with user data * API endpoint for resending email verification confirmation * Displays verification status on Account page and allows resending * Send back error string * Passes email address through to sign/verify helper * Uses enum-style object to set verified state * Sends minimal info when user verifies since it can be done without login * Provides /verify UI and sends confirmation token to API * Better name for JWT secret token env var * Adds mail config variables to Readme * Encrypts email address in JWT The JWT sent as the token in the Confirm Password URL can be unencoded by anyone, although it's signature can only be verified by us. To ensure that no passwords are leaked, we encrypt the email address before creating the token. * Removes unused mail templates * Resets verified flag when email is changed and sends another email * Moves email confirmation functions next to each other * Extracts random token generator to helper * Moves email confirmation actions into Redux - updates the AccountForm label with a message to check inbox - show status when verifying email token * Uses generated token stored in DB for email confirmation * Sets email confirmation status to verified if logging in from Github * Sends email using new method on account creation * Fixes linting errors * Removes replyTo config
2017-06-26 16:48:28 +00: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
}));
};
}
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
}));
};
}
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 19:30:50 +00:00
dispatch(showToast(5500));
dispatch(setToastText('Settings saved.'));
})
2018-05-09 01:35:18 +00:00
.catch(response => Promise.reject(new Error(response.data.error)));
}
export function createApiKeySuccess(user) {
return {
type: ActionTypes.API_KEY_CREATED,
user
};
}
export function createApiKey(label) {
return dispatch =>
axios.post(`${ROOT_URL}/account/api-keys`, { label }, { withCredentials: true })
.then((response) => {
dispatch(createApiKeySuccess(response.data));
})
.catch(response => Promise.reject(new Error(response.data.error)));
}
export function removeApiKey(keyId) {
return dispatch =>
axios.delete(`${ROOT_URL}/account/api-keys/${keyId}`, { withCredentials: true })
.then((response) => {
dispatch({
type: ActionTypes.API_KEY_REMOVED,
user: response.data
});
})
.catch(response => Promise.reject(new Error(response.data.error)));
}