From f86946d2ae2394d9072bad1d671884f4a12fb22e Mon Sep 17 00:00:00 2001
From: Anca Ursachi <anca.ursachi@thinslices.com>
Date: Tue, 4 Dec 2018 00:12:09 +0200
Subject: [PATCH] fix(changePassword): Make password validation work after we
 want to change the password.

---
 ...dConfirmation.js => PasswordValidation.js} | 20 +++++++++----------
 ...dConfirmation.md => PasswordValidation.md} |  2 +-
 packages/component-faraday-ui/src/Utils.js    | 11 +++++-----
 packages/component-faraday-ui/src/index.js    |  2 +-
 .../src/routes/users/changePassword.js        | 11 +++++-----
 .../SignUp/ReviewerInviteDecision.js          |  3 ++-
 .../src/components/SignUp/SignUpStep1.js      |  6 +++---
 .../UserProfile/ChangePasswordPage.js         | 10 ++++------
 .../src/components/utils.js                   |  4 ++--
 9 files changed, 33 insertions(+), 36 deletions(-)
 rename packages/component-faraday-ui/src/{PasswordConfirmation.js => PasswordValidation.js} (93%)
 rename packages/component-faraday-ui/src/{PasswordConfirmation.md => PasswordValidation.md} (77%)

diff --git a/packages/component-faraday-ui/src/PasswordConfirmation.js b/packages/component-faraday-ui/src/PasswordValidation.js
similarity index 93%
rename from packages/component-faraday-ui/src/PasswordConfirmation.js
rename to packages/component-faraday-ui/src/PasswordValidation.js
index a6aa981d2..7e0734078 100644
--- a/packages/component-faraday-ui/src/PasswordConfirmation.js
+++ b/packages/component-faraday-ui/src/PasswordValidation.js
@@ -19,7 +19,7 @@ import {
 
 const PasswordField = input => <TextField {...input} type="password" />
 
-const PasswordConfirmation = ({
+const PasswordValidation = ({
   formName,
   formLabel,
   minLength,
@@ -45,7 +45,7 @@ const PasswordConfirmation = ({
         <Label required>Retype Password</Label>
         <ValidatedField
           component={PasswordField}
-          name="confirmPassword"
+          name="confirmNewPassword"
           validate={[required]}
         />
       </Item>
@@ -94,7 +94,7 @@ const PasswordConfirmation = ({
     </Row>
   </Fragment>
 )
-PasswordConfirmation.propTypes = {
+PasswordValidation.propTypes = {
   /** Name of the redux form. */
   formName: PropTypes.string,
   /** Label name for password input */
@@ -102,17 +102,17 @@ PasswordConfirmation.propTypes = {
   /** If the password requirements are not fulfilled it will return true. */
   submitFailed: PropTypes.bool,
   /** If the password's length is greater or equal than the minimum value, it will return true. */
-  minLength: PropTypes.func,
+  minLength: PropTypes.bool,
   /** If the password has at least one digit it will return true. */
-  atLeastOneDigit: PropTypes.func,
+  atLeastOneDigit: PropTypes.bool,
   /** If the password has at least one uppercase it will return true. */
-  atLeastOneUppercase: PropTypes.func,
+  atLeastOneUppercase: PropTypes.bool,
   /** If the password has at least one lowercase it will return true. */
-  atLeastOneLowerrcase: PropTypes.func,
+  atLeastOneLowerrcase: PropTypes.bool,
   /** If the password has at least one punctuation it will return true. */
-  atLeastOnePunctuation: PropTypes.func,
+  atLeastOnePunctuation: PropTypes.bool,
 }
-PasswordConfirmation.defaultProps = {
+PasswordValidation.defaultProps = {
   formName: undefined,
   formLabel: undefined,
   submitFailed: false,
@@ -137,7 +137,7 @@ export default compose(
       get(formValues, 'password', ''),
     ),
   })),
-)(PasswordConfirmation)
+)(PasswordValidation)
 
 const RulesTitle = styled.p`
   margin: 0px;
diff --git a/packages/component-faraday-ui/src/PasswordConfirmation.md b/packages/component-faraday-ui/src/PasswordValidation.md
similarity index 77%
rename from packages/component-faraday-ui/src/PasswordConfirmation.md
rename to packages/component-faraday-ui/src/PasswordValidation.md
index 88bb5be46..8a09ec872 100644
--- a/packages/component-faraday-ui/src/PasswordConfirmation.md
+++ b/packages/component-faraday-ui/src/PasswordValidation.md
@@ -3,7 +3,7 @@ const React = require('react')
 const reduxForm = require('redux-form').reduxForm;
 
 const PasswordField = () => (
-  <PasswordConfirmation formLabel="Password" formName="signUpInvitation" />
+  <PasswordValidation formLabel="Password" formName="signUpInvitation" />
 )
 
 const Form = reduxForm({
diff --git a/packages/component-faraday-ui/src/Utils.js b/packages/component-faraday-ui/src/Utils.js
index 704a4daf3..95db29476 100644
--- a/packages/component-faraday-ui/src/Utils.js
+++ b/packages/component-faraday-ui/src/Utils.js
@@ -19,8 +19,7 @@ export const atLeastOnePunctuation = value => {
 
 export const passwordValidator = values => {
   const errors = {}
-  const { password, confirmPassword } = values
-
+  const { password, confirmNewPassword } = values
   if (
     !(
       minLength(password, 6) &&
@@ -35,10 +34,10 @@ export const passwordValidator = values => {
   if (!password) {
     errors.password = 'Required'
   }
-  if (!confirmPassword) {
-    errors.confirmPassword = 'Required'
-  } else if (confirmPassword !== password) {
-    errors.confirmPassword = "Passwords don't match."
+  if (!confirmNewPassword) {
+    errors.confirmNewPassword = 'Required'
+  } else if (confirmNewPassword !== password) {
+    errors.confirmNewPassword = "Passwords don't match."
   }
 
   return errors
diff --git a/packages/component-faraday-ui/src/index.js b/packages/component-faraday-ui/src/index.js
index 7f5dc9436..b5e2c1089 100644
--- a/packages/component-faraday-ui/src/index.js
+++ b/packages/component-faraday-ui/src/index.js
@@ -48,7 +48,7 @@ export { default as WizardFiles } from './WizardFiles'
 export { default as TextTooltip } from './TextTooltip'
 export { default as EditorialReportCard } from './EditorialReportCard'
 export { default as ReviewerReportAuthor } from './ReviewerReportAuthor'
-export { default as PasswordConfirmation } from './PasswordConfirmation'
+export { default as PasswordValidation } from './PasswordValidation'
 
 export { SubmitRevision } from './submissionRevision'
 
diff --git a/packages/component-user-manager/src/routes/users/changePassword.js b/packages/component-user-manager/src/routes/users/changePassword.js
index 040fd8729..7630d4374 100644
--- a/packages/component-user-manager/src/routes/users/changePassword.js
+++ b/packages/component-user-manager/src/routes/users/changePassword.js
@@ -3,11 +3,10 @@ const { token } = require('pubsweet-server/src/authentication')
 const { passwordStrengthRegex } = require('config')
 
 module.exports = models => async (req, res) => {
-  const { password, newPassword } = req.body
-  if (!services.checkForUndefinedParams(password, newPassword))
+  const { currentPassword, password } = req.body
+  if (!services.checkForUndefinedParams(currentPassword, password))
     return res.status(400).json({ error: 'Missing required params.' })
-
-  if (!passwordStrengthRegex.test(newPassword))
+  if (!passwordStrengthRegex.test(password))
     return res.status(400).json({
       error: 'Password is too weak. Please check password requirements.',
     })
@@ -16,10 +15,10 @@ module.exports = models => async (req, res) => {
   try {
     user = await models.User.find(req.user)
 
-    if (!await user.validPassword(password)) {
+    if (!await user.validPassword(currentPassword)) {
       return res.status(400).json({ error: 'Wrong username or password.' })
     }
-    user.password = newPassword
+    user.password = password
     user = await user.save()
 
     return res.status(200).json({
diff --git a/packages/components-faraday/src/components/SignUp/ReviewerInviteDecision.js b/packages/components-faraday/src/components/SignUp/ReviewerInviteDecision.js
index 917b0073b..a7c1cbaea 100644
--- a/packages/components-faraday/src/components/SignUp/ReviewerInviteDecision.js
+++ b/packages/components-faraday/src/components/SignUp/ReviewerInviteDecision.js
@@ -15,7 +15,8 @@ import {
   withFetching,
 } from 'pubsweet-component-faraday-ui'
 
-import { redirectToError, passwordValidator } from '../utils'
+import { redirectToError } from '../utils'
+import { passwordValidator } from '../../../../component-faraday-ui/src/Utils'
 import { reviewerDecision, setReviewerPassword } from '../../redux/reviewers'
 
 const agreeText = `You have been invited to review a manuscript on the Hindawi platform. Please set a password and proceed to the manuscript.`
diff --git a/packages/components-faraday/src/components/SignUp/SignUpStep1.js b/packages/components-faraday/src/components/SignUp/SignUpStep1.js
index a8b701635..2a153c89e 100644
--- a/packages/components-faraday/src/components/SignUp/SignUpStep1.js
+++ b/packages/components-faraday/src/components/SignUp/SignUpStep1.js
@@ -7,7 +7,7 @@ import {
   Item,
   Label,
   Text,
-  PasswordConfirmation,
+  PasswordValidation,
 } from 'pubsweet-component-faraday-ui'
 
 import { emailValidator } from '../utils'
@@ -27,12 +27,12 @@ const SignUpForm = () => (
         />
       </Item>
     </Row>
-    <PasswordConfirmation formLabel="Password" formName="signUpInvitation" />
+    <PasswordValidation formLabel="Password" formName="signUpInvitation" />
   </Fragment>
 )
 
 const InviteForm = () => (
-  <PasswordConfirmation formLabel="Password" formName="signUpInvitation" />
+  <PasswordValidation formLabel="Password" formName="signUpInvitation" />
 )
 
 const ForgotEmailForm = () => (
diff --git a/packages/components-faraday/src/components/UserProfile/ChangePasswordPage.js b/packages/components-faraday/src/components/UserProfile/ChangePasswordPage.js
index a715a46f2..7bb23a363 100644
--- a/packages/components-faraday/src/components/UserProfile/ChangePasswordPage.js
+++ b/packages/components-faraday/src/components/UserProfile/ChangePasswordPage.js
@@ -11,13 +11,11 @@ import {
   Text,
   Label,
   ShadowedBox,
-  PasswordConfirmation,
+  PasswordValidation,
 } from 'pubsweet-component-faraday-ui'
 
-import {
-  changePasswordValidator,
-  onSubmitChangePassword as onSubmit,
-} from '../utils'
+import { changePasswordValidator } from '../../../../component-faraday-ui/src/Utils'
+import { onSubmitChangePassword as onSubmit } from '../utils'
 
 const PasswordField = input => <TextField {...input} type="password" />
 
@@ -40,7 +38,7 @@ const ChangePassword = ({ history, handleSubmit, error }) => (
         />
       </Item>
     </Row>
-    <PasswordConfirmation formLabel="New Password" formName="changePassword" />
+    <PasswordValidation formLabel="New Password" formName="changePassword" />
     {error && (
       <Row mt={1}>
         <Item>
diff --git a/packages/components-faraday/src/components/utils.js b/packages/components-faraday/src/components/utils.js
index 6ca28eae7..0f1f81c6b 100644
--- a/packages/components-faraday/src/components/utils.js
+++ b/packages/components-faraday/src/components/utils.js
@@ -146,11 +146,11 @@ export const saveUserDetails = (userId, values) => dispatch =>
   })
 
 export const onSubmitChangePassword = (
-  { password, newPassword },
+  { currentPassword, password },
   dispatch,
   { history },
 ) =>
-  create(`/users/change-password`, { password, newPassword })
+  create(`/users/change-password`, { currentPassword, password })
     .then(() => {
       history.goBack()
     })
-- 
GitLab