1.0.7 • Published 1 year ago

use-password-policy v1.0.7

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

usePasswordPolicy Hook

This React hook simplifies password policy enforcement in your application. It provides a flexible and customizable way to validate passwords against various criteria, ensuring strong password security.

Features:

  • Built-in Policies: Enforces common password complexity requirements like minimum length, case sensitivity, digit inclusion, and special characters.

  • Custom Policies: Define your own validation checks using regular expressions or custom functions for even more granular control.

  • Configurable Defaults: Specify a default configuration for common policies, or override them with custom settings.

Installation:

npm install use-password-policy

Usage:

import { usePasswordPolicy } from './use-password-policy';
function MyComponent() {
	const [password, setPassword] = useState('');
	const policy = usePasswordPolicy({
						password,
						config: { minLength: 12 },
						customPolicies: [{ name: 'noRepeatedChars', regex: /^(?!.*(.)\1)/ },],});
	const isPasswordValid = Object.values(policy).every(check => check);
	
	return (
		<form>
			<input
				type="password"
				value={password}
				onChange={e => setPassword(e.target.value)}
			/>
		{isPasswordValid ? (
			<p>Password is strong!</p>
		) : (
			<ul>
				{Object.entries(policy).map(([key, value]) => (
				<li key={key}>{!value && key.replace(/([A-Z])/g, ' $1')}</li>
				))}
			</ul>
			)}
		</form>
	);
}

Props:

Prop NameProps ValuesTypeDescriptionDefault Value
passwordstringThe password to be validated.Required
configobject (optional)An object overriding default configuration for built-in policies.
- minLengthnumberMinimum password length8
- uppercaseCharRegexRegExpRegular expression for uppercase characters/A-Z/
- digitRegexRegExpRegular expression for digits/\d/
- specialCharRegexRegExpRegular expression for special characters/[!@#$%^&*()_+-=[]{};':"|
- caseCheckbooleanThis flag determines the availability of a feature or functionality within your application's statetrue
- lengthCheckbooleanThis flag determines the availability of a feature or functionality within your application's statetrue
- digitCheckbooleanThis flag determines the availability of a feature or functionality within your application's statetrue
- specialCharCheckbooleanThis flag determines the availability of a feature or functionality within your application's statetrue
customPoliciesarray of objects (optional)An array of custom policies to enforce.
- namestringName of the custom policy for clarity in feedback.
- regexRegExp (optional)Regular expression for custom validation.
- checkFunctionfunction (optional)A function taking the password as an argument and returning true/false for a custom check.
useDefaultConfigbooleanThis flag controls whether to use the default configuration for validations or checks in your application.true

Return Value:

An object containing boolean values for each policy check (built-in and custom). Use Object.values(policy).every(check => check) to determine if all policies are satisfied.

Benefits:

  • Improved Security: Enforces strong passwords, reducing the risk of brute-force attacks and data breaches.
  • Enhanced User Experience: Provides clear feedback to users on password strength, guiding them towards creating secure passwords.
  • Customization: Adapts to your specific security requirements through configurable defaults and custom policies.
1.0.7

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago