From a9ba0e25472a0784469a3311f15a5b050ddb74e9 Mon Sep 17 00:00:00 2001 From: Manan Jadhav Date: Wed, 4 Aug 2021 21:10:08 +0530 Subject: [PATCH 1/5] fix(change-password): Handle confirm password validation in Change Password screen --- src/pages/settings/PasswordPage.js | 46 +++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/pages/settings/PasswordPage.js b/src/pages/settings/PasswordPage.js index a10c43c9d877..effff730e9bf 100755 --- a/src/pages/settings/PasswordPage.js +++ b/src/pages/settings/PasswordPage.js @@ -49,6 +49,7 @@ class PasswordPage extends Component { newPassword: '', confirmNewPassword: '', isPasswordRequirementsVisible: false, + shouldShowPasswordConfirmError: false, }; this.handleChangePassword = this.handleChangePassword.bind(this); @@ -59,6 +60,31 @@ class PasswordPage extends Component { Onyx.merge(ONYXKEYS.ACCOUNT, {error: '', success: ''}); } + onBlurNewPassword() { + if (!this.state.newPassword || !this.isValidPassword()) { + this.setState({isPasswordRequirementsVisible: true}); + } else { + this.setState({isPasswordRequirementsVisible: false}); + } + + if (this.state.newPassword && this.state.confirmNewPassword && !this.doPasswordsMatch()) { + this.setState({shouldShowPasswordConfirmError: true}); + } + } + + 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) => { @@ -68,6 +94,18 @@ class PasswordPage extends Component { }); } + doPasswordsMatch() { + return this.state.newPassword === this.state.confirmNewPassword; + } + + showPasswordMatchError() { + return Boolean( + !this.doPasswordsMatch() + && this.state.shouldShowPasswordConfirmError + && this.state.confirmNewPassword, + ); + } + render() { return ( { @@ -114,7 +152,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 && ( @@ -134,6 +172,7 @@ class PasswordPage extends Component { value={this.state.confirmNewPassword} onChangeText={confirmNewPassword => this.setState({confirmNewPassword})} onSubmitEditing={this.handleChangePassword} + onBlur={() => this.onBlurConfirmPassword()} /> {!isEmpty(this.props.account.error) && ( @@ -141,6 +180,11 @@ class PasswordPage extends Component { {this.props.account.error} )} + {this.showPasswordMatchError() && ( + + {this.props.translate('setPasswordPage.passwordsDontMatch')} + + )}