Skip to content

Commit

Permalink
Fixed error showing error mesage when password is empty. New setting …
Browse files Browse the repository at this point in the history
…to show all errors found instead the first one. Updated docs.
  • Loading branch information
Raúl Polanco Montiel committed Jun 17, 2021
1 parent 0d21c08 commit 2717b1b
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 48 deletions.
53 changes: 48 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Here is a list of the settings currently available:
Setting | Default | Description
---------------------------- | ----------- | -------------------------------------------------------------
debug | `false` | Enable debug mode
allErrors | `false` | Show all errors found as an array instead the first one as string
minUpperChars | `1` | Minimum uppercase characters required `[A-Z]`.
minLowerChars | `1` | Minimum lowercase characters required `[a-z]`.
minNumberChars | `1` | Minimum numbers required `[0-9]`.
Expand All @@ -36,6 +37,7 @@ weak | *Weak*
average | *Average*
strong | *Strong*
secure | *Secure*
enterPassword | *Enter a password*
minPasswordChar | *At least %s character please*
minPasswordChars | *At least %s characters please*
minLowerChar | *At least %s lowercase character please*
Expand Down Expand Up @@ -70,16 +72,47 @@ secure | <span style="color:#007000;">#007000</span>

The function returns an object as follows:

### Error response

```js
{
"success": false, // any error occurs
"key": "error", // Indicates error
"message": "First error message found", // Multilanguage string with the error message
"color": "#rrggbb" // Hex color depending on the key
}
```

### Error response with `allErrors: true` setting

```js
{
"success": false, // any error occurs
"key": "error", // Indicates error
"message": ["Error message 1", "Error message 2", "Error message 3", ...], // Multilanguage array of strings with the error messages
"color": "#rrggbb" // Hex color depending on the key
}
```

### Success response

```js
{
"success":false|true, // false if any error occurs, otherwise true
"key":"error|weak|average|strong|secure", // Indicates error or the strength
"message":"Error message or strength", // Multilanguage string with the error message or strength
"color":"#rrggbb" // Hex color depending on the key
"success": true, // no errors
"key": "weak|average|strong|secure", // Indicates the strength
"message": "Strength", // Multilanguage string with the strength
"color": "#rrggbb" // Hex color depending on the key
}
```

## Examples
## Error examples

Default settings, the first error checked will be the empty password:

```js
console.log(passwordStrength(''));
// {"success":false,"key":"error","message":"Enter a password","color":"#ee0000"}
```

Default settings, the first error checked will be the password length:

Expand Down Expand Up @@ -123,6 +156,16 @@ Maximum consecutive repeating characters allowed:
// {"success":false,"key":"error","message":"No more than 2 consecutive repeating characters or numbers please","color":"#ee0000"}
```

With `allErrors: true` setting and the empty password, all possible errors will be shown, only in an error occurs (`success: false` in the response).
This is the only case where the `message` property will be an array of strings.

```js
console.log(passwordStrength('', { allErrors: true }));
// {"success":false,"key":"error","message":["Enter a password","At least 8 characters please","At least 1 lowercase character please","At least 1 uppercase character please","At least 1 number please","At least 1 special character please"],"color":"#ee0000"}
```

## Success examples

Success result, with weak password:

```js
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ function test(title, password, options = {}) {
process.stdout.write('\n');
}

test('Empty password', '');
test('Min password length', 'test');
test('Min upper chars', 'testtest', { minUpperChars: 1 });
test('Min lower chars', 'TESTTEST', { minLowerChars: 1 });
test('Min numbers', 'TestTest', { minNumberChars: 1 });
test('Min special chars', 'TestTest1', { minSpecialChars: 1 });
test('Max consecutive repeating chars', 'TestTTTTest1!', { maxConsecutiveRepeatingChars: 2 });
test('Empty password with all possible errors', '', { allErrors: true });
test('Weak password', 'testtest', { minUpperChars: 0, minNumberChars: 0, minSpecialChars: 0 });
test('Average password', 'testest1', { minUpperChars: 0, minNumberChars: 0, minSpecialChars: 0 });
test('Strong password', 'TestTest1!');
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"#USER",
"/"
],
"_resolved": "https://registry.npmjs.org/pwd-strength/-/pwd-strength-1.0.6.tgz",
"_resolved": "https://registry.npmjs.org/pwd-strength/-/pwd-strength-1.1.1.tgz",
"_shasum": "1ccc118c7a90b02d22c3ae1815af237321e32878",
"_spec": "pwd-strength",
"_where": "C:\\nodejs\\pwd-strength",
Expand Down Expand Up @@ -57,5 +57,5 @@
"demo": "node ./index.js",
"test": "tape ./test/*spec.js | tap-spec"
},
"version": "1.0.6"
"version": "1.1.1"
}
98 changes: 57 additions & 41 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = function passwordStrength(value, options) {

let defaults = {
debug: false,
allErrors: false, // Return all errors instead the first error found
minUpperChars: 1, // Upper characters ([A-Z])
minLowerChars: 1, // Lower characters ([a-z])
minNumberChars: 1, // Numbers ([0-9])
Expand Down Expand Up @@ -148,57 +149,68 @@ module.exports = function passwordStrength(value, options) {
let result = {
success: false,
key: 'error',
message: '',
message: [],
color: ''
};

if (value === '') {
result.message = settings.enterPassword;
if(strPassword === '') {
let message = lang.enterPassword;
result.message.push(message);
result.color = colors.error;
} else if (charPassword.length < settings.minPasswordLength) {
result.message = settings.minPasswordLength !== 1 ? lang.minPasswordChars : lang.minPasswordChar;
result.message = format(result.message, settings.minPasswordLength);
}
if(charPassword.length < settings.minPasswordLength) {
let message = settings.minPasswordLength !== 1 ? lang.minPasswordChars : lang.minPasswordChar;
result.message.push(format(message, settings.minPasswordLength));
result.color = colors.error;
} else if (num.lower < settings.minLowerChars) {
result.message = settings.minLowerChars !== 1 ? lang.minLowerChars : lang.minLowerChar;
result.message = format(result.message, settings.minLowerChars);
}
if(num.lower < settings.minLowerChars) {
let message = settings.minLowerChars !== 1 ? lang.minLowerChars : lang.minLowerChar;
result.message.push(format(message, settings.minLowerChars));
result.color = colors.error;
} else if (num.upper < settings.minUpperChars) {
result.message = settings.minUpperChars !== 1 ? lang.minUpperChars : lang.minUpperChar;
result.message = format(result.message, settings.minUpperChars);
}
if(num.upper < settings.minUpperChars) {
let message = settings.minUpperChars !== 1 ? lang.minUpperChars : lang.minUpperChar;
result.message.push(format(message, settings.minUpperChars));
result.color = colors.error;
} else if (num.numbers < settings.minNumberChars) {
result.message = settings.minNumberChars !== 1 ? lang.minNumberChars : lang.minNumberChar;
result.message = format(result.message, settings.minNumberChars);
}
if(num.numbers < settings.minNumberChars) {
let message = settings.minNumberChars !== 1 ? lang.minNumberChars : lang.minNumberChar;
result.message.push(format(message, settings.minNumberChars));
result.color = colors.error;
} else if (num.symbols < settings.minSpecialChars) {
result.message = settings.minSpecialChars !== 1 ? lang.minSpecialChars : lang.minSpecialChar;
result.message = format(result.message, settings.minSpecialChars);
}
if(num.symbols < settings.minSpecialChars) {
let message = settings.minSpecialChars !== 1 ? lang.minSpecialChars : lang.minSpecialChar;
result.message.push(format(message, settings.minSpecialChars));
result.color = colors.error;
} else if (settings.maxConsecutiveRepeatingChars > 1 && num.repeatingChars > settings.maxConsecutiveRepeatingChars) {
result.message = lang.maxConsecutiveRepeatingChars;
result.message = format(result.message, settings.maxConsecutiveRepeatingChars);
}
if(settings.maxConsecutiveRepeatingChars > 1 && num.repeatingChars > settings.maxConsecutiveRepeatingChars) {
let message = lang.maxConsecutiveRepeatingChars;
result.message.push(format(message, settings.maxConsecutiveRepeatingChars));
result.color = colors.error;
} else if (score < 50) {
result.success = true;
result.key = 'weak';
result.message = lang.weak;
result.color = colors.weak;
} else if (score >= 50 && score < 75) {
result.success = true;
result.key = 'average';
result.message = lang.average;
result.color = colors.average;
} else if (score >= 75 && score < 100) {
result.success = true;
result.key = 'strong';
result.message = lang.strong;
result.color = colors.strong;
} else if (score >= 100) {
result.success = true;
result.key = 'secure';
result.message = lang.secure;
result.color = colors.secure;
}

if(result.message.length === 0) {
if (score < 50) {
result.success = true;
result.key = 'weak';
result.message = lang.weak;
result.color = colors.weak;
} else if (score >= 50 && score < 75) {
result.success = true;
result.key = 'average';
result.message = lang.average;
result.color = colors.average;
} else if (score >= 75 && score < 100) {
result.success = true;
result.key = 'strong';
result.message = lang.strong;
result.color = colors.strong;
} else if (score >= 100) {
result.success = true;
result.key = 'secure';
result.message = lang.secure;
result.color = colors.secure;
}
}

if(settings.debug) {
Expand All @@ -217,6 +229,10 @@ module.exports = function passwordStrength(value, options) {
console.log(result.debug);
}

if(!result.success && !settings.allErrors) {
result.message = result.message.length > 0 ? result.message[0] : '';
}

return result;

}
Expand Down

0 comments on commit 2717b1b

Please sign in to comment.