diff --git a/src/components/auth/actions.js b/src/components/auth/actions.js index 70aa9fc..44c7175 100644 --- a/src/components/auth/actions.js +++ b/src/components/auth/actions.js @@ -1,6 +1,6 @@ import { routeActions } from 'react-router-redux'; -import { updateUser, logout as logoutUser, authenticate } from 'components/user/actions'; +import { updateUser, logout as logoutUser, changePassword as changeUserPassword, authenticate } from 'components/user/actions'; import request from 'services/request'; export function login({login = '', password = '', rememberMe = false}) { @@ -32,7 +32,7 @@ export function login({login = '', password = '', rememberMe = false}) { username: login, email: login })); - } else { + } else if (resp.errors) { if (resp.errors.login === LOGIN_REQUIRED && password) { dispatch(logout()); } @@ -46,6 +46,25 @@ export function login({login = '', password = '', rememberMe = false}) { ; } +export function changePassword({ + password = '', + newPassword = '', + newRePassword = '' +}) { + return (dispatch) => + dispatch(changeUserPassword({password, newPassword, newRePassword})) + .catch((resp) => { + if (resp.errors) { + const errorMessage = resp.errors[Object.keys(resp.errors)[0]]; + dispatch(setError(errorMessage)); + throw new Error(errorMessage); + } + + // TODO: log unexpected errors + }) + ; +} + export function register({ email = '', username = '', @@ -67,9 +86,11 @@ export function register({ dispatch(routeActions.push('/activation')); }) .catch((resp) => { - const errorMessage = resp.errors[Object.keys(resp.errors)[0]]; - dispatch(setError(errorMessage)); - throw new Error(errorMessage); + if (resp.errors) { + const errorMessage = resp.errors[Object.keys(resp.errors)[0]]; + dispatch(setError(errorMessage)); + throw new Error(errorMessage); + } // TODO: log unexpected errors }) diff --git a/src/components/auth/authError/AuthError.jsx b/src/components/auth/authError/AuthError.jsx index a8f7873..d1ec0d6 100644 --- a/src/components/auth/authError/AuthError.jsx +++ b/src/components/auth/authError/AuthError.jsx @@ -71,6 +71,10 @@ export default class AuthError extends Component { 'error.you_must_accept_rules': () => this.errorsMap['error.rulesAgreement_required'](), 'error.key_required': () => , 'error.key_is_required': () => this.errorsMap['error.key_required'](), - 'error.key_not_exists': () => + 'error.key_not_exists': () => , + + 'error.newPassword_required': () => , + 'error.newRePassword_required': () => , + 'error.newRePassword_does_not_match': () => }; } diff --git a/src/components/auth/authError/AuthError.messages.js b/src/components/auth/authError/AuthError.messages.js index bd0c397..7388ec8 100644 --- a/src/components/auth/authError/AuthError.messages.js +++ b/src/components/auth/authError/AuthError.messages.js @@ -31,6 +31,16 @@ export default defineMessages({ defaultMessage: 'Please enter password' }, + newPasswordRequired: { + id: 'newPasswordRequired', + defaultMessage: 'Please enter new password' + }, + + newRePasswordRequired: { + id: 'newRePasswordRequired', + defaultMessage: 'Please repeat new password' + }, + usernameRequired: { id: 'usernameRequired', defaultMessage: 'Username is required' diff --git a/src/components/auth/changePassword/ChangePassword.jsx b/src/components/auth/changePassword/ChangePassword.jsx index 8eb9a2d..6e0d3aa 100644 --- a/src/components/auth/changePassword/ChangePassword.jsx +++ b/src/components/auth/changePassword/ChangePassword.jsx @@ -30,6 +30,7 @@ class Body extends BaseAuthBody { @@ -46,6 +48,7 @@ class Body extends BaseAuthBody { diff --git a/src/components/user/actions.js b/src/components/user/actions.js index adbd142..2c26707 100644 --- a/src/components/user/actions.js +++ b/src/components/user/actions.js @@ -24,7 +24,6 @@ export function logout() { return setUser({isGuest: true}); } - export function fetchUserData() { return (dispatch) => request.get('/api/accounts/current') @@ -45,6 +44,26 @@ export function fetchUserData() { }); } +export function changePassword({ + password = '', + newPassword = '', + newRePassword = '' +}) { + return (dispatch) => + request.post( + '/api/accounts/change-password', + {password, newPassword, newRePassword} + ) + .then((resp) => { + dispatch(updateUser({ + shouldChangePassword: false + })); + + return resp; + }) + ; +} + export function authenticate(token) { if (!token || token.split('.').length !== 3) { diff --git a/src/services/authFlow/ChangePasswordState.js b/src/services/authFlow/ChangePasswordState.js index e2f8eab..45570b7 100644 --- a/src/services/authFlow/ChangePasswordState.js +++ b/src/services/authFlow/ChangePasswordState.js @@ -6,6 +6,11 @@ export default class ChangePasswordState extends AbstractState { context.navigate('/change-password'); } + resolve(context, payload) { + context.run('changePassword', payload) + .then(() => context.setState(new CompleteState())); + } + reject(context) { context.run('updateUser', { shouldChangePassword: false