Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Confirm Password Validation in "Change Password" screen #4409

Merged
45 changes: 43 additions & 2 deletions src/pages/settings/PasswordPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class PasswordPage extends Component {
newPassword: '',
confirmNewPassword: '',
isPasswordRequirementsVisible: false,
shouldShowPasswordConfirmError: false,
};

this.handleChangePassword = this.handleChangePassword.bind(this);
Expand All @@ -59,6 +60,36 @@ class PasswordPage extends Component {
Onyx.merge(ONYXKEYS.ACCOUNT, {error: '', success: ''});
}

onBlurNewPassword() {
const stateToUpdate = {};
if (!this.state.newPassword || !this.isValidPassword()) {
stateToUpdate.isPasswordRequirementsVisible = true;
} else {
stateToUpdate.isPasswordRequirementsVisible = false;
}

if (this.state.newPassword && this.state.confirmNewPassword && !this.doPasswordsMatch()) {
stateToUpdate.shouldShowPasswordConfirmError = true;
}

if (!isEmpty(stateToUpdate)) {
this.setState(stateToUpdate);
}
}

onBlurConfirmPassword() {
if (!this.state.confirmNewPassword || !this.doPasswordsMatch()) {
this.setState({shouldShowPasswordConfirmError: true});
} else {
this.setState({shouldShowPasswordConfirmError: false});
}
}

isValidPassword() {
return this.state.newPassword.match(CONST.PASSWORD_COMPLEXITY_REGEX_STRING);
}


handleChangePassword() {
changePassword(this.state.currentPassword, this.state.newPassword)
.then((response) => {
Expand All @@ -68,6 +99,10 @@ class PasswordPage extends Component {
});
}

doPasswordsMatch() {
return this.state.newPassword === this.state.confirmNewPassword;
}

render() {
return (
<ScreenWrapper onTransitionEnd={() => {
Expand Down Expand Up @@ -114,7 +149,7 @@ class PasswordPage extends Component {
value={this.state.newPassword}
onChangeText={newPassword => this.setState({newPassword})}
onFocus={() => this.setState({isPasswordRequirementsVisible: true})}
onBlur={() => this.setState({isPasswordRequirementsVisible: false})}
onBlur={() => this.onBlurNewPassword()}
/>
{this.state.isPasswordRequirementsVisible && (
<Text style={[styles.formHint, styles.mt1]}>
Expand All @@ -134,13 +169,19 @@ class PasswordPage extends Component {
value={this.state.confirmNewPassword}
onChangeText={confirmNewPassword => this.setState({confirmNewPassword})}
onSubmitEditing={this.handleChangePassword}
onBlur={() => this.onBlurConfirmPassword()}
/>
</View>
{!isEmpty(this.props.account.error) && (
{!this.state.shouldShowPasswordConfirmError && !isEmpty(this.props.account.error) && (
<Text style={styles.formError}>
{this.props.account.error}
</Text>
)}
{this.state.shouldShowPasswordConfirmError && (
<Text style={[styles.formError, styles.mt1]}>
{this.props.translate('setPasswordPage.passwordsDontMatch')}
</Text>
)}
</ScrollView>
<FixedFooter style={[styles.flexGrow0]}>
<Button
Expand Down