password.ts 1.0 KB

1234567891011121314151617181920212223242526
  1. /**
  2. * Password complexity check matching the backend rules in
  3. * `backend/app/schemas/auth.py:_validate_password_complexity` plus the
  4. * implicit `min_length=8` that most server-side schemas enforce.
  5. *
  6. * Returning the FIRST unmet requirement as a translation-key suffix keeps the
  7. * UI message order identical to what the backend would have returned — the
  8. * user sees the same rule fail whether the check happens client- or server-
  9. * side, which avoids the confusion of fixing one issue only to immediately
  10. * trip another after the round-trip.
  11. */
  12. export type PasswordRequirementKey =
  13. | 'tooShort'
  14. | 'needsUppercase'
  15. | 'needsLowercase'
  16. | 'needsDigit'
  17. | 'needsSpecial';
  18. export function checkPasswordComplexity(password: string): PasswordRequirementKey | null {
  19. if (password.length < 8) return 'tooShort';
  20. if (!/[A-Z]/.test(password)) return 'needsUppercase';
  21. if (!/[a-z]/.test(password)) return 'needsLowercase';
  22. if (!/\d/.test(password)) return 'needsDigit';
  23. if (!/[^A-Za-z0-9]/.test(password)) return 'needsSpecial';
  24. return null;
  25. }