passwordPolicyUi.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import {
  2. PASSWORD_POLICY,
  3. PASSWORD_POLICY_REASON,
  4. } from "@/lib/auth/passwordPolicy";
  5. const ORDERED_REASONS = [
  6. PASSWORD_POLICY_REASON.MIN_LENGTH,
  7. PASSWORD_POLICY_REASON.MISSING_LETTER,
  8. PASSWORD_POLICY_REASON.MISSING_NUMBER,
  9. PASSWORD_POLICY_REASON.SAME_AS_CURRENT,
  10. ];
  11. function uniqueStrings(arr) {
  12. const list = Array.isArray(arr) ? arr.map(String) : [];
  13. return Array.from(new Set(list));
  14. }
  15. function labelForReasonDe(reason, { minLength } = {}) {
  16. const min =
  17. Number.isInteger(minLength) && minLength > 0
  18. ? minLength
  19. : PASSWORD_POLICY.minLength;
  20. if (reason === PASSWORD_POLICY_REASON.MIN_LENGTH) {
  21. return `Mindestens ${min} Zeichen`;
  22. }
  23. if (reason === PASSWORD_POLICY_REASON.MISSING_LETTER) {
  24. return "Mindestens 1 Buchstabe (A–Z)";
  25. }
  26. if (reason === PASSWORD_POLICY_REASON.MISSING_NUMBER) {
  27. return "Mindestens 1 Zahl (0–9)";
  28. }
  29. if (reason === PASSWORD_POLICY_REASON.SAME_AS_CURRENT) {
  30. return "Neues Passwort darf nicht identisch sein";
  31. }
  32. return null;
  33. }
  34. export function getPasswordPolicyHintLinesDe(policy = PASSWORD_POLICY) {
  35. const min =
  36. Number.isInteger(policy?.minLength) && policy.minLength > 0
  37. ? policy.minLength
  38. : PASSWORD_POLICY.minLength;
  39. const lines = [`Mindestens ${min} Zeichen`];
  40. if (policy?.requireLetter) lines.push("Mindestens 1 Buchstabe (A–Z)");
  41. if (policy?.requireNumber) lines.push("Mindestens 1 Zahl (0–9)");
  42. if (policy?.disallowSameAsCurrent)
  43. lines.push("Darf nicht identisch zum aktuellen Passwort sein");
  44. return lines;
  45. }
  46. export function reasonsToHintLinesDe({ reasons, minLength } = {}) {
  47. const set = new Set(uniqueStrings(reasons));
  48. const ordered = ORDERED_REASONS.filter((r) => set.has(r));
  49. const lines = ordered
  50. .map((r) => labelForReasonDe(r, { minLength }))
  51. .filter(Boolean);
  52. return lines;
  53. }
  54. export function buildWeakPasswordMessageDe({ reasons, minLength } = {}) {
  55. const lines = reasonsToHintLinesDe({ reasons, minLength });
  56. if (lines.length === 0) return "Bitte wählen Sie ein stärkeres Passwort.";
  57. return lines.join(" • ");
  58. }