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.

79 lines
1.9 KiB

  1. <template lang="pug">
  2. .password-strength
  3. v-progress-linear(
  4. :color='passwordStrengthColor'
  5. v-model='passwordStrength'
  6. height='2'
  7. )
  8. .caption(v-if='!hideText', :class='passwordStrengthColor + "--text"') {{passwordStrengthText}}
  9. </template>
  10. <script>
  11. import zxcvbn from 'zxcvbn'
  12. import _ from 'lodash'
  13. export default {
  14. props: {
  15. value: {
  16. type: String,
  17. default: ''
  18. },
  19. hideText: {
  20. type: Boolean,
  21. default: false
  22. }
  23. },
  24. data() {
  25. return {
  26. passwordStrength: 0,
  27. passwordStrengthColor: 'grey',
  28. passwordStrengthText: ''
  29. }
  30. },
  31. watch: {
  32. value(newValue) {
  33. this.checkPasswordStrength(newValue)
  34. }
  35. },
  36. methods: {
  37. checkPasswordStrength: _.debounce(function (pwd) {
  38. if (!pwd || pwd.length < 1) {
  39. this.passwordStrength = 0
  40. this.passwordStrengthColor = 'grey'
  41. this.passwordStrengthText = ''
  42. return
  43. }
  44. const strength = zxcvbn(pwd)
  45. this.passwordStrength = _.round((strength.score + 1 ) / 5 * 100)
  46. if (this.passwordStrength <= 20) {
  47. this.passwordStrengthColor = 'red'
  48. this.passwordStrengthText = 'Very Weak'
  49. } else if (this.passwordStrength <= 40) {
  50. this.passwordStrengthColor = 'orange'
  51. this.passwordStrengthText = 'Weak'
  52. } else if (this.passwordStrength <= 60) {
  53. this.passwordStrengthColor = 'teal'
  54. this.passwordStrengthText = 'Average'
  55. } else if (this.passwordStrength <= 80) {
  56. this.passwordStrengthColor = 'green'
  57. this.passwordStrengthText = 'Strong'
  58. } else {
  59. this.passwordStrengthColor = 'green'
  60. this.passwordStrengthText = 'Very Strong'
  61. }
  62. }, 100)
  63. }
  64. }
  65. </script>
  66. <style lang="scss">
  67. .password-strength > .caption {
  68. width: 100%;
  69. left: 0;
  70. margin: 0;
  71. position: absolute;
  72. top: calc(100% + 5px);
  73. }
  74. </style>