use-password-policy 1.0.0
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/package.json +29 -0
- package/use-password-policy.ts +94 -0
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "use-password-policy",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "use-password-policy.ts",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/rahulpatwa1303/use-password-policy.git"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"password",
|
|
15
|
+
"password-policy",
|
|
16
|
+
"hooks",
|
|
17
|
+
"react"
|
|
18
|
+
],
|
|
19
|
+
"author": "",
|
|
20
|
+
"license": "ISC",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/rahulpatwa1303/use-password-policy/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/rahulpatwa1303/use-password-policy#readme",
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"react": "^18.3.1",
|
|
27
|
+
"react-dom": "^18.3.1"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { useState, useEffect, useMemo } from "react";
|
|
2
|
+
import { CustomPolicy } from "../App";
|
|
3
|
+
|
|
4
|
+
export const usePasswordPolicy = ({
|
|
5
|
+
password,
|
|
6
|
+
config,
|
|
7
|
+
customPolicies = [],
|
|
8
|
+
useDefaultConfig = true,
|
|
9
|
+
}: {
|
|
10
|
+
password: string;
|
|
11
|
+
config?: {};
|
|
12
|
+
customPolicies?: CustomPolicy[];
|
|
13
|
+
useDefaultConfig?: boolean;
|
|
14
|
+
}) => {
|
|
15
|
+
const defaultConfig = {
|
|
16
|
+
caseCheck: true,
|
|
17
|
+
lengthCheck: true,
|
|
18
|
+
digitCheck: true,
|
|
19
|
+
specialCharCheck: true,
|
|
20
|
+
minLength: 8, // Adjust minimum length as needed
|
|
21
|
+
uppercaseCharRegex: /[A-Z]/, // Allow customization of regex patterns
|
|
22
|
+
digitRegex: /\d/,
|
|
23
|
+
specialCharRegex: /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?`~]/, // More inclusive special characters
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const merged = useMemo(() => mergedConfig(defaultConfig, config), []);
|
|
27
|
+
|
|
28
|
+
function mergedConfig(defaultConfig: {}, config: any) {
|
|
29
|
+
if (useDefaultConfig) {
|
|
30
|
+
return { ...defaultConfig, ...config };
|
|
31
|
+
} else {
|
|
32
|
+
return { ...config };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const [policy, setPolicy] = useState<{ [key: string]: boolean }>(() => {
|
|
37
|
+
if (useDefaultConfig) {
|
|
38
|
+
return {
|
|
39
|
+
caseCheck: false,
|
|
40
|
+
lengthCheck: false,
|
|
41
|
+
digitCheck: false,
|
|
42
|
+
specialCharCheck: false,
|
|
43
|
+
// Add properties for custom policies from mergedConfig
|
|
44
|
+
...customPolicies.reduce(
|
|
45
|
+
(acc, policy: { name: string }) => ({
|
|
46
|
+
...acc,
|
|
47
|
+
[policy?.name]: false,
|
|
48
|
+
}),
|
|
49
|
+
{}
|
|
50
|
+
),
|
|
51
|
+
};
|
|
52
|
+
} else
|
|
53
|
+
return {
|
|
54
|
+
...customPolicies.reduce(
|
|
55
|
+
(acc, policy: { name: string }) => ({
|
|
56
|
+
...acc,
|
|
57
|
+
[policy?.name]: false,
|
|
58
|
+
}),
|
|
59
|
+
{}
|
|
60
|
+
),
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const evaluateChecks = () => {
|
|
65
|
+
const newPolicy: { [key: string]: boolean } = { ...policy };
|
|
66
|
+
if (useDefaultConfig) {
|
|
67
|
+
newPolicy.caseCheck =
|
|
68
|
+
merged.caseCheck && RegExp(merged.uppercaseCharRegex).test(password);
|
|
69
|
+
newPolicy.lengthCheck =
|
|
70
|
+
merged.lengthCheck && password.length >= merged.minLength;
|
|
71
|
+
newPolicy.digitCheck =
|
|
72
|
+
merged.digitCheck && RegExp(merged.digitRegex).test(password);
|
|
73
|
+
newPolicy.specialCharCheck =
|
|
74
|
+
merged.specialCharCheck &&
|
|
75
|
+
RegExp(merged.specialCharRegex).test(password);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
customPolicies.forEach((customPolicy: CustomPolicy) => {
|
|
79
|
+
const name: string = customPolicy?.name;
|
|
80
|
+
if (customPolicy?.regex) {
|
|
81
|
+
newPolicy[name] = customPolicy.regex.test(password);
|
|
82
|
+
} else if (customPolicy?.checkFunction) {
|
|
83
|
+
newPolicy[name] = customPolicy.checkFunction(password);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
setPolicy(newPolicy);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
evaluateChecks();
|
|
92
|
+
}, [password]);
|
|
93
|
+
return policy;
|
|
94
|
+
};
|