-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add password requirements to login page (#103)
closes #90 Would appreciate some feedback on placement and styling - wanted to fit it into the `Label` element but thought it might be better after the form field
- Loading branch information
1 parent
36999ce
commit 7eebda6
Showing
2 changed files
with
87 additions
and
2 deletions.
There are no files selected for viewing
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,70 @@ | ||
import { useMemo } from 'react'; | ||
|
||
const checkLength = (password: string) => password.length >= 12; | ||
const checkOneUpper = (password: string) => /[A-Z]+/.test(password); | ||
const checkOneLower = (password: string) => /[a-z]+/.test(password); | ||
const checkOneNumber = (password: string) => /\d+/.test(password); | ||
const checkOneSpecial = (password: string) => /\W+/.test(password); | ||
|
||
export const PasswordCheck = (opts: { password: string }) => { | ||
const password = opts.password; | ||
return ( | ||
<div> | ||
<div> | ||
<CheckOrX handler={checkLength} password={password}> | ||
minimum 12 characters | ||
</CheckOrX> | ||
</div> | ||
<div> | ||
<CheckOrX handler={checkOneUpper} password={password}> | ||
at least 1 uppercase | ||
</CheckOrX> | ||
</div> | ||
<div> | ||
<CheckOrX handler={checkOneLower} password={password}> | ||
at least 1 lowercase | ||
</CheckOrX> | ||
</div> | ||
<div> | ||
<CheckOrX handler={checkOneNumber} password={password}> | ||
at least 1 number | ||
</CheckOrX> | ||
</div> | ||
<div> | ||
<CheckOrX handler={checkOneSpecial} password={password}> | ||
at least 1 special character | ||
</CheckOrX> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export const CheckOrX = ({ | ||
handler, | ||
password, | ||
children, | ||
}: { | ||
handler: (password: string) => boolean; | ||
password: string | { password: string | null }; | ||
children: React.ReactNode; | ||
}) => { | ||
let actualPassword = ''; | ||
if (typeof password === 'string') { | ||
actualPassword = password; | ||
} else { | ||
actualPassword = password.password ?? ''; | ||
} | ||
const isValid = useMemo( | ||
() => handler(actualPassword), | ||
[handler, actualPassword], | ||
); | ||
return ( | ||
<span className={isValid ? 'text-success' : 'text-danger'}> | ||
{isValid ? <Check /> : <XShape />} {children} | ||
</span> | ||
); | ||
}; | ||
|
||
const Check = () => <i className={'bi bi-check2'}></i>; | ||
|
||
const XShape = () => <i className={'bi bi-x'}></i>; |