Production-Ready Node.js & NestJS OTP/2FA with Infobip - code-examples -

Frequently Asked Questions

Use the `/otp/send` endpoint with a POST request containing the recipient's phone number in E.164 format. The NestJS application will interact with the Infobip 2FA API to generate and send the OTP via SMS, returning a unique `pinId` for verification. Ensure your request body includes a `phoneNumber` field formatted correctly as a string, for example, '+14155552671'.
First, obtain an Infobip account, Base URL, and API Key. Then, create a 2FA application and message template in the Infobip portal. Store the `applicationId`, `messageId`, Base URL, and API Key securely in your `.env` file, loaded using NestJS's `ConfigService`.
The `pinId` is a unique identifier generated by the Infobip API after sending an OTP. It's crucial for verifying the OTP code submitted by the user. Your NestJS application should temporarily store the `pinId` linked to the user's pending action (e.g. login, registration).
These libraries facilitate request body validation using decorators in your DTOs (Data Transfer Objects). `class-validator` provides decorators like `@IsNotEmpty`, `@IsPhoneNumber`, etc., while `class-transformer` handles transformations between plain objects and class instances.
A global exception filter is highly recommended for production applications to handle errors consistently. It provides centralized error logging and standardized error responses, improving maintainability and the user experience.
Inject the `HttpService` from `@nestjs/axios` into your NestJS service. Use it to make POST requests to Infobip's 2FA API endpoints, setting appropriate headers, including the `Authorization` header with your API key. Manage all API credentials using environment variables and the `ConfigService`.
Send a POST request to the `/otp/verify` endpoint, including the `pinId` obtained during the send request and the user-entered `otpCode` in the request body. The NestJS application verifies the code against Infobip, returning a boolean `verified` status in the response.
The `@nestjs/throttler` module provides rate limiting to prevent API abuse. This mitigates brute-force attacks on the `/otp/send` and `/otp/verify` endpoints and safeguards your application.
Yes, use the `@Throttle` decorator on individual controller methods to override global rate limiting settings from `ThrottlerModule.forRoot`. This allows for fine-grained control, such as permitting more verify attempts than send requests.
TypeScript enhances JavaScript with static typing, improving code quality, maintainability, and developer experience. It makes large projects like this NestJS application easier to manage, debug, and scale.
An LTS (Long Term Support) version like Node.js v18 or v20 is recommended for stability and maintenance. These versions receive security updates and performance improvements for an extended period.
Implement a dedicated error handling method in your service to catch `AxiosError` instances. This provides an opportunity to log details like error messages, response data, and status codes, along with appropriate context.
The client application initiates OTP requests to the NestJS API, which acts as an intermediary for interacting with the Infobip 2FA API. This setup decoupled direct client interaction with Infobip, providing greater flexibility and control over the authentication flow.