[PR #2543] [CLOSED] feat(auth): implement configurable password complexity rules #29969

Closed
opened 2026-04-17 21:11:48 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/2543
Author: @devabdultech
Created: 5/5/2025
Status: Closed

Base: mainHead: feature/password-complexity


📝 Commits (1)

  • 1bd2010 feat(auth): implement configurable password complexity rules

📊 Changes

11 files changed (+910 additions, -749 deletions)

View changed files

📝 docs/content/docs/authentication/email-password.mdx (+55 -1)
📝 packages/better-auth/src/__snapshots__/init.test.ts.snap (+5 -0)
📝 packages/better-auth/src/adapters/kysely-adapter/test/state.txt (+1 -1)
📝 packages/better-auth/src/api/routes/forget-password.ts (+6 -9)
📝 packages/better-auth/src/api/routes/sign-up.ts (+7 -12)
📝 packages/better-auth/src/api/routes/update-user.ts (+715 -726)
📝 packages/better-auth/src/error/codes.ts (+4 -0)
📝 packages/better-auth/src/init.ts (+10 -0)
📝 packages/better-auth/src/types/options.ts (+30 -0)
📝 packages/better-auth/src/utils/index.ts (+1 -0)
packages/better-auth/src/utils/password-validator.ts (+76 -0)

📄 Description

Overview

This PR introduces configurable password complexity rules across all authentication flows. Developers can now enforce custom password requirements with minimal configuration, enhancing overall security while maintaining backward compatibility.

Changes

  • New Configuration Options

    • requireUppercase: Enforce at least one uppercase letter
    • requireLowercase: Enforce at least one lowercase letter
    • requireNumber: Enforce at least one numeric digit
    • requireSpecialChar: Enforce at least one special character
    • specialCharacters: Custom string of characters treated as “special”
  • Centralized Validation

    • Created a new utility function validatePasswordComplexity
    • Plugged into:
      • Sign-up flow
      • Password-reset flow
      • Change-password flow
  • Documentation

    • Updated reference docs with all new options
    • Added examples of recommended settings

Implementation Details

  1. Utility Function

    • File: utils/validatePasswordComplexity.ts
    • Accepts:
      interface PasswordComplexityOptions {
        requireUppercase?: boolean;
        requireLowercase?: boolean;
        requireNumber?: boolean;
        requireSpecialChar?: boolean;
        specialCharacters?: string;
      }
      
    • Returns either true or throws a PasswordValidationError with:
      {
        code: 'PASSWORD_TOO_SIMPLE',
        message: 'Password must include at least one uppercase letter.'
      }
      
  2. Default Configuration

    const defaultPasswordConfig: PasswordComplexityOptions = {
      requireUppercase: false,
      requireLowercase: false,
      requireNumber:    false,
      requireSpecialChar: false,
      specialCharacters: '!@#$%^&*()'
    };
    
    

Closes #2484


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/2543 **Author:** [@devabdultech](https://github.com/devabdultech) **Created:** 5/5/2025 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feature/password-complexity` --- ### 📝 Commits (1) - [`1bd2010`](https://github.com/better-auth/better-auth/commit/1bd20104bb83171a427645d7c9c9226f028b5eeb) ✨ feat(auth): implement configurable password complexity rules ### 📊 Changes **11 files changed** (+910 additions, -749 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/authentication/email-password.mdx` (+55 -1) 📝 `packages/better-auth/src/__snapshots__/init.test.ts.snap` (+5 -0) 📝 `packages/better-auth/src/adapters/kysely-adapter/test/state.txt` (+1 -1) 📝 `packages/better-auth/src/api/routes/forget-password.ts` (+6 -9) 📝 `packages/better-auth/src/api/routes/sign-up.ts` (+7 -12) 📝 `packages/better-auth/src/api/routes/update-user.ts` (+715 -726) 📝 `packages/better-auth/src/error/codes.ts` (+4 -0) 📝 `packages/better-auth/src/init.ts` (+10 -0) 📝 `packages/better-auth/src/types/options.ts` (+30 -0) 📝 `packages/better-auth/src/utils/index.ts` (+1 -0) ➕ `packages/better-auth/src/utils/password-validator.ts` (+76 -0) </details> ### 📄 Description ## Overview This PR introduces configurable password complexity rules across all authentication flows. Developers can now enforce custom password requirements with minimal configuration, enhancing overall security while maintaining backward compatibility. ## Changes - **New Configuration Options** - `requireUppercase`: Enforce at least one uppercase letter - `requireLowercase`: Enforce at least one lowercase letter - `requireNumber`: Enforce at least one numeric digit - `requireSpecialChar`: Enforce at least one special character - `specialCharacters`: Custom string of characters treated as “special” - **Centralized Validation** - Created a new utility function `validatePasswordComplexity` - Plugged into: - Sign-up flow - Password-reset flow - Change-password flow - **Documentation** - Updated reference docs with all new options - Added examples of recommended settings ## Implementation Details 1. **Utility Function** - **File**: `utils/validatePasswordComplexity.ts` - Accepts: ```ts interface PasswordComplexityOptions { requireUppercase?: boolean; requireLowercase?: boolean; requireNumber?: boolean; requireSpecialChar?: boolean; specialCharacters?: string; } ``` - Returns either `true` or throws a `PasswordValidationError` with: ```ts { code: 'PASSWORD_TOO_SIMPLE', message: 'Password must include at least one uppercase letter.' } ``` 2. **Default Configuration** ```ts const defaultPasswordConfig: PasswordComplexityOptions = { requireUppercase: false, requireLowercase: false, requireNumber: false, requireSpecialChar: false, specialCharacters: '!@#$%^&*()' }; Closes #2484 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-17 21:11:48 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#29969