Password confirmation field appearing and preventing submission of mismatching passwords.

pull/1587/head
Warrick Corfe-Tan 5 years ago
parent 354cf3750e
commit 7ec90d55e9

@ -24,6 +24,7 @@ interface State {
error: string | null; error: string | null;
currentPasswordEntered: string | null; currentPasswordEntered: string | null;
currentPasswordConfirmEntered: string | null; currentPasswordConfirmEntered: string | null;
currentPasswordRetypeEntered: string | null;
} }
class SessionPasswordModalInner extends React.Component<Props, State> { class SessionPasswordModalInner extends React.Component<Props, State> {
@ -36,6 +37,7 @@ class SessionPasswordModalInner extends React.Component<Props, State> {
error: null, error: null,
currentPasswordEntered: null, currentPasswordEntered: null,
currentPasswordConfirmEntered: null, currentPasswordConfirmEntered: null,
currentPasswordRetypeEntered: null
}; };
this.showError = this.showError.bind(this); this.showError = this.showError.bind(this);
@ -45,6 +47,7 @@ class SessionPasswordModalInner extends React.Component<Props, State> {
this.onPasswordInput = this.onPasswordInput.bind(this); this.onPasswordInput = this.onPasswordInput.bind(this);
this.onPasswordConfirmInput = this.onPasswordConfirmInput.bind(this); this.onPasswordConfirmInput = this.onPasswordConfirmInput.bind(this);
this.onPasswordRetypeInput = this.onPasswordRetypeInput.bind(this);
} }
public componentDidMount() { public componentDidMount() {
@ -58,7 +61,7 @@ class SessionPasswordModalInner extends React.Component<Props, State> {
const { action, onOk } = this.props; const { action, onOk } = this.props;
const placeholders = const placeholders =
action === PasswordAction.Change action === PasswordAction.Change
? [window.i18n('typeInOldPassword'), window.i18n('enterPassword')] ? [window.i18n('typeInOldPassword'), window.i18n('enterPassword'), window.i18n('confirmPassword')]
: [window.i18n('enterPassword'), window.i18n('confirmPassword')]; : [window.i18n('enterPassword'), window.i18n('confirmPassword')];
const confirmButtonColor = const confirmButtonColor =
@ -92,6 +95,14 @@ class SessionPasswordModalInner extends React.Component<Props, State> {
onKeyUp={this.onPasswordConfirmInput} onKeyUp={this.onPasswordConfirmInput}
/> />
)} )}
{action === PasswordAction.Change && (
<input
type="password"
id="password-modal-input-reconfirm"
placeholder={placeholders[2]}
onKeyUp={this.onPasswordRetypeInput}
/>
)}
</div> </div>
<div className="spacer-sm" /> <div className="spacer-sm" />
@ -185,12 +196,21 @@ class SessionPasswordModalInner extends React.Component<Props, State> {
this.closeDialog(); this.closeDialog();
} }
private async handleActionChange(oldPassword: string, newPassword: string) { private async handleActionChange(oldPassword: string, newPassword: string, newConfirmedPassword: string) {
// We don't validate oldPassword on change: this is validate on the validatePasswordHash below // We don't validate oldPassword on change: this is validate on the validatePasswordHash below
// we only validate the newPassword here // we only validate the newPassword here
if (!this.validatePassword(newPassword)) { if (!this.validatePassword(newPassword)) {
return; return;
} }
// Check the retyped password matches the new password
if (newPassword !== newConfirmedPassword) {
this.setState({
error: window.i18n('passwordsDoNotMatch')
})
return
}
const isValidWithStoredInDB = Boolean( const isValidWithStoredInDB = Boolean(
await this.validatePasswordHash(oldPassword) await this.validatePasswordHash(oldPassword)
); );
@ -242,12 +262,14 @@ class SessionPasswordModalInner extends React.Component<Props, State> {
const { const {
currentPasswordEntered, currentPasswordEntered,
currentPasswordConfirmEntered, currentPasswordConfirmEntered,
currentPasswordRetypeEntered
} = this.state; } = this.state;
const { Set, Remove, Change } = PasswordAction; const { Set, Remove, Change } = PasswordAction;
// Trim leading / trailing whitespace for UX // Trim leading / trailing whitespace for UX
const firstPasswordEntered = (currentPasswordEntered || '').trim(); const firstPasswordEntered = (currentPasswordEntered || '').trim();
const secondPasswordEntered = (currentPasswordConfirmEntered || '').trim(); const secondPasswordEntered = (currentPasswordConfirmEntered || '').trim();
const thirdPasswordEntered = (currentPasswordRetypeEntered || '').trim();
switch (action) { switch (action) {
case Set: { case Set: {
@ -257,7 +279,8 @@ class SessionPasswordModalInner extends React.Component<Props, State> {
case Change: { case Change: {
await this.handleActionChange( await this.handleActionChange(
firstPasswordEntered, firstPasswordEntered,
secondPasswordEntered secondPasswordEntered,
thirdPasswordEntered
); );
return; return;
} }
@ -293,6 +316,15 @@ class SessionPasswordModalInner extends React.Component<Props, State> {
this.setState({ currentPasswordConfirmEntered }); this.setState({ currentPasswordConfirmEntered });
} }
private async onPasswordRetypeInput(event: any) {
if (event.key === 'Enter') {
return this.setPassword();
}
const currentPasswordRetypeEntered = event.target.value;
this.setState({ currentPasswordRetypeEntered });
}
} }
export const SessionPasswordModal = withTheme(SessionPasswordModalInner); export const SessionPasswordModal = withTheme(SessionPasswordModalInner);

Loading…
Cancel
Save