Module: AuctionFunCore::Business::TokenGenerator

Defined in:
lib/auction_fun_core/business/token_generator.rb

Overview

The TokenGenerator module is responsible for generating interaction tokens used in various parts of the AuctionFun application for authentication and verification purposes, particularly in interactions with system users.

Class Method Summary collapse

Class Method Details

.generate_email_token(length = 20) ⇒ String

Generates a secure, URL-safe base64 token primarily used for email verification. This method modifies certain characters to avoid common readability issues.

Examples:

Generating an email token

email_token = AuctionFunCore::Business::TokenGenerator.generate_email_token(20)
puts email_token  # Output example: "V4Ph2wkJG_bRzs_zuGyJ"

Parameters:

  • length (Integer) (defaults to: 20)

    the desired length of the generated token before modification

Returns:

  • (String)

    a URL-safe, base64 encoded string suitable for email verification links



17
18
19
20
# File 'lib/auction_fun_core/business/token_generator.rb', line 17

def self.generate_email_token(length = 20)
  rlength = (length * 3) / 4
  SecureRandom.urlsafe_base64(rlength).tr("lIO0", "sxyz")
end

.generate_phone_token(length = 6) ⇒ String

Generates a numerical token used for phone verification processes. The token is zero-padded to ensure it meets the required length.

Examples:

Generating a phone token

phone_token = AuctionFunCore::Business::TokenGenerator.generate_phone_token(6)
puts phone_token  # Output example: "045673"

Parameters:

  • length (Integer) (defaults to: 6)

    the desired length of the numerical token

Returns:

  • (String)

    a string of digits, zero-padded to the specified length



30
31
32
# File 'lib/auction_fun_core/business/token_generator.rb', line 30

def self.generate_phone_token(length = 6)
  rand(0o00000..999_999).to_s.rjust(length, "0")
end