Implementing Infobip OTP/2FA with Vite (React) and Node.js - code-examples -

Frequently Asked Questions

Axios, a promise-based HTTP client, is used for making API calls from both the frontend (React) to the backend (Node.js) and the backend to the Infobip API.
Implement Infobip 2FA by setting up a Vite/React frontend, a Node.js/Express backend, and configuring Infobip's OTP service. The frontend sends API requests to the backend, which interacts with Infobip to send and verify OTPs via SMS. This guide provides step-by-step instructions and code examples for a complete setup.
Infobip provides a reliable and global communication platform with robust 2FA APIs, enabling secure user accounts and critical actions by verifying possession of a phone number through OTP, mitigating risks associated with compromised passwords.
The article recommends a monorepo with separate 'client' and 'server' directories. This helps organize the frontend (Vite/React) and backend (Node.js) code cleanly within a single project.
Configure environment variables before running the application. Store sensitive credentials like Infobip API keys, base URL, Application ID, and Message Template ID in a '.env' file on the backend (server-side). Load these via the 'dotenv' library.
Send a POST request to the correct Infobip API endpoint (e.g., your_instance.api.infobip.com/2fa/2/pin) with required data including 'applicationId,' 'messageId,' and the user's phone number in E.164 format, using appropriate authorization headers.
Send a POST request to the Infobip verification API endpoint (e.g., .../2fa/2/pin/PIN_ID/verify) containing the OTP entered by the user. In a production environment, retrieve the 'pinId' from server-side session storage.
The 'pinId' links the OTP request to the verification attempt. In production, store it securely on the server-side, associating it with the user's session. NEVER expose it client-side, unlike the demo code, as this introduces vulnerabilities.
The project uses a Vite (React) frontend for the user interface, a Node.js/Express backend to handle logic and API calls, and Infobip as the OTP provider. The user interacts with the frontend, which communicates with the backend, which in turn interacts with Infobip.
While Axios is recommended for its ease of use, other HTTP clients like 'node-fetch' or the built-in 'http' module can be used by adjusting the request-handling code on the backend (Node.js).
Configure the proxy in the 'vite.config.js' file to forward '/api' requests to your backend server address, like 'http://localhost:3001', to simplify development and avoid CORS issues.
Error codes include 401 (Unauthorized), 400 (Bad Request—often validation errors), 403 (Forbidden), and 500 (Internal Server Error). The article details these and potential causes, such as incorrect API key format, bad phone number format, or invalid application/message IDs.
CORS (Cross-Origin Resource Sharing) configuration is necessary to allow the frontend running on one port (e.g., 5173) to access the backend API running on a different port (e.g., 3001), which is common in development.
Key security practices include protecting your API keys, implementing rate limiting, using HTTPS, securing the 'pinId' server-side, and validating all user inputs.
OTP expiry is defined in the Infobip 2FA Application settings (PIN Time To Live or TTL). Set this to a short duration, like 5 minutes (5m), to enhance security.