custom-password-strength-checker
v1.0.0
Published
check-password-strength is a customizable npm package to assess the strength of a password based on specific requirements. It provides feedback on password strength, suggestions for improvement, and allows for custom messages to fit your application's nee
Readme
Check Password Strength
checkPasswordStrength
is a customizable npm package to assess the strength of a password based on specific requirements. It provides feedback on password strength, suggestions for improvement, and allows for custom messages to fit your application's needs.
Installation
npm install check-password-strength
Usage
Import the checkPasswordStrength
function and call it with a password string and an optional configuration object.
Example
const checkPasswordStrength = require("check-password-strength");
const feedback = checkPasswordStrength("Test@123", {
minLength: 10,
requireNumbers: true,
requireSpecialChars: true,
requireUppercase: true,
requireLowercase: true,
disallowCommonPasswords: true,
customMessages: {
minLength: "Password should be at least 10 characters long.",
requireNumbers: "Include at least one numerical digit.",
commonPassword:
"This password is too commonly used, choose something unique.",
},
});
console.log(feedback);
Output:
The function returns an object with the following properties:
{
"score": 4,
"strength": "Strong",
"suggestions": ["Password should be at least 10 characters long."]
}
Options
The checkPasswordStrength
function accepts a configuration object with the following options:
- minLength (default:
8
): Minimum length required for the password. - requireNumbers (default:
true
): Requires at least one number in the password. - requireSpecialChars (default:
true
): Requires at least one special character. - requireUppercase (default:
true
): Requires at least one uppercase letter. - requireLowercase (default:
true
): Requires at least one lowercase letter. - disallowCommonPasswords (default:
false
): Prevents the use of commonly used passwords like"123456"
,"password"
, etc. - customMessages (default:
{}
): Custom messages for different requirements. Useful for localization or personalization.
Feedback Structure
The feedback object returned by checkPasswordStrength
contains:
- score: A numeric score based on the password's compliance with the specified criteria.
- strength: A descriptive strength level based on the score:
Weak
(score <= 2)Moderate
(score === 3)Strong
(score === 4)Very Strong
(score >= 5)
- suggestions: An array of strings with suggestions for making the password stronger based on the unmet criteria.
Custom Error Messages
You can customize error messages using the customMessages
object in the options. The custom messages object can have the following keys:
- minLength: Custom message when the password does not meet the minimum length.
- requireNumbers: Custom message when a number is required but not included.
- requireSpecialChars: Custom message when a special character is required but not included.
- requireUppercase: Custom message when an uppercase letter is required but not included.
- requireLowercase: Custom message when a lowercase letter is required but not included.
- commonPassword: Custom message when the password is flagged as too common.
Example with Custom Messages
const feedback = checkPasswordStrength("password123", {
minLength: 12,
disallowCommonPasswords: true,
customMessages: {
minLength: "Please make your password at least 12 characters long.",
commonPassword:
"This password is too commonly used, please choose another one.",
},
});
console.log(feedback);
Output:
{
"score": 2,
"strength": "Weak",
"suggestions": [
"Please make your password at least 12 characters long.",
"This password is too commonly used, please choose another one."
]
}
License
MIT License
This markdown is ready for use as a `README.md` file. It provides clear instructions, usage examples, and details on customization options for your password strength checker package.