-
Notifications
You must be signed in to change notification settings - Fork 16
/
password-strenght-meter.tsx
70 lines (62 loc) · 1.64 KB
/
password-strenght-meter.tsx
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
import * as React from 'react';
import clsx from 'clsx';
import zxcvbn from 'zxcvbn';
import { Typography, LinearProgress, colors } from '@material-ui/core';
import { makeStyles, Theme } from '@material-ui/core/styles';
const useStyles = makeStyles((theme: Theme) => ({
progress: {
backgroundColor: colors.grey[300],
height: '8px',
},
weak: {
backgroundColor: theme.palette.error.dark,
},
fair: {
backgroundColor: colors.yellow[500],
},
good: {
backgroundColor: colors.blue[500],
},
strong: {
backgroundColor: colors.green[500],
},
}));
const mapScoreLabel = (score: number) => {
switch (score) {
case 0:
return 'weak';
case 1:
return 'weak';
case 2:
return 'fair';
case 3:
return 'good';
case 4:
return 'strong';
default:
return 'weak';
}
};
export interface PasswordStrengthMeterProps {
className?: string;
password: string;
}
export const PasswordStrengthMeter: React.FC<
PasswordStrengthMeterProps
> = props => {
const { className, password } = props;
const classes = useStyles();
const { score } = zxcvbn(password);
const scoreLabel = mapScoreLabel(score);
return (
<>
<LinearProgress
className={clsx(classes.progress, className)}
classes={{ barColorPrimary: classes[scoreLabel] }}
variant="determinate"
value={100 / (4 / score)}
/>
<Typography>Password strength: {scoreLabel}</Typography>
</>
);
};