Skip to content

Commit 715e81d

Browse files
committed
feat: add password strength indicator UI
1 parent ad00dd8 commit 715e81d

4 files changed

Lines changed: 65 additions & 1 deletion

File tree

client/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"react-redux": "*",
3636
"react-scripts": "*",
3737
"recompose": "*",
38-
"typeface-roboto": "*"
38+
"typeface-roboto": "*",
39+
"zxcvbn": "^4.4.2"
3940
},
4041
"scripts": {
4142
"start": "cross-env PORT=3001 react-scripts start",

client/src/components/Account.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import TextField from '@material-ui/core/TextField'
1414
import DialogContentText from '@material-ui/core/DialogContentText'
1515
import DialogTitle from '@material-ui/core/DialogTitle'
1616
import { AppContext } from './MainFrame'
17+
import PasswordStrengthMeter from './PasswordStrengthMeter'
18+
1719
import axios from 'axios'
1820

1921
const style = (theme) => ({
@@ -74,6 +76,9 @@ function Account(props) {
7476
const handleClose = () => {
7577
setOpenPwdForm(false)
7678
setErrorMessage('')
79+
setOldPassword('')
80+
setNewPassword('')
81+
setConfirmedPassword('')
7782
}
7883

7984
const onChangeOldPwd = (e) => {
@@ -264,6 +269,7 @@ function Account(props) {
264269
onChange={onChangeNewPwd}
265270
value={newPassword}
266271
/>
272+
<PasswordStrengthMeter password={newPassword} />
267273
<TextField
268274
variant="outlined"
269275
margin="normal"
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React from 'react'
2+
import zxcvbn from 'zxcvbn'
3+
import { withStyles } from '@material-ui/core/styles'
4+
import LinearProgress from '@material-ui/core/LinearProgress'
5+
import Typography from '@material-ui/core/Typography'
6+
import classNames from 'classnames'
7+
8+
const styles = (theme) => ({
9+
alert: {
10+
color: theme.palette.primary.main,
11+
},
12+
})
13+
14+
const PasswordStrengthMeter = (props) => {
15+
const { password, classes } = props
16+
17+
const pwdScore = zxcvbn(password).score
18+
19+
const pwdAlert = (pwdScore) => {
20+
switch (pwdScore) {
21+
case 0:
22+
return 'Too weak'
23+
case 1:
24+
return 'Weak'
25+
case 2:
26+
return 'Fair'
27+
case 3:
28+
return 'Good'
29+
case 4:
30+
return 'Strong'
31+
default:
32+
return 'Weak'
33+
}
34+
}
35+
36+
return (
37+
<>
38+
{password && (
39+
<>
40+
<LinearProgress
41+
variant="determinate"
42+
color={classNames(pwdScore > 1 ? 'primary' : 'secondary')}
43+
value={pwdScore * 25}
44+
/>
45+
<Typography className={classes.alert}>{pwdAlert(pwdScore)}</Typography>
46+
</>
47+
)}
48+
</>
49+
)
50+
}
51+
52+
export default withStyles(styles)(PasswordStrengthMeter)

0 commit comments

Comments
 (0)