-
Notifications
You must be signed in to change notification settings - Fork 37
/
PasswordStrength.js
162 lines (144 loc) · 4.1 KB
/
PasswordStrength.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import camelCase from 'lodash/camelCase';
import noop from 'lodash/noop';
import { FormattedMessage } from 'react-intl';
import TextField from '../TextField';
import InfoPopover from '../InfoPopover';
import {
Col,
Row,
} from '../LayoutGrid';
import css from './PasswordStrength.css';
const passwordStrengthTypes = ['VERY_WEAK', 'WEAK', 'REASONABLE', 'STRONG', 'VERY_STRONG'];
const defaultPasswordStrengthType = camelCase(passwordStrengthTypes[0]);
class PasswordStrength extends Component {
static propTypes = {
input: PropTypes.shape({
value: PropTypes.string,
}),
/**
* Properties for column that includes textInput
*/
inputColProps: PropTypes.object,
meta: PropTypes.shape({
dirty: PropTypes.bool,
valid: PropTypes.bool,
}),
/**
* Properties for column that includes password strength meter
*/
passwordMeterColProps: PropTypes.object,
/**
* Is password strength meter hidden
*/
passwordStrengthHidden: PropTypes.bool
};
static defautlProps = {
inputColProps: {},
passwordMeterColProps: {},
passwordStrengthHidden: false,
};
constructor(props) {
super(props);
this.state = {
hasLoaded: false,
};
this.strengthTester = {
check: noop,
};
this.outputTypes = this.getOutputTypes();
}
componentDidMount() {
this._isMounted = true;
this.initPasswordStrength();
}
componentWillUnmount() {
this._isMounted = false;
}
initPasswordStrength() {
import('tai-password-strength').then(taiPasswordStrength => {
this.strengthTester = new taiPasswordStrength.PasswordStrength();
this.strengthTester.addCommonPasswords(taiPasswordStrength.commonPasswords);
this.strengthTester.addTrigraphMap(taiPasswordStrength.trigraphs);
if (this._isMounted) {
this.setState({ hasLoaded: true });
}
});
}
getOutputTypes() {
return passwordStrengthTypes.reduce((result, element) => {
return {
...result,
[element]: camelCase(element),
};
}, {});
}
render() {
const {
meta: {
valid,
dirty,
},
input: { value },
} = this.props;
const {
inputColProps,
passwordMeterColProps,
passwordStrengthHidden,
...rest
} = this.props;
const { hasLoaded } = this.state;
const isVisible = valid && !passwordStrengthHidden && dirty;
const { strengthCode } = (hasLoaded && this.strengthTester.check(value)) || {};
const output = this.outputTypes[strengthCode] || defaultPasswordStrengthType;
return (
<Row>
<Col
xs={6}
{...inputColProps}
>
<TextField {...rest} />
</Col>
<Col
xs={4}
aria-live="polite"
{...passwordMeterColProps}
>
{isVisible &&
<div className="password-strength">
<div className={css['password-strength__label']}>
<FormattedMessage
id="stripes-components.passwordStrength.label"
/>
</div>
<div
className={classNames(css.indicator__container, css[`indicator__container--${output}`])}
>
<div className={css.indicator__item} />
<div className={css.indicator__item} />
<div className={css.indicator__item} />
<div className={css.indicator__item} />
<div className={css.indicator__item} />
</div>
<div className={css['password-strength-text__wrapper']}>
<FormattedMessage
id={`stripes-components.passwordStrength.${output}`}
/>
<InfoPopover
content={
<FormattedMessage
id="stripes-components.passwordStrength.infoPopoverText"
/>
}
/>
</div>
</div>
}
</Col>
</Row>
);
}
}
export default PasswordStrength;