-
Notifications
You must be signed in to change notification settings - Fork 13
/
password-strength-meter.js
82 lines (72 loc) · 2.23 KB
/
password-strength-meter.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import React, {Component,PropTypes} from 'react';
import ReactDom from 'react-dom';
import zxcvbn from 'zxcvbn';
export default class PasswordStrengthMeter extends Component{
constructor(props){
super(props);
this.handleInput = this.handleInput.bind(this);
this.state = {
resultScore: '',
warning: '',
suggestions:''
};
}
handleInput(event){
event.preventDefault();
let { strength } = this.props;
strength = (strength) ? strength : {
0: "Worst ☹",
1: "Bad ☹",
2: "Weak ☹",
3: "Good ☺",
4: "Strong ☻"
}
const password = ReactDom.findDOMNode(this.refs.password);
const meter = ReactDom.findDOMNode(this.refs.passwordStrengthMeter);
const text = ReactDom.findDOMNode(this.refs.passwordStrengthText);
let val = password.value;
let result = zxcvbn(val);
// Update the password strength meter
meter.value = result.score;
// Update the text indicator
if(val !== "") {
this.setState({
resultScore:strength[result.score],
warning:result.feedback.warning,
suggestions:result.feedback.suggestions
});
}
else {
this.setState({
resultScore:'',
warning:'',
suggestions:''
})
}
if(typeof this.props.onChange === 'function'){
this.props.onChange(event);
}
}
render(){
const { passwordText } = this.props;
const passwordHeader = (passwordText) ? passwordText : 'Enter Password';
const {resultScore,warning,suggestions} = this.state;
return(
<section>
<label forHtml="password">{passwordHeader}</label>
<input onInput={this.handleInput} type="password" name="password" id="password" ref="password" />
<meter max="4" id="password-strength-meter" ref="passwordStrengthMeter"></meter>
<p id="password-strength-text" ref="passwordStrengthText">
{resultScore &&
"Strength: "}
<strong>{resultScore}</strong><span className="feedback">{warning + " " + suggestions}</span>
</p>
</section>
)
}
}
PasswordStrengthMeter.propTypes = {
passwordText: React.PropTypes.string,
strength: React.PropTypes.object,
onChange: React.PropTypes.func,
}