All files / src/utils/passwordStrength passwordStrength.ts

100% Statements 10/10
100% Branches 4/4
100% Functions 2/2
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60                                              3x 457x                           3x 219x     219x                 219x     50x     15x    
/**
 * Password Strength Utility
 * Story 1.2.3: Implement Account Creation Flow - Task 3 (GREEN Phase)
 *
 * Provides password requirement checking and strength calculation
 * following AWS Cognito password policy requirements.
 */
 
export interface PasswordRequirements {
  minLength: boolean;
  hasUppercase: boolean;
  hasLowercase: boolean;
  hasNumber: boolean;
  hasSpecialChar?: boolean;
}
 
export type PasswordStrength = 'weak' | 'medium' | 'strong';
 
/**
 * Check if password meets individual requirements
 * @param password - The password to check
 * @returns Object with boolean flags for each requirement
 */
export const checkPasswordRequirements = (password: string): PasswordRequirements => {
  return {
    minLength: password.length >= 8,
    hasUppercase: /[A-Z]/.test(password),
    hasLowercase: /[a-z]/.test(password),
    hasNumber: /[0-9]/.test(password),
    hasSpecialChar: /[^a-zA-Z0-9\s]/.test(password),
  };
};
 
/**
 * Calculate overall password strength based on requirements met
 * @param password - The password to evaluate
 * @returns Password strength level (weak, medium, strong)
 */
export const calculatePasswordStrength = (password: string): PasswordStrength => {
  const requirements = checkPasswordRequirements(password);
 
  // Count how many requirements are met
  const score = [
    requirements.minLength,
    requirements.hasUppercase,
    requirements.hasLowercase,
    requirements.hasNumber,
    requirements.hasSpecialChar,
  ].filter(Boolean).length;
 
  // Weak: 0-3 requirements met
  if (score <= 3) return 'weak';
 
  // Medium: 4 requirements met
  if (score === 4) return 'medium';
 
  // Strong: All 5 requirements met
  return 'strong';
};