How kech37/otp-authentication works
Main Components
• OTP Generator: This module encapsulates the creation of one-time passwords (relying on an external otp-generator package).
• Cache Service: Manages OTP storage with the default implementation using Redis. Note the abstraction available (from src/@types/cache-service-abstract.ts) to allow custom implementations.
• Delivery Service: Responsible for sending OTPs. There are two delivery options:
• Email Delivery using Nodemailer (via src/lib/nodemailer-delivery-service.ts) along with configuration settings for mail options.
• SMS Delivery using Twilio (via src/lib/twilio-delivery-service.ts).
• Central OTP Authentication Class: This core module (found in src/lib/otp-authentication.ts) orchestrates the OTP creation, storage, and delivery processes.
• External Dependencies: These include Redis, Nodemailer, Twilio, and the underlying npm otp-generator package.
Out Relationships and Interactions
• The OTP Authentication component calls on the OTP Generator to produce an OTP.
• It then interacts with the Cache Service to store the OTP (with a TTL – Time-To-Live – for security).
• Depending on the chosen method (email or SMS), the OTP Authentication component uses either the Email or SMS Delivery Service to send the OTP to the user.
• From the OTP Authentication module to the OTP Generator (“Generate OTP”).
• From OTP Authentication to Cache Service (“Store OTP” and “Retrieve OTP”).
• From OTP Authentication to Delivery Service (Email or SMS) (“Send OTP”).
• Flows from external components like the Redis server, SMTP server, or Twilio’s API indicating actual interactions when sending or fetching data.
Architectural Patterns & Design Principles
• Separation of Concerns: Each major functionality (OTP generation, cache management, and delivery) is encapsulated in its own module. This separation should be visually represented as distinct layers or blocks.
• Dependency Injection: The constructors for EmailOTPAuthentication and SMSOTPAuthentication take configuration options for both the cache and delivery services. Illustrate this flow as “injected dependencies” which allow easy swapping of implementations.
• Be detailed: ensure that every component’s role—how it generates, stores, or transmits the OTP—is captured.
• Finally, emphasize the orchestration flow: when a request arrives (via an Express endpoint or similar), showing the full lifecycle from OTP generation, caching, delivery, and subsequent verification.