use-password-policy 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/Type.ts +4 -0
- package/package.json +1 -1
- package/use-password-policy.ts +34 -17
package/README.md
CHANGED
package/Type.ts
CHANGED
package/package.json
CHANGED
package/use-password-policy.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { useState, useEffect, useMemo } from "react";
|
|
2
|
-
import { CustomPolicy, StateObjects } from "./Type";
|
|
3
|
-
|
|
2
|
+
import { CustomPolicy, StateObjects, DefaultConfig } from "./Type";
|
|
4
3
|
|
|
5
4
|
export const usePasswordPolicy = ({
|
|
6
5
|
password,
|
|
@@ -13,15 +12,15 @@ export const usePasswordPolicy = ({
|
|
|
13
12
|
customPolicies?: CustomPolicy[];
|
|
14
13
|
useDefaultConfig?: boolean;
|
|
15
14
|
}) => {
|
|
16
|
-
const defaultConfig = {
|
|
15
|
+
const defaultConfig: DefaultConfig = {
|
|
17
16
|
caseCheck: true,
|
|
18
17
|
lengthCheck: true,
|
|
19
18
|
digitCheck: true,
|
|
20
19
|
specialCharCheck: true,
|
|
21
|
-
minLength: 8,
|
|
22
|
-
uppercaseCharRegex: /[A-Z]/,
|
|
20
|
+
minLength: 8,
|
|
21
|
+
uppercaseCharRegex: /[A-Z]/,
|
|
23
22
|
digitRegex: /\d/,
|
|
24
|
-
specialCharRegex: /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?`~]/,
|
|
23
|
+
specialCharRegex: /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?`~]/,
|
|
25
24
|
};
|
|
26
25
|
|
|
27
26
|
const merged = useMemo(() => mergedConfig(defaultConfig, config), []);
|
|
@@ -36,12 +35,22 @@ export const usePasswordPolicy = ({
|
|
|
36
35
|
|
|
37
36
|
const [policy, setPolicy] = useState<StateObjects>(() => {
|
|
38
37
|
if (useDefaultConfig) {
|
|
39
|
-
|
|
38
|
+
const defaultStates: StateObjects = {
|
|
40
39
|
caseCheck: false,
|
|
41
40
|
lengthCheck: false,
|
|
42
41
|
digitCheck: false,
|
|
43
42
|
specialCharCheck: false,
|
|
44
|
-
|
|
43
|
+
};
|
|
44
|
+
const safeConfig: StateObjects = config || {};
|
|
45
|
+
|
|
46
|
+
Object.entries(defaultStates).map(([key]) => {
|
|
47
|
+
if (safeConfig.hasOwnProperty(key) && safeConfig[key] === false) {
|
|
48
|
+
delete defaultStates[key];
|
|
49
|
+
delete defaultConfig[key];
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
return {
|
|
53
|
+
...defaultStates,
|
|
45
54
|
...customPolicies.reduce(
|
|
46
55
|
(acc, policy: { name: string }) => ({
|
|
47
56
|
...acc,
|
|
@@ -64,16 +73,24 @@ export const usePasswordPolicy = ({
|
|
|
64
73
|
|
|
65
74
|
const evaluateChecks = () => {
|
|
66
75
|
const newPolicy: StateObjects = { ...policy };
|
|
76
|
+
const policyKeys = Object.keys(policy);
|
|
77
|
+
|
|
67
78
|
if (useDefaultConfig) {
|
|
68
|
-
|
|
69
|
-
merged.
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
79
|
+
policyKeys.forEach((key) => {
|
|
80
|
+
if (merged.hasOwnProperty(key) && policy.hasOwnProperty(key)) {
|
|
81
|
+
const checkFunction = {
|
|
82
|
+
caseCheck: () => RegExp(merged.uppercaseCharRegex).test(password),
|
|
83
|
+
lengthCheck: () => password.length >= merged.minLength,
|
|
84
|
+
digitCheck: () => RegExp(merged.digitRegex).test(password),
|
|
85
|
+
specialCharCheck: () =>
|
|
86
|
+
RegExp(merged.specialCharRegex).test(password),
|
|
87
|
+
}[key];
|
|
88
|
+
|
|
89
|
+
if (checkFunction) {
|
|
90
|
+
newPolicy[key] = merged[key] && checkFunction();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
});
|
|
77
94
|
}
|
|
78
95
|
|
|
79
96
|
customPolicies.forEach((customPolicy: CustomPolicy) => {
|