diff --git a/frontend/src/SettingsPage/SettingsPage.jsx b/frontend/src/SettingsPage/SettingsPage.jsx index fefeccb51a..53dfcad2d8 100644 --- a/frontend/src/SettingsPage/SettingsPage.jsx +++ b/frontend/src/SettingsPage/SettingsPage.jsx @@ -88,10 +88,11 @@ function SettingsPage(props) { const handlePasswordInput = (input) => { setNewPassword(input); - if (input.length > 100) { + const trimmedInput = input.trim(); + if (trimmedInput.length > 100) { setHelperText('Password should be Max 100 characters'); setValidPassword(false); - } else if (input.length < 5 && input.length > 0) { + } else if (trimmedInput.length < 5 && trimmedInput.length > 0) { setHelperText('Password should be at least 5 characters'); setValidPassword(false); } else { @@ -100,11 +101,19 @@ function SettingsPage(props) { } }; + const handleConfirmPasswordInput = (input) => { + setConfirmPassword(input); + }; + const changePassword = async () => { + const trimmedCurrentPassword = currentpassword.trim(); + const trimmedNewPassword = newPassword.trim(); + const trimmedConfirmPassword = confirmPassword.trim(); + const errorMsg = - (currentpassword.match(/^ *$/) !== null && 'Current password') || - (newPassword.match(/^ *$/) !== null && 'New password') || - (confirmPassword.match(/^ *$/) !== null && 'Confirm new password'); + (trimmedCurrentPassword.length === 0 && 'Current password') || + (trimmedNewPassword.length === 0 && 'New password') || + (trimmedConfirmPassword.length === 0 && 'Confirm new password'); if (errorMsg) { toast.error(errorMsg + " can't be empty!", { @@ -112,13 +121,13 @@ function SettingsPage(props) { }); return; } - if (currentpassword === newPassword) { + if (trimmedCurrentPassword === trimmedNewPassword) { toast.error("New password can't be the same as the current one!", { duration: 3000, }); return; } - if (newPassword !== confirmPassword) { + if (trimmedNewPassword !== trimmedConfirmPassword) { toast.error('New password and confirm new password should be same', { duration: 3000, }); @@ -127,7 +136,7 @@ function SettingsPage(props) { setPasswordChangeInProgress(true); try { - await userService.changePassword(currentpassword, newPassword); + await userService.changePassword(trimmedCurrentPassword, trimmedNewPassword); toast.success('Password updated successfully', { duration: 3000, }); @@ -293,7 +302,7 @@ function SettingsPage(props) { placeholder={t('header.profileSettingPage.confirmNewPassword', 'Confirm new password')} value={confirmPassword} ref={focusRef} - onChange={(event) => setConfirmPassword(event.target.value)} + onChange={(event) => handleConfirmPasswordInput(event.target.value)} onKeyPress={confirmPasswordKeyPressHandler} data-cy="confirm-password-input" /> @@ -301,7 +310,7 @@ function SettingsPage(props) {