PasswordSettings.jsx 5.2 KB

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