Added check that password is not an email address

This commit is contained in:
Joel Biddle
2023-08-23 02:14:22 +10:00
parent 2960f86647
commit ea262da505
15 changed files with 88 additions and 13 deletions
@@ -45,6 +45,7 @@ type Errors = {
number?: string;
specialChar?: string;
repeatedChar?: string;
isEmail?: string;
isBeachedPassword?: string;
};
@@ -1,3 +1,4 @@
import isEmail from "validator/lib/isEmail";
import { checkIsPasswordBreached } from "./checkIsPasswordBreached";
/* eslint-disable no-param-reassign */
@@ -11,6 +12,7 @@ interface PasswordCheckProps {
setPasswordErrorNumber: (value: boolean) => void;
setPasswordErrorSpecialChar: (value: boolean) => void;
setPasswordErrorRepeatedChar: (value: boolean) => void;
setPasswordErrorIsEmail: (value: boolean) => void;
setPasswordErrorIsBreachedPassword: (value: boolean) => void;
}
@@ -26,6 +28,7 @@ const passwordCheck = async ({
setPasswordErrorNumber,
setPasswordErrorSpecialChar,
setPasswordErrorRepeatedChar,
setPasswordErrorIsEmail,
setPasswordErrorIsBreachedPassword,
errorCheck
}: PasswordCheckProps) => {
@@ -93,6 +96,14 @@ const passwordCheck = async ({
setPasswordErrorRepeatedChar(false);
}
// isEmail
if (isEmail(password)) {
setPasswordErrorIsEmail(true);
errorCheck = true;
} else {
setPasswordErrorIsEmail(false);
}
// breachedPassword
if (await checkIsPasswordBreached(password)) {
setPasswordErrorIsBreachedPassword(true);
@@ -1,3 +1,4 @@
import isEmail from "validator/lib/isEmail";
import { checkIsPasswordBreached } from "./checkIsPasswordBreached";
type Errors = {
@@ -8,6 +9,7 @@ type Errors = {
number?: string;
specialChar?: string;
repeatedChar?: string;
isEmail?: string;
isBreachedPassword?: string;
};
@@ -25,6 +27,7 @@ interface CheckPasswordParams {
* - Contains at least 1 number (0-9)
* - Contains at least 1 special character
* - Does not contain 3 repeat, consecutive characters
* - Is not an email address
* - Is not in a database of breached passwords
*
* The function returns whether or not the password [password]
@@ -64,7 +67,7 @@ const checkPassword = async ({ password, setErrors }: CheckPasswordParams): Prom
// number
if (!/[0-9]/.test(password)) {
errors.number = "at least 1 number (0-9)";
errors.number = "at least 1 number";
}
// specialChar
@@ -76,7 +79,7 @@ const checkPassword = async ({ password, setErrors }: CheckPasswordParams): Prom
)
) {
errors.specialChar =
'at least 1 special character from !@#$%^&*(),.?":{}|<> or many languages including Korean, Devanagari, Cyrillic, Turkish and emojis.';
"at least 1 special character (emojis and many langauge scripts supported)";
}
// repeatedChar
@@ -87,7 +90,12 @@ const checkPassword = async ({ password, setErrors }: CheckPasswordParams): Prom
password
)
) {
errors.repeatedChar = "At most 2 repeated, consecutive characters";
errors.repeatedChar = "at most 2 repeated, consecutive characters";
}
// isEmail
if (isEmail(password)) {
errors.isEmail = "The password cannot be an email address";
}
// breachedPassword