|
| 1 | +import {Component, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core'; |
| 2 | + |
| 3 | +export enum Colors { |
| 4 | + primary, |
| 5 | + accent, |
| 6 | + warn |
| 7 | +} |
| 8 | + |
| 9 | +@Component({ |
| 10 | + selector: 'ngx-material-password-strength', |
| 11 | + templateUrl: './password-strength.component.html', |
| 12 | + styleUrls: ['./password-strength.component.scss'] |
| 13 | +}) |
| 14 | +export class PasswordStrengthComponent implements OnInit, OnChanges { |
| 15 | + |
| 16 | + @Input() |
| 17 | + password: string; |
| 18 | + |
| 19 | + @Input() |
| 20 | + externalError: boolean; |
| 21 | + |
| 22 | + private _strength: number; |
| 23 | + |
| 24 | + private _color: string; |
| 25 | + |
| 26 | + constructor() { |
| 27 | + } |
| 28 | + |
| 29 | + ngOnInit(): void { |
| 30 | + } |
| 31 | + |
| 32 | + ngOnChanges(changes: SimpleChanges): void { |
| 33 | + console.log('on change: ', changes); |
| 34 | + if (changes.externalError && changes.externalError.firstChange) { |
| 35 | + this._color = Colors[Colors.primary]; |
| 36 | + return; |
| 37 | + } |
| 38 | + if (changes.externalError && changes.externalError.currentValue) { |
| 39 | + this._color = Colors[Colors.warn]; |
| 40 | + return; |
| 41 | + } |
| 42 | + this.password && this.password.length > 0 ? |
| 43 | + this._calculatePasswordStrength() : this._strength = 0; |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + get strength(): number { |
| 48 | + // console.log('strength = ', this._strength); |
| 49 | + return this._strength; |
| 50 | + } |
| 51 | + |
| 52 | + get color(): string { |
| 53 | + |
| 54 | + if (this._strength <= 20) { |
| 55 | + return Colors[Colors.warn]; |
| 56 | + } else if (this._strength <= 80) { |
| 57 | + return Colors[Colors.accent]; |
| 58 | + } else { |
| 59 | + return Colors[Colors.primary]; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private _containAtLeastEightChars(): boolean { |
| 64 | + return this.password.length >= 8; |
| 65 | + } |
| 66 | + |
| 67 | + private _containAtLeastOneLowerCaseLetter(): boolean { |
| 68 | + return RegExp(/^(?=.*?[a-z])/).test(this.password); |
| 69 | + } |
| 70 | + |
| 71 | + private _containAtLeastOneUpperCaseLetter(): boolean { |
| 72 | + return RegExp(/^(?=.*?[A-Z])/).test(this.password); |
| 73 | + } |
| 74 | + |
| 75 | + private _containAtLeastOneDigit(): boolean { |
| 76 | + return RegExp(/^(?=.*?[0-9])/).test(this.password); |
| 77 | + } |
| 78 | + |
| 79 | + private _containAtLeastOneSpecialChar(): boolean { |
| 80 | + return RegExp(/^(?=.*?[#?!@$%^&*-])/).test(this.password); |
| 81 | + } |
| 82 | + |
| 83 | + private _calculatePasswordStrength() { |
| 84 | + console.log('on _calculatePasswordStrength()'); |
| 85 | + const requirements: Array<boolean> = []; |
| 86 | + const unit = 100 / 5; |
| 87 | + |
| 88 | + |
| 89 | + requirements.push( |
| 90 | + this._containAtLeastEightChars(), |
| 91 | + this._containAtLeastOneLowerCaseLetter(), |
| 92 | + this._containAtLeastOneUpperCaseLetter(), |
| 93 | + this._containAtLeastOneDigit(), |
| 94 | + this._containAtLeastOneSpecialChar()); |
| 95 | + |
| 96 | + console.log('requirements = ', requirements); |
| 97 | + |
| 98 | + this._strength = requirements.filter(v => v).length * unit; |
| 99 | + |
| 100 | + console.log('strength = ', this._strength); |
| 101 | + } |
| 102 | +} |
0 commit comments