Next.js template
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PasswordStrength.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import React, { useEffect, useRef, useState } from 'react';
  2. import PropTypes from 'prop-types';
  3. import owasp from 'owasp-password-strength-test';
  4. import i18next from 'i18next';
  5. owasp.config({
  6. minOptionalTestsToPass: 3,
  7. });
  8. const passwordStrengthOptions = [
  9. {
  10. strength: 'weak',
  11. color: '#FF5028',
  12. },
  13. {
  14. strength: 'average',
  15. color: '#FDB942',
  16. },
  17. {
  18. strength: 'good',
  19. color: '#06BEE7',
  20. },
  21. {
  22. strength: 'strong',
  23. color: '#00876A',
  24. },
  25. ];
  26. /**
  27. * User must pass a required test and at least 3 optional.
  28. * @param result - owasp result
  29. * @returns {number} - index of password strength 0-3
  30. */
  31. function getPasswordStrengthIndex(result) {
  32. // requirement for strong password is required test passed and at least 3 optional tests
  33. if (result.strong) {
  34. return 3;
  35. }
  36. if (!result.strong && result.optionalTestsPassed >= 3) {
  37. return 2;
  38. }
  39. if (result.optionalTestsPassed <= 0) {
  40. return 0;
  41. }
  42. return result.optionalTestsPassed - 1;
  43. }
  44. const PasswordStrength = ({
  45. shouldTestPasswordStrength,
  46. passwordValue,
  47. passwordStrengthTestsRequired,
  48. }) => {
  49. const strengthContainer = useRef(null);
  50. const [passwordStrength, setPasswordStrength] = useState({
  51. width: 0,
  52. color: 'red',
  53. });
  54. const [error, setError] = useState('');
  55. useEffect(() => {
  56. if (shouldTestPasswordStrength && passwordValue) {
  57. const bBox = strengthContainer.current.getBoundingClientRect();
  58. const result = owasp.test(passwordValue);
  59. const passwordStrengthIndex = getPasswordStrengthIndex(result);
  60. const passwordOption = passwordStrengthOptions[passwordStrengthIndex];
  61. const width = !passwordValue
  62. ? 0
  63. : (bBox.width * (passwordStrengthIndex + 1)) /
  64. passwordStrengthTestsRequired;
  65. setPasswordStrength({ width, color: passwordOption.color });
  66. const strength = i18next.t(`password.${passwordOption.strength}`);
  67. setError(i18next.t('login.passwordStrength', { strength }));
  68. }
  69. }, [
  70. passwordValue,
  71. shouldTestPasswordStrength,
  72. passwordStrengthTestsRequired,
  73. ]);
  74. if (!shouldTestPasswordStrength || !passwordValue) {
  75. return null;
  76. }
  77. const renderError = () => {
  78. if (!error) {
  79. return null;
  80. }
  81. return (
  82. <div
  83. className="c-input--error"
  84. style={{
  85. color: passwordStrength.color,
  86. }}
  87. >
  88. {error}
  89. </div>
  90. );
  91. };
  92. return (
  93. <div ref={strengthContainer} className="c-password-strength__container">
  94. <div className="c-password-strength__line--wrapper">
  95. <div
  96. className="c-password-strength__line"
  97. style={{
  98. backgroundColor: passwordStrength.color,
  99. width: passwordStrength.width,
  100. }}
  101. />
  102. </div>
  103. {renderError()}
  104. </div>
  105. );
  106. };
  107. PasswordStrength.propTypes = {
  108. shouldTestPasswordStrength: PropTypes.bool,
  109. passwordValue: PropTypes.string,
  110. passwordStrengthTestsRequired: PropTypes.number,
  111. };
  112. PasswordStrength.defaultProps = {
  113. passwordStrengthTestsRequired: 4,
  114. };
  115. export default PasswordStrength;