PasswordStrengthBar.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { InlineLinkClassName } from './InlineLink'
  2. import { PASSWORD_STRENGTH_COLOR, PASSWORD_STRENGTH_PERCENTAGE } from '@/lib/constants'
  3. import { PasswordStrengthScore } from '@/lib/password-strength'
  4. interface Props {
  5. passwordStrengthScore: PasswordStrengthScore
  6. passwordStrengthMessage: string
  7. password: string
  8. generateStrongPassword: () => void
  9. }
  10. export const PasswordStrengthBar = ({
  11. passwordStrengthScore = 0,
  12. passwordStrengthMessage = '',
  13. password = '',
  14. generateStrongPassword,
  15. }: Props) => {
  16. return (
  17. <>
  18. {password && (
  19. <div
  20. aria-valuemax={100}
  21. aria-valuemin={0}
  22. aria-valuenow={PASSWORD_STRENGTH_PERCENTAGE[passwordStrengthScore]}
  23. aria-valuetext={`${PASSWORD_STRENGTH_PERCENTAGE[passwordStrengthScore]}%`}
  24. role="progressbar"
  25. className="mb-2 overflow-hidden transition-all border rounded-sm bg-200 w-full"
  26. >
  27. <div
  28. style={{
  29. width: `${PASSWORD_STRENGTH_PERCENTAGE[passwordStrengthScore]}%`,
  30. }}
  31. className={`relative h-1 w-full ${PASSWORD_STRENGTH_COLOR[passwordStrengthScore]} transition-all duration-500 ease-out shadow-inner`}
  32. />
  33. </div>
  34. )}
  35. <p>
  36. {(passwordStrengthMessage
  37. ? passwordStrengthMessage
  38. : 'This is the password to your Postgres database, so it must be strong and hard to guess.') +
  39. ' '}
  40. <button type="button" className={InlineLinkClassName} onClick={generateStrongPassword}>
  41. Generate a password
  42. </button>
  43. .
  44. </p>
  45. </>
  46. )
  47. }