-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword-validator.ts
53 lines (42 loc) · 1.6 KB
/
password-validator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { AuthOptions, AuthOptionsFromJSONTyped } from "../../api";
import { ValidatorResult } from "../types/ValidatorResult";
export const passwordIsValid = (authOptions: AuthOptions, password: string) => {
let isValid = true
const errors: string[] = []
if (password.trim().length < authOptions.password.requiredLength) {
isValid = false;
errors.push(`Min length ${authOptions.password.requiredLength}`)
}
if (authOptions.password.requireLowercase) {
const testAtLeastOneLowercase = /^(?=.*[a-z])/;
if (testAtLeastOneLowercase.test(password) === false) {
isValid = false;
errors.push("At least one lowercase letter");
}
}
if (authOptions.password.requireUppercase) {
const testAtLeastOneUppercase = /^(?=.*[A-Z])/;
if (testAtLeastOneUppercase.test(password) === false) {
isValid = false;
errors.push("At least one uppercase letter");
}
}
if (authOptions.password.requireDigit) {
const testAtLeastOneNumber = /^(?=.*[0-9])/;
if (testAtLeastOneNumber.test(password) === false) {
isValid = false;
errors.push("At least one number");
}
}
if (authOptions.password.requireNonAlphanumeric) {
const testAtLeastOneSymbol = /^(?=.*[~`!@#$%^&*()--+={}\[\]|\\:;"'<>,.?/_₹]).*$/;
if (testAtLeastOneSymbol.test(password) === false) {
isValid = false;
errors.push("At least one symbol");
}
}
return {
isValid: isValid,
errors: errors
} as ValidatorResult
}