WayrApp Backend & Ecosystem Documentation - v1.0.0
    Preparing search index...

    Function generateSecureToken

    • 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.

      Parameters

      • Optionallength: number = 32

        Length of the generated token string (default: 32)

      Returns string

      Random alphanumeric string of specified length

      // Generate default 32-character token
      const sessionToken = generateSecureToken();
      console.log(sessionToken); // 'A7x9K2mP8qR5vN3jL6wE9tY4uI1oS0cF'
      // Generate custom length tokens
      const shortToken = generateSecureToken(16); // 16 characters
      const longToken = generateSecureToken(64); // 64 characters
      // Use for API key generation
      const apiKey = `wayr_${generateSecureToken(40)}`;
      console.log(apiKey); // 'wayr_X8mK9pL2qR7vN4jM6wE3tY5uI0oS1cF2dG8hJ9k'