PasswordSettings.jsx 5.5 KB

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