fixed more error handling for password checks & translations

This commit is contained in:
Joel Biddle
2023-08-22 22:42:02 +10:00
parent 00089a6bba
commit 1f60a3d73e
12 changed files with 294 additions and 163 deletions
@@ -6,9 +6,12 @@ interface PasswordCheckProps {
errorCheck: boolean;
setPasswordErrorTooShort: (value: boolean) => void;
setPasswordErrorTooLong: (value: boolean) => void;
setPasswordErrorNumber: (value: boolean) => void;
setPasswordErrorUpperCase: (value: boolean) => void;
setPasswordErrorLowerCase: (value: boolean) => void;
setPasswordErrorIsBreached: (value: boolean) => void;
setPasswordErrorNumber: (value: boolean) => void;
setPasswordErrorSpecialChar: (value: boolean) => void;
setPasswordErrorRepeatedChar: (value: boolean) => void;
setPasswordErrorIsBreachedPassword: (value: boolean) => void;
}
/**
@@ -18,11 +21,15 @@ const passwordCheck = async ({
password,
setPasswordErrorTooShort,
setPasswordErrorTooLong,
setPasswordErrorNumber,
setPasswordErrorUpperCase,
setPasswordErrorLowerCase,
setPasswordErrorIsBreached,
setPasswordErrorNumber,
setPasswordErrorSpecialChar,
setPasswordErrorRepeatedChar,
setPasswordErrorIsBreachedPassword,
errorCheck
}: PasswordCheckProps) => {
// tooShort
if (!password || password.length < 14) {
setPasswordErrorTooShort(true);
errorCheck = true;
@@ -30,6 +37,7 @@ const passwordCheck = async ({
setPasswordErrorTooShort(false);
}
// tooLong
if (password.length > 100) {
setPasswordErrorTooLong(true);
errorCheck = true;
@@ -37,51 +45,54 @@ const passwordCheck = async ({
setPasswordErrorTooLong(false);
}
if (!/\d/.test(password)) {
// upperCase
if (!/[A-Z]/.test(password)) {
setPasswordErrorUpperCase(true);
errorCheck = true;
} else {
setPasswordErrorUpperCase(false);
}
// lowerCase
if (!/[a-z]/.test(password)) {
setPasswordErrorLowerCase(true);
errorCheck = true;
} else {
setPasswordErrorLowerCase(false);
}
// number
if (!/[0-9]/.test(password)) {
setPasswordErrorNumber(true);
errorCheck = true;
} else {
setPasswordErrorNumber(false);
}
if (!/[a-z]/.test(password)) {
setPasswordErrorLowerCase(true);
// specialChar
if (!/[!@#$%^&*(),.?":{}|<>]/.test(password)) {
setPasswordErrorSpecialChar(true);
errorCheck = true;
// } else if (/(.)(?:(?!\1).){1,2}/.test(password)) {
// console.log(111)
// setPasswordError(true);
// setPasswordErrorMessage("Password should not contain repeating characters.");
// errorCheck = true;
// } else if (RegExp(`[${email}]`).test(password)) {
// console.log(222)
// setPasswordError(true);
// setPasswordErrorMessage("Password should not contain your email.");
// errorCheck = true;
} else {
setPasswordErrorLowerCase(false);
setPasswordErrorSpecialChar(false);
}
// repeatedChar
if (/([A-Za-z0-9])\1\1\1/.test(password)) {
setPasswordErrorRepeatedChar(true);
errorCheck = true;
} else {
setPasswordErrorRepeatedChar(false);
}
// breachedPassword
if (await checkIsPasswordBreached(password)) {
setPasswordErrorIsBreached(true);
setPasswordErrorIsBreachedPassword(true);
errorCheck = true;
} else {
setPasswordErrorIsBreached(false);
setPasswordErrorIsBreachedPassword(false);
}
// if (!/[A-Z]/.test(password)) {
// setPasswordErrorUpperCase(true);
// errorCheck = true;
// } else {
// setPasswordErrorUpperCase(false);
// }
// if (!/(?=.*[!@#$%^&*])/.test(password)) {
// setPasswordErrorSpecialChar(true);
// // "Please add at least 1 special character (*, !, #, %)."
// errorCheck = true;
// } else {
// setPasswordErrorSpecialChar(false);
// }
return errorCheck;
};
@@ -8,8 +8,8 @@ type Errors = {
number?: string;
specialChar?: string;
repeatedChar?: string;
commonPassword?: string;
breachedPassword?: string;
isBreachedPassword?: string;
isCommonPassword?: string;
};
interface CheckPasswordParams {
@@ -20,12 +20,15 @@ interface CheckPasswordParams {
/**
* Validate that the password [password]:
* - Contains at least 14 characters long
* - Contains at most 100 characters long
* - Contains at least 14 characters
* - Contains at most 100 characters
* - Contains at least 1 uppercase character (A-Z)
* - Contains at least 1 lowercase character (a-z)
* - Contains at least 1 number (0-9)
* - Contains at least 1 special character
* - Does not contain 3 repeat, consecutive characters
* - Is not in a database of breached passwords
* - Is not in a list of common passwords
*
* The function returns whether or not the password [password]
* passes the minimum requirements above. It sets errors on
@@ -42,41 +45,50 @@ const checkPassword = async ({
}: CheckPasswordParams): Promise<boolean> => {
const errors: Errors = {};
// tooShort
if (password.length < 14) {
errors.tooShort = "at least 14 characters";
}
// toolong
if (password.length > 100) {
errors.tooLong = "at most 100 characters";
}
// upperCase
if (!/[A-Z]/.test(password)) {
errors.upperCase = "at least 1 uppercase character (A-Z)";
}
// lowerCase
if (!/[a-z]/.test(password)) {
errors.lowerCase = "at least 1 lowercase character (a-z)";
}
// number
if (!/[0-9]/.test(password)) {
errors.number = "at least 1 number (0-9)";
}
// specialChar
if (!/[!@#$%^&*(),.?":{}|<>]/.test(password)) {
errors.specialChar = "at least 1 special character (!@#$%^&*(),.?)";
}
// repeatedChar
if (/([A-Za-z0-9])\1\1\1/.test(password)) {
errors.repeatedChar = "No 3 repeat, consecutive characters";
}
// breachedPassword
if (await checkIsPasswordBreached(password)) {
errors.breachedPassword =
errors.isBreachedPassword =
"The password you provided is in a list of passwords commonly used on other websites. Please try again with a stronger password.";
}
// commonPassword
if (commonPasswords.includes(password)) {
errors.commonPassword = "No common passwords";
errors.isCommonPassword = "No common passwords";
}
setErrors(errors);