Merge pull request #2384 from alberto-art3ch/fix/user_change_password_validations

Change password view enhancement to support and display password vali…
This commit is contained in:
Jose Alberto Hernandez
2025-04-06 12:29:30 -05:00
committed by GitHub
24 changed files with 352 additions and 102 deletions
+35
View File
@@ -0,0 +1,35 @@
import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';
import { environment } from 'environments/environment';
export function passwordValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const value = control.value;
if (!value) return null; // No validation if the field is empty
const errors: ValidationErrors = {};
if (value.length < environment.minPasswordLength) {
errors['minLength'] = 'Password must be at least ' + environment.minPasswordLength + ' characters long';
}
if (value.length > 50) {
errors['maxLength'] = 'Password must be maximum 50 characters long';
}
if (!/[A-Z]/.test(value)) {
errors['uppercase'] = 'Password must contain at least one uppercase letter';
}
if (!/[a-z]/.test(value)) {
errors['lowercase'] = 'Password must contain at least one lowercase letter';
}
if (!/\d/.test(value)) {
errors['number'] = 'Password must contain at least one number';
}
if (!/^(?:(.)(?!\1))+$/.test(value)) {
errors['repeated'] = 'Password must have not consecutive repeating characters';
}
if (!/[@$!%*?&]/.test(value)) {
errors['specialChar'] = 'Password must contain at least one special character (@$!%*?&)';
}
return Object.keys(errors).length > 0 ? errors : null;
};
}
+10 -4
View File
@@ -1,21 +1,27 @@
import { Injectable } from '@angular/core';
import { AbstractControl, ValidationErrors, ValidatorFn, Validators } from '@angular/forms';
import { environment } from 'environments/environment';
import { Subscription } from 'rxjs';
import { passwordValidator } from './password.validator';
import { environment } from 'environments/environment';
@Injectable({
providedIn: 'root'
})
export class PasswordsUtility {
minPasswordLength: number = environment.minPasswordLength | 12;
// password regex pattern
public static PASSWORD_REGEX = '^(?!.*(.)\\1{1,})(?!.*\\s)(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^\\w\\s]).{12,50}$';
public static PASSWORD_REGEX =
'^(?!.*(.)\\1{1,})(?!.*\\s)(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^\\w\\s]).{' +
(environment.minPasswordLength | 12) +
',50}$';
public getPasswordValidators(): ValidatorFn[] {
return [
Validators.required,
Validators.pattern(PasswordsUtility.PASSWORD_REGEX),
Validators.minLength(this.minPasswordLength),
Validators.maxLength(50),
Validators.minLength(environment.minPasswordLength)];
passwordValidator()
];
}
/**