Skip to content

Instantly share code, notes, and snippets.

@rakia
Created December 24, 2024 19:36
Show Gist options
  • Save rakia/c3fa325571ad7672fc37312a057c3f73 to your computer and use it in GitHub Desktop.
Save rakia/c3fa325571ad7672fc37312a057c3f73 to your computer and use it in GitHub Desktop.
Angular component: password-field
@Component({
selector: 'app-password-field',
templateUrl: './password-field.component.html',
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [ReactiveFormsModule, StrengthMeterComponent]
})
export class PasswordFieldComponent implements OnInit {
destroyRef = inject(DestroyRef);
@Input() passwordFormControl: FormControl = new FormControl<any>('');
showPassword = false;
passwordStrength: number = 0;
ngOnInit(): void {
this.passwordFormControl.valueChanges
.pipe(
debounceTime(200), // wait for 200ms after the last keystroke
distinctUntilChanged(), // trigger only when the value actually changes
takeUntilDestroyed(this.destroyRef)) // unsubscribe when the component is destroyed
.subscribe((value) => {
// run logic
this.passwordStrength = this.calculatePasswordStrength(value);
});
}
/**
* In the passwordRules, we check whether each the user has considered each factor that makes their password stronger.
* This is reflected on the UI as well to guide the user.
*/
get passwordRules() {
// ...
}
calculatePasswordStrength(password: string): number {
// ...
}
togglePasswordVisibility(): void {
this.showPassword = !this.showPassword;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment