password-rules.ts 1.1 KB

123456789101112131415161718192021222324252627282930
  1. /**
  2. * What the server accepts as a new password, in one place for every form
  3. * that sets one.
  4. *
  5. * These are Better Auth's own limits: `src/lib/auth.ts` sets neither
  6. * `minPasswordLength` nor `maxPasswordLength`, so its defaults of 8 and 128
  7. * apply to sign-up, reset, change and set password alike. Better Auth
  8. * measures with `password.length`, and so does this file. There are no
  9. * other rules (no character classes, no strength score); the forms say
  10. * exactly this and nothing more.
  11. */
  12. export const PASSWORD_MIN_LENGTH = 8
  13. export const PASSWORD_MAX_LENGTH = 128
  14. export type PasswordCheck =
  15. | { status: 'empty' }
  16. | { status: 'tooShort'; remaining: number }
  17. | { status: 'tooLong'; over: number }
  18. | { status: 'ok' }
  19. export function checkPassword(password: string): PasswordCheck {
  20. if (password.length === 0) return { status: 'empty' }
  21. if (password.length < PASSWORD_MIN_LENGTH) {
  22. return { status: 'tooShort', remaining: PASSWORD_MIN_LENGTH - password.length }
  23. }
  24. if (password.length > PASSWORD_MAX_LENGTH) {
  25. return { status: 'tooLong', over: password.length - PASSWORD_MAX_LENGTH }
  26. }
  27. return { status: 'ok' }
  28. }