Introduction
ValidAuth provides a comprehensive set of functions for authentication validation and token generation in JavaScript applications. All functions are exported from the main package and can be imported individually or together.API Organization
The ValidAuth API is organized into three main categories:Validators
Functions that validate user input and credentials
Generators
Functions that generate secure tokens and passwords
Utilities
Helper functions for password strength and matching
Validators
Validation functions help ensure user input meets security requirements.isEmail
Validates email addresses with customizable options
isPassword
Validates passwords against security requirements
isUsername
Validates usernames with configurable rules
validateOTP
Validates one-time password codes
isSessionTokenValid
Checks if session tokens are valid and not expired
isXSSSafe
Checks input for XSS attack patterns
Validator Functions
isEmail(email, options)
Validates an email address with extensive customization options. Parameters:email(string): The email address to validateoptions(Object, optional):allowPlusAddressing(boolean): Allow plus addressing (default:true)requireTLD(boolean): Require top-level domain (default:true)blockedDomains(string[]): List of blocked domains (default:[])details(boolean): Return detailed validation results (default:false)
boolean or Object (if details: true)
Example:
isPassword(password, options)
Validates a password against security requirements. Parameters:password(string): The password to validateoptions(Object, optional):minLength(number): Minimum length (default:8)maxLength(number): Maximum length (default:128)requireUppercase(boolean): Require uppercase letters (default:true)requireLowercase(boolean): Require lowercase letters (default:true)requireNumbers(boolean): Require numbers (default:true)requireSymbols(boolean): Require symbols (default:true)forbidCommonPasswords(boolean): Block common passwords (default:true)details(boolean): Return detailed validation results (default:false)
boolean or Object (if details: true)
Example:
isUsername(username, options)
Validates a username with configurable rules. Parameters:username(string): The username to validateoptions(Object, optional):minLength(number): Minimum length (default:3)maxLength(number): Maximum length (default:30)allowSpecialChars(boolean): Allow special characters (default:false)forbidSpaces(boolean): Forbid spaces (default:true)forbidStartingNumber(boolean): Forbid starting with number (default:true)blockedUsernames(string[]): List of blocked usernames (default:[])details(boolean): Return detailed validation results (default:false)
boolean or Object (if details: true)
Example:
validateOTP(otp, correctOTP, options)
Validates an OTP code with attempt tracking. Parameters:otp(string): The OTP code to validatecorrectOTP(string): The correct OTP code to compare againstoptions(Object, optional):attempts(number): Current attempt numbermaxAttempts(number): Maximum attempts allowed (default:3)details(boolean): Return detailed validation results (default:false)
boolean or Object (if details: true)
Example:
isSessionTokenValid(token, options)
Validates a session token to check if it’s still valid and not expired. Parameters:token(string): The session token to validateoptions(Object, optional):length(number): Length of the random part (default:32)userID(string): User ID in the token (default:null)expiresIn(string): Expiration time format (default:'1h')includeTimestamp(boolean): Whether token has timestamp (default:true)details(boolean): Return detailed validation results (default:false)
boolean or Object (if details: true)
Example:
isXSSSafe(input, options)
Checks if a string is safe from XSS (Cross-Site Scripting) attacks. Parameters:input(string): The string to check for XSS patternsoptions(Object, optional):details(boolean): Return detailed validation results (default:false)
boolean or Object (if details: true)
Example:
Generators
Generator functions create secure passwords, tokens, and OTP codes.generatePassword
Generate strong, secure passwords
generateOTP
Generate one-time password codes
generateSessionToken
Generate secure session tokens
Generator Functions
generatePassword(options)
Generates a strong password based on specified criteria. Parameters:options(Object, optional):length(number): Password length (default:12)includeUppercase(boolean): Include uppercase letters (default:true)includeLowercase(boolean): Include lowercase letters (default:true)includeNumbers(boolean): Include numbers (default:true)includeSymbols(boolean): Include symbols (default:true)details(boolean): Return detailed information (default:false)
string or Object (if details: true)
Example:
generateOTP(options)
Generates an OTP code. Parameters:options(Object, optional):length(number): OTP code length (default:4)type(string): Type of OTP -'numeric'or'alphanumeric'(default:'numeric')details(boolean): Return detailed information (default:false)
string or Object (if details: true)
Example:
generateSessionToken(options)
Generates a secure session token with optional timestamp and user ID. Parameters:options(Object, optional):length(number): Token length (excluding timestamp/userID) (default:32)userID(string): User ID to include in token (default:null)expiresIn(string): Expiration time (default:'1h')includeTimestamp(boolean): Include timestamp (default:true)details(boolean): Return detailed information (default:false)
string or Object (if details: true)
Example:
Utilities
Utility functions provide additional authentication-related functionality.getPasswordStrength(password, options)
Calculates the strength of a password. Parameters:password(string): The password to evaluateoptions(Object, optional):details(boolean): Return detailed strength analysis (default:false)
string or Object (if details: true)
Returns one of: 'weak', 'medium', or 'strong'
Example:
isPasswordMatch(password, confirmPassword, options)
Checks if two passwords match. Parameters:password(string): The passwordconfirmPassword(string): The confirmation passwordoptions(Object, optional):details(boolean): Return detailed match results (default:false)
boolean or Object (if details: true)
Example:
Quick Reference Table
| Function | Category | Purpose | Returns |
|---|---|---|---|
isEmail | Validator | Validate email addresses | boolean or Object |
isPassword | Validator | Validate password strength | boolean or Object |
isUsername | Validator | Validate username format | boolean or Object |
validateOTP | Validator | Validate OTP codes | boolean or Object |
isSessionTokenValid | Validator | Validate session tokens | boolean or Object |
isXSSSafe | Validator | Check for XSS patterns | boolean or Object |
generatePassword | Generator | Generate secure passwords | string or Object |
generateOTP | Generator | Generate OTP codes | string or Object |
generateSessionToken | Generator | Generate session tokens | string or Object |
getPasswordStrength | Utility | Calculate password strength | string or Object |
isPasswordMatch | Utility | Compare two passwords | boolean or Object |
Common Patterns
Detailed Responses
All validators and generators support adetails option that returns comprehensive information:
Error Handling
When validation fails withdetails: true, the response includes an errors array:
TypeScript Support
ValidAuth is written in JavaScript but provides TypeScript type definitions for all functions. Import types alongside functions:Next Steps
Validators
Explore detailed validator documentation
Generators
Learn about token and password generation
Examples
See real-world usage examples
Best Practices
Learn best practices and configuration