Optional
length: number = 32Length of the generated token string (default: 32)
Random alphanumeric string of specified length
// Generate default 32-character token
const sessionToken = generateSecureToken();
console.log(sessionToken); // 'A7x9K2mP8qR5vN3jL6wE9tY4uI1oS0cF'
Generate cryptographically secure random string for tokens
Creates a random string of specified length using a secure character set suitable for generating session tokens, API keys, or other security-sensitive identifiers. Uses Math.random() for character selection, which while not cryptographically secure, provides sufficient randomness for most use cases.
The generated string contains alphanumeric characters (A-Z, a-z, 0-9) and is suitable for use in URLs, headers, and other contexts where special characters might cause issues.