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

273 lines
6.7 KiB
JavaScript
Raw Normal View History

2016-06-24 00:29:55 +02:00
import { browserHistory } from 'react-router';
import * as ActionTypes from '../../constants';
import apiClient from '../../utils/apiClient';
import { showErrorModal, justOpenedProject } from '../IDE/actions/ide';
import { setLanguage } from '../IDE/actions/preferences';
2019-04-17 21:30:50 +02:00
import { showToast, setToastText } from '../IDE/actions/toast';
2016-06-24 00:29:55 +02:00
export function authError(error) {
return {
type: ActionTypes.AUTH_ERROR,
payload: error
};
}
export function signUpUser(previousPath, formValues) {
2016-06-24 00:29:55 +02:00
return (dispatch) => {
apiClient.post('/signup', formValues)
.then((response) => {
2018-05-05 02:22:39 +02:00
dispatch({
type: ActionTypes.AUTH_USER,
user: response.data
2016-06-14 20:46:40 +02:00
});
dispatch(justOpenedProject());
browserHistory.push(previousPath);
})
2020-04-25 16:48:39 +02:00
.catch((error) => {
const { response } = error;
dispatch(authError(response.data.error));
});
2016-06-24 00:29:55 +02:00
};
}
2016-06-14 22:48:16 +02:00
export function loginUser(formValues) {
return apiClient.post('/login', formValues);
}
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 02:22:39 +02:00
dispatch({
type: ActionTypes.AUTH_USER,
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
});
setLanguage(response.data.preferences.language, { persistPreference: false });
dispatch(justOpenedProject());
browserHistory.push(previousPath);
resolve();
2016-06-14 22:48:16 +02:00
})
.catch(error =>
reject({ password: error.response.data.message, _error: 'Login failed!' })); // eslint-disable-line
});
2016-06-14 22:48:16 +02:00
}
export function getUser() {
2016-06-24 00:29:55 +02:00
return (dispatch) => {
apiClient.get('/session')
.then((response) => {
2016-06-24 00:29:55 +02:00
dispatch({
type: ActionTypes.AUTH_USER,
user: response.data
});
dispatch({
type: ActionTypes.SET_PREFERENCES,
preferences: response.data.preferences
});
setLanguage(response.data.preferences.language, { persistPreference: false });
}).catch((error) => {
2020-04-25 16:48:39 +02:00
const { response } = error;
const message = response.message || response.data.error;
dispatch(authError(message));
});
};
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) => {
apiClient.get('/session')
.then((response) => {
2017-01-17 21:34:32 +01:00
const state = getState();
if (state.user.username !== response.data.username) {
dispatch(showErrorModal('staleSession'));
2017-01-17 21:34:32 +01:00
}
})
2020-04-25 16:48:39 +02:00
.catch((error) => {
const { response } = error;
2017-01-17 21:34:32 +01:00
if (response.status === 404) {
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) => {
apiClient.get('/logout')
2016-08-28 02:46:20 +02:00
.then(() => {
dispatch({
type: ActionTypes.UNAUTH_USER
});
})
2020-04-25 16:48:39 +02:00
.catch((error) => {
const { response } = error;
dispatch(authError(response.data.error));
});
2016-08-28 02:46:20 +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
});
apiClient.post('/reset-password', formValues)
.then(() => {
2016-10-19 17:34:08 +02:00
// do nothing
2016-10-12 20:25:24 +02:00
})
2020-04-25 16:48:39 +02:00
.catch((error) => {
const { response } = error;
dispatch({
type: ActionTypes.ERROR,
message: response.data
});
});
};
2016-10-12 20:25:24 +02: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 18:48:28 +02:00
export function initiateVerification() {
return (dispatch) => {
dispatch({
type: ActionTypes.EMAIL_VERIFICATION_INITIATE
});
apiClient.post('/verify/send', {})
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 18:48:28 +02:00
.then(() => {
// do nothing
})
2020-04-25 16:48:39 +02:00
.catch((error) => {
const { response } = error;
dispatch({
type: ActionTypes.ERROR,
message: response.data
});
});
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 18:48:28 +02:00
};
}
export function verifyEmailConfirmation(token) {
return (dispatch) => {
dispatch({
type: ActionTypes.EMAIL_VERIFICATION_VERIFY,
state: 'checking',
});
return apiClient.get(`/verify?t=${token}`, {})
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 18:48:28 +02:00
.then(response => dispatch({
type: ActionTypes.EMAIL_VERIFICATION_VERIFIED,
message: response.data,
}))
2020-04-25 16:48:39 +02:00
.catch((error) => {
const { response } = error;
dispatch({
type: ActionTypes.EMAIL_VERIFICATION_INVALID,
message: response.data
});
});
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 18:48:28 +02:00
};
}
export function resetPasswordReset() {
return {
type: ActionTypes.RESET_PASSWORD_RESET
};
}
export function validateResetPasswordToken(token) {
return (dispatch) => {
apiClient.get(`/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) => {
apiClient.post(`/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 =>
apiClient.put('/account', formValues)
.then((response) => {
dispatch(updateSettingsSuccess(response.data));
browserHistory.push('/');
2019-04-17 21:30:50 +02:00
dispatch(showToast(5500));
dispatch(setToastText('Toast.SettingsSaved'));
})
2020-04-25 16:48:39 +02:00
.catch((error) => {
const { response } = error;
Promise.reject(new Error(response.data.error));
});
}
export function createApiKeySuccess(user) {
return {
type: ActionTypes.API_KEY_CREATED,
user
};
}
export function createApiKey(label) {
return dispatch =>
apiClient.post('/account/api-keys', { label })
.then((response) => {
dispatch(createApiKeySuccess(response.data));
})
2020-04-25 16:48:39 +02:00
.catch((error) => {
const { response } = error;
Promise.reject(new Error(response.data.error));
});
}
export function removeApiKey(keyId) {
return dispatch =>
apiClient.delete(`/account/api-keys/${keyId}`)
.then((response) => {
dispatch({
type: ActionTypes.API_KEY_REMOVED,
user: response.data
});
})
2020-04-25 16:48:39 +02:00
.catch((error) => {
const { response } = error;
Promise.reject(new Error(response.data.error));
});
}