PasswordSettings.jsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { useTranslation } from 'react-i18next';
  4. import PersonalContainer from '~/client/services/PersonalContainer';
  5. import { toastSuccess, toastError } from '~/client/util/apiNotification';
  6. import { apiv3Get, apiv3Put } from '~/client/util/apiv3-client';
  7. import { withUnstatedContainers } from '../UnstatedUtils';
  8. class PasswordSettings extends React.Component {
  9. constructor() {
  10. super();
  11. this.state = {
  12. retrieveError: null,
  13. oldPassword: '',
  14. newPassword: '',
  15. newPasswordConfirm: '',
  16. isPasswordSet: false,
  17. minPasswordLength: null,
  18. };
  19. this.onClickSubmit = this.onClickSubmit.bind(this);
  20. this.onChangeOldPassword = this.onChangeOldPassword.bind(this);
  21. }
  22. async componentDidMount() {
  23. try {
  24. const res = await apiv3Get('/personal-setting/is-password-set');
  25. const { isPasswordSet, minPasswordLength } = res.data;
  26. this.setState({ isPasswordSet, minPasswordLength });
  27. }
  28. catch (err) {
  29. toastError(err);
  30. this.setState({ retrieveError: err });
  31. }
  32. }
  33. async onClickSubmit() {
  34. const { t, personalContainer } = this.props;
  35. const { oldPassword, newPassword, newPasswordConfirm } = this.state;
  36. try {
  37. await apiv3Put('/personal-setting/password', {
  38. oldPassword, newPassword, newPasswordConfirm,
  39. });
  40. this.setState({ oldPassword: '', newPassword: '', newPasswordConfirm: '' });
  41. await personalContainer.retrievePersonalData();
  42. toastSuccess(t('toaster.update_successed', { target: t('Password') }));
  43. }
  44. catch (err) {
  45. toastError(err);
  46. }
  47. }
  48. onChangeOldPassword(oldPassword) {
  49. this.setState({ oldPassword });
  50. }
  51. onChangeNewPassword(newPassword) {
  52. this.setState({ newPassword });
  53. }
  54. onChangeNewPasswordConfirm(newPasswordConfirm) {
  55. this.setState({ newPasswordConfirm });
  56. }
  57. render() {
  58. const { t } = this.props;
  59. const { newPassword, newPasswordConfirm, minPasswordLength } = this.state;
  60. const isIncorrectConfirmPassword = (newPassword !== newPasswordConfirm);
  61. if (this.state.retrieveError != null) {
  62. throw new Error(this.state.retrieveError.message);
  63. }
  64. return (
  65. <React.Fragment>
  66. { (!this.state.isPasswordSet) && (
  67. <div className="alert alert-warning">{ t('personal_settings.password_is_not_set') }</div>
  68. ) }
  69. {(this.state.isPasswordSet)
  70. ? <h2 className="border-bottom my-4">{t('personal_settings.update_password')}</h2>
  71. : <h2 className="border-bottom my-4">{t('personal_settings.set_new_password')}</h2>}
  72. {(this.state.isPasswordSet)
  73. && (
  74. <div className="row mb-3">
  75. <label htmlFor="oldPassword" className="col-md-3 text-md-right">{ t('personal_settings.current_password') }</label>
  76. <div className="col-md-5">
  77. <input
  78. className="form-control"
  79. type="password"
  80. name="oldPassword"
  81. value={this.state.oldPassword}
  82. onChange={(e) => { this.onChangeOldPassword(e.target.value) }}
  83. />
  84. </div>
  85. </div>
  86. )}
  87. <div className="row mb-3">
  88. <label htmlFor="newPassword" className="col-md-3 text-md-right">{t('personal_settings.new_password') }</label>
  89. <div className="col-md-5">
  90. {/* to prevent autocomplete username into userForm[email] in BasicInfoSettings component */}
  91. {/* https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion */}
  92. <input type="password" autoComplete="new-password" style={{ display: 'none' }} />
  93. <input
  94. className="form-control"
  95. type="password"
  96. name="newPassword"
  97. value={this.state.newPassword}
  98. onChange={(e) => { this.onChangeNewPassword(e.target.value) }}
  99. />
  100. </div>
  101. </div>
  102. <div className={`row mb-3 ${isIncorrectConfirmPassword && 'has-error'}`}>
  103. <label htmlFor="newPasswordConfirm" className="col-md-3 text-md-right">{t('personal_settings.new_password_confirm') }</label>
  104. <div className="col-md-5">
  105. <input
  106. className="form-control"
  107. type="password"
  108. name="newPasswordConfirm"
  109. value={this.state.newPasswordConfirm}
  110. onChange={(e) => { this.onChangeNewPasswordConfirm(e.target.value) }}
  111. />
  112. <p className="form-text text-muted">{t('page_register.form_help.password', { target: minPasswordLength }) }</p>
  113. </div>
  114. </div>
  115. <div className="row my-3">
  116. <div className="offset-5">
  117. <button
  118. data-testid="grw-password-settings-update-button"
  119. type="button"
  120. className="btn btn-primary"
  121. onClick={this.onClickSubmit}
  122. disabled={isIncorrectConfirmPassword}
  123. >
  124. {t('Update')}
  125. </button>
  126. </div>
  127. </div>
  128. </React.Fragment>
  129. );
  130. }
  131. }
  132. PasswordSettings.propTypes = {
  133. t: PropTypes.func.isRequired, // i18next
  134. personalContainer: PropTypes.instanceOf(PersonalContainer).isRequired,
  135. };
  136. const PasswordSettingsWrapperFC = (props) => {
  137. const { t } = useTranslation();
  138. return <PasswordSettings t={t} {...props} />;
  139. };
  140. /**
  141. * Wrapper component for using unstated
  142. */
  143. const PasswordSettingsWrapper = withUnstatedContainers(PasswordSettingsWrapperFC, [PersonalContainer]);
  144. export default PasswordSettingsWrapper;