| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- import React from 'react';
- import PropTypes from 'prop-types';
- import { withTranslation } from 'react-i18next';
- import PersonalContainer from '~/client/services/PersonalContainer';
- import { toastSuccess, toastError } from '~/client/util/apiNotification';
- import { apiv3Get, apiv3Put } from '~/client/util/apiv3-client';
- import { withUnstatedContainers } from '../UnstatedUtils';
- class PasswordSettings extends React.Component {
- constructor() {
- super();
- this.state = {
- retrieveError: null,
- oldPassword: '',
- newPassword: '',
- newPasswordConfirm: '',
- isPasswordSet: false,
- };
- this.onClickSubmit = this.onClickSubmit.bind(this);
- this.onChangeOldPassword = this.onChangeOldPassword.bind(this);
- }
- async componentDidMount() {
- try {
- const res = await apiv3Get('/personal-setting/is-password-set');
- const { isPasswordSet } = res.data;
- this.setState({ isPasswordSet });
- }
- catch (err) {
- toastError(err);
- this.setState({ retrieveError: err });
- }
- }
- async onClickSubmit() {
- const { t, personalContainer } = this.props;
- const { oldPassword, newPassword, newPasswordConfirm } = this.state;
- try {
- await apiv3Put('/personal-setting/password', {
- oldPassword, newPassword, newPasswordConfirm,
- });
- this.setState({ oldPassword: '', newPassword: '', newPasswordConfirm: '' });
- await personalContainer.retrievePersonalData();
- toastSuccess(t('toaster.update_successed', { target: t('Password') }));
- }
- catch (err) {
- toastError(err);
- }
- }
- onChangeOldPassword(oldPassword) {
- this.setState({ oldPassword });
- }
- onChangeNewPassword(newPassword) {
- this.setState({ newPassword });
- }
- onChangeNewPasswordConfirm(newPasswordConfirm) {
- this.setState({ newPasswordConfirm });
- }
- render() {
- const { t } = this.props;
- const { newPassword, newPasswordConfirm } = this.state;
- const isIncorrectConfirmPassword = (newPassword !== newPasswordConfirm);
- if (this.state.retrieveError != null) {
- throw new Error(this.state.retrieveError.message);
- }
- return (
- <React.Fragment>
- { (!this.state.isPasswordSet) && (
- <div className="alert alert-warning">{ t('personal_settings.password_is_not_set') }</div>
- ) }
- {(this.state.isPasswordSet)
- ? <h2 className="border-bottom my-4">{t('personal_settings.update_password')}</h2>
- : <h2 className="border-bottom my-4">{t('personal_settings.set_new_password')}</h2>}
- {(this.state.isPasswordSet)
- && (
- <div className="row mb-3">
- <label htmlFor="oldPassword" className="col-md-3 text-md-right">{ t('personal_settings.current_password') }</label>
- <div className="col-md-5">
- <input
- className="form-control"
- type="password"
- name="oldPassword"
- value={this.state.oldPassword}
- onChange={(e) => { this.onChangeOldPassword(e.target.value) }}
- />
- </div>
- </div>
- )}
- <div className="row mb-3">
- <label htmlFor="newPassword" className="col-md-3 text-md-right">{t('personal_settings.new_password') }</label>
- <div className="col-md-5">
- {/* to prevent autocomplete username into userForm[email] in BasicInfoSettings component */}
- {/* https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion */}
- <input type="password" autoComplete="new-password" style={{ display: 'none' }} />
- <input
- className="form-control"
- type="password"
- name="newPassword"
- value={this.state.newPassword}
- onChange={(e) => { this.onChangeNewPassword(e.target.value) }}
- />
- </div>
- </div>
- <div className={`row mb-3 ${isIncorrectConfirmPassword && 'has-error'}`}>
- <label htmlFor="newPasswordConfirm" className="col-md-3 text-md-right">{t('personal_settings.new_password_confirm') }</label>
- <div className="col-md-5">
- <input
- className="form-control"
- type="password"
- name="newPasswordConfirm"
- value={this.state.newPasswordConfirm}
- onChange={(e) => { this.onChangeNewPasswordConfirm(e.target.value) }}
- />
- <p className="form-text text-muted">{t('page_register.form_help.password') }</p>
- </div>
- </div>
- <div className="row my-3">
- <div className="offset-5">
- <button
- data-testid="grw-password-settings-update-button"
- type="button"
- className="btn btn-primary"
- onClick={this.onClickSubmit}
- disabled={isIncorrectConfirmPassword}
- >
- {t('Update')}
- </button>
- </div>
- </div>
- </React.Fragment>
- );
- }
- }
- const PasswordSettingsWrapper = withUnstatedContainers(PasswordSettings, [PersonalContainer]);
- PasswordSettings.propTypes = {
- t: PropTypes.func.isRequired, // i18next
- personalContainer: PropTypes.instanceOf(PersonalContainer).isRequired,
- };
- export default withTranslation()(PasswordSettingsWrapper);
|