From 8e7fa33909959f11efe392fa97c481a793d8675c Mon Sep 17 00:00:00 2001 From: SleepWalker Date: Wed, 16 Mar 2016 07:54:42 +0200 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=BD=D1=82=D0=B5=D0=B3=D1=80=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=B1=D0=B5=D0=BA=D0=B0=20=D1=81=20=D1=84?= =?UTF-8?q?=D0=BE=D1=80=D0=BC=D0=BE=D0=B9=20=D1=81=D0=BC=D0=B5=D0=BD=D1=8B?= =?UTF-8?q?=20=D0=BF=D0=B0=D1=80=D0=BE=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/auth/actions.js | 31 ++++++++++++++++--- src/components/auth/authError/AuthError.jsx | 6 +++- .../auth/authError/AuthError.messages.js | 10 ++++++ .../auth/changePassword/ChangePassword.jsx | 3 ++ src/components/user/actions.js | 21 ++++++++++++- src/services/authFlow/ChangePasswordState.js | 5 +++ 6 files changed, 69 insertions(+), 7 deletions(-) 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