Implement Production-Ready SMS OTP/2FA in NestJS with Plivo - code-examples -

Frequently Asked Questions

Use the Plivo Node.js SDK within a NestJS service to send SMS messages containing one-time passwords (OTPs). The `@plivo/rest-client` library provides methods for sending SMS messages given a Plivo phone number, recipient's number, and the OTP.
Plivo is a cloud communications platform used to send the actual SMS messages containing the OTPs. The provided code example uses the Plivo Node.js helper library to interact with the Plivo API for sending SMS messages reliably.
NestJS provides a structured, scalable framework for building server-side applications in Node.js. Its features like dependency injection and modules make it well-suited for complex tasks like two-factor authentication (2FA) implementation using OTP, as shown in the tutorial.
Rate limiting is crucial in production to prevent abuse and protect your application from excessive requests. This article recommends using the `@nestjs/throttler` module in your NestJS application to implement rate limiting, especially for endpoints like OTP sending, as shown in the example code with the `Throttle` decorator.
Yes, the article suggests using a cache like Redis or Memcached for OTP storage in production, though the example utilizes an in-memory store (`Map`) for simplicity. Integrate Redis by installing `@nestjs/cache-manager` and including it within the modules using Redis for OTP storage and retrieval.
Sign up for a Plivo account, obtain API keys (Auth ID and Auth Token), and rent a Plivo phone number capable of sending SMS. Configure these credentials within your NestJS project, making sure to keep your Auth Token secret (e.g., within a `.env` file).
The `speakeasy` library is used for generating time-based one-time passwords (TOTP) and HMAC-based one-time passwords (HOTP). The example code utilizes it within a NestJS service to generate a unique OTP code to send to a user for 2FA.
Store your Plivo API keys (Auth ID and Auth Token) in a `.env` file within your project, as shown in the guide. Ensure this file is added to your `.gitignore` to prevent it from being committed to version control and exposed publicly.
OTPs have an expiry time to limit their validity, enhancing security. In the example, the `OTP_EXPIRY_SECONDS` environment variable (with a default of 300 seconds/5 minutes) sets how long each OTP is valid before it must be regenerated by the user.
The Plivo Node.js library will throw errors for issues like invalid credentials or network problems. Use a `try-catch` block within your NestJS service to handle these exceptions, log the errors for debugging, and return appropriate responses to the client, typically a generic server error message to avoid exposing sensitive details.
Create a NestJS service method (and corresponding controller endpoint) to receive the user's phone number and OTP. The example includes a dedicated `AuthService` for verification with the `verifyOtp` method, comparing the received OTP against the stored, unexpired OTP to verify user authentication.
The tutorial uses `speakeasy.hotp` in combination with a secret key, the user's phone number, and the current timestamp to generate a one-time password (OTP). This OTP is then sent to the user via SMS using the Plivo API.
Two-factor authentication (2FA), such as using SMS OTP, adds an extra layer of security, protecting user accounts even if passwords are compromised. If an attacker gains access to a user's password, they still need the temporary OTP to access the account.
You'll need Node.js and npm/yarn installed, a Plivo account with API keys and a rented phone number, and a basic understanding of NestJS, TypeScript, and a suitable code editor. The guide also suggests familiarity with making API requests using tools like `curl` or Postman.