forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpasswordInput.js
More file actions
39 lines (34 loc) · 1.26 KB
/
passwordInput.js
File metadata and controls
39 lines (34 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { passwordStrength } from 'check-password-strength';
import { debounce, randomPassword } from './helpers';
// Adds listeners to password inputs (to monitor strength) and generate password buttons
export default function handlePasswordInput() {
// Listen for changes to password inputs and update the password strength
document.querySelectorAll('.js-password-input').forEach((passwordInput) => {
passwordInput.addEventListener(
'input',
debounce((evt) => recalculatePasswordStrength(evt.target)),
);
});
// Listen for clicks on generate password buttons and set a new random password
document.querySelectorAll('.js-generate-password').forEach((generatePasswordButton) => {
generatePasswordButton.addEventListener('click', () => {
const passwordInput =
generatePasswordButton.parentNode.nextElementSibling.querySelector('.js-password-input');
if (passwordInput) {
passwordInput.value = randomPassword();
passwordInput.dispatchEvent(new Event('input'));
}
});
});
}
function recalculatePasswordStrength(input) {
const password = input.value;
const meter = input.parentNode.querySelector('.js-password-meter');
if (meter) {
if (password === '') {
meter.value = 0;
return;
}
meter.value = passwordStrength(password).id + 1;
}
}