-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-type: patch
- Loading branch information
Andrea Rosci
authored and
Andrea Rosci
committed
Mar 4, 2024
1 parent
2d5086a
commit 6339752
Showing
4 changed files
with
70 additions
and
66 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Box, Typography } from '@mui/material'; | ||
import { useState, useEffect } from 'react'; | ||
import zxcvbn from 'zxcvbn'; | ||
|
||
const STRENGTH_TITLES = ['Very weak', 'Weak', 'Good', 'Strong', 'Very strong']; | ||
const STRENGTH_STYLES = [ | ||
{ width: 0 }, | ||
{ width: '25%', backgroundColor: 'orange' }, | ||
{ width: '50%', backgroundColor: 'yellow' }, | ||
{ width: '75%', backgroundColor: 'green' }, | ||
{ width: '100%', backgroundColor: 'darkgreen' }, | ||
]; | ||
|
||
export interface PasswordStrengthProps { | ||
password?: string; | ||
} | ||
|
||
export const PasswordStrength = ({ password }: PasswordStrengthProps) => { | ||
const [strengthScore, setStrengthScore] = useState<number | undefined>(); | ||
|
||
useEffect(() => { | ||
if (password?.length) { | ||
const { score } = zxcvbn(password); | ||
setStrengthScore(score); | ||
} | ||
}, [password]); | ||
|
||
if (!password || strengthScore === undefined) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Box> | ||
<Typography> | ||
Password strength: <em>{STRENGTH_TITLES[strengthScore]}</em> | ||
</Typography> | ||
<Box | ||
sx={[ | ||
STRENGTH_STYLES[strengthScore], | ||
{ height: '4px', transition: 'all ease-out 150ms' }, | ||
]} | ||
/> | ||
</Box> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters