Skip to content

Commit

Permalink
updated password validation callback function
Browse files Browse the repository at this point in the history
  • Loading branch information
manojleaton committed Mar 27, 2024
1 parent c6f3b67 commit 0798c4c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const ChangePasswordDialog: React.FC<ChangePasswordDialogProps> = (props)
const [showSuccessScreen, setShowSuccessScreen] = useState(false);
const { actions } = useAuthContext();

const passwordRequirements = defaultPasswordRequirements(t);
const passwordReqs = PasswordProps?.passwordRequirements ?? defaultPasswordRequirements(t);

const updateFields = useCallback(
(fields: { password: string; confirm: string }) => {
Expand All @@ -70,14 +70,14 @@ export const ChangePasswordDialog: React.FC<ChangePasswordDialogProps> = (props)
);

const areValidMatchingPasswords = useCallback((): boolean => {
if (PasswordProps?.passwordRequirements?.length === 0) {
if (passwordReqs?.length === 0) {
return confirmInput === passwordInput;
}
for (let i = 0; i < passwordRequirements.length; i++) {
if (!new RegExp(passwordRequirements[i].regex).test(passwordInput)) return false;
for (let i = 0; i < passwordReqs.length; i++) {
if (!new RegExp(passwordReqs[i].regex).test(passwordInput)) return false;
}
return confirmInput === passwordInput;
}, [PasswordProps?.passwordRequirements?.length, passwordRequirements, passwordInput, confirmInput]);
}, [passwordReqs, passwordInput, confirmInput]);

const checkPasswords =
currentInput !== '' && passwordInput !== '' && confirmInput !== '' && areValidMatchingPasswords();
Expand Down Expand Up @@ -115,7 +115,7 @@ export const ChangePasswordDialog: React.FC<ChangePasswordDialogProps> = (props)
confirmRef,
initialNewPasswordValue: passwordInput,
initialConfirmPasswordValue: confirmInput,
passwordRequirements,
passwordRequirements: passwordReqs,
passwordNotMatchError: t('bluiCommon:FORMS.PASS_MATCH_ERROR'),
...PasswordProps,
onPasswordChange: (passwordData: { password: string; confirm: string }): void => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ describe('Create Password Screen', () => {
const passwordField = getByLabelText('Password');
const confirmPasswordField = getByLabelText('Confirm Password');

fireEvent.change(passwordField, { target: { value: 'Abcd@123' } });
fireEvent.change(passwordField, { target: { value: 'Ab@12' } });
fireEvent.blur(passwordField);
fireEvent.change(confirmPasswordField, { target: { value: 'Abcd@123' } });
fireEvent.change(confirmPasswordField, { target: { value: 'Ab@12' } });
fireEvent.blur(confirmPasswordField);

const nextButton = screen.getByText('Next');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const CreatePasswordScreen: React.FC<CreatePasswordScreenProps> = (props)
confirmPassword !== '' ? confirmPassword : PasswordProps?.initialConfirmPasswordValue ?? ''
);
const [isLoading, setIsLoading] = useState(false);
const passwordRequirements = defaultPasswordRequirements(t);
const passwordReqs = PasswordProps?.passwordRequirements ?? defaultPasswordRequirements(t);
const { triggerError, errorManagerConfig } = useErrorManager();
const errorDisplayConfig = {
...errorManagerConfig,
Expand Down Expand Up @@ -92,20 +92,20 @@ export const CreatePasswordScreen: React.FC<CreatePasswordScreenProps> = (props)
);

const areValidMatchingPasswords = useCallback((): boolean => {
if (PasswordProps?.passwordRequirements?.length === 0) {
if (passwordReqs?.length === 0) {
return confirmInput === passwordInput;
}
for (let i = 0; i < passwordRequirements.length; i++) {
if (!new RegExp(passwordRequirements[i].regex).test(passwordInput)) return false;
for (let i = 0; i < passwordReqs.length; i++) {
if (!new RegExp(passwordReqs[i].regex).test(passwordInput)) return false;
}
return confirmInput === passwordInput;
}, [PasswordProps?.passwordRequirements?.length, passwordRequirements, passwordInput, confirmInput]);
}, [passwordReqs, passwordInput, confirmInput]);

const passwordProps = {
newPasswordLabel: t('bluiCommon:FORMS.PASSWORD'),
confirmPasswordLabel: t('bluiCommon:FORMS.CONFIRM_PASSWORD'),
passwordNotMatchError: t('bluiCommon:FORMS.PASS_MATCH_ERROR'),
passwordRequirements: PasswordProps?.passwordRequirements ?? passwordRequirements,
passwordRequirements: passwordReqs,
passwordRef,
confirmRef,
...PasswordProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const ResetPasswordScreen: React.FC<ResetPasswordScreenProps> = (props) =
const { code, email } = parseQueryString(window.location.search);

const { actions, navigate, routeConfig } = useAuthContext();
const passwordRequirements = defaultPasswordRequirements(t);
const passwordReqs = PasswordProps?.passwordRequirements ?? defaultPasswordRequirements(t);

const verifyResetCode = useCallback(async (): Promise<void> => {
try {
Expand Down Expand Up @@ -92,14 +92,14 @@ export const ResetPasswordScreen: React.FC<ResetPasswordScreenProps> = (props) =
}, [actions, code, passwordInput, email, triggerError, props.showSuccessScreen, navigate, routeConfig]);

const areValidMatchingPasswords = useCallback((): boolean => {
if (PasswordProps?.passwordRequirements?.length === 0) {
if (passwordReqs?.length === 0) {
return confirmInput === passwordInput;
}
for (let i = 0; i < passwordRequirements.length; i++) {
if (!new RegExp(passwordRequirements[i].regex).test(passwordInput)) return false;
for (let i = 0; i < passwordReqs.length; i++) {
if (!new RegExp(passwordReqs[i].regex).test(passwordInput)) return false;
}
return confirmInput === passwordInput;
}, [PasswordProps?.passwordRequirements?.length, passwordRequirements, passwordInput, confirmInput]);
}, [passwordReqs, passwordInput, confirmInput]);

const updateFields = useCallback(
(fields: { password: string; confirm: string }) => {
Expand Down Expand Up @@ -151,7 +151,7 @@ export const ResetPasswordScreen: React.FC<ResetPasswordScreenProps> = (props) =
newPasswordLabel: t('bluiAuth:CHANGE_PASSWORD.NEW_PASSWORD'),
confirmPasswordLabel: t('bluiAuth:CHANGE_PASSWORD.CONFIRM_NEW_PASSWORD'),
passwordNotMatchError: t('bluiCommon:FORMS.PASS_MATCH_ERROR'),
passwordRequirements: PasswordProps?.passwordRequirements ?? passwordRequirements,
passwordRequirements: passwordReqs,
passwordRef,
confirmRef,
...PasswordProps,
Expand Down

0 comments on commit 0798c4c

Please sign in to comment.