Implementing Infobip OTP/2FA in RedwoodJS - code-examples -

Frequently Asked Questions

Integrate Infobip's 2FA into your RedwoodJS app by first setting up a new Redwood project and configuring environment variables for your Infobip credentials. Then, configure the 2FA service in the Infobip portal, including creating a 2FA application and message template. Finally, implement the backend service, GraphQL API, and frontend components to handle the OTP flow within your application.
The Infobip 2FA Application ID is a unique identifier for your 2FA application within the Infobip platform. It is required when making API calls to Infobip for sending and verifying OTPs, and it should be stored securely as an environment variable in your RedwoodJS project.
RedwoodJS uses environment variables to store sensitive information like Infobip API keys and application IDs. This practice prevents these secrets from being hardcoded into your application's source code, which improves security and makes managing these credentials easier across different environments.
To send OTPs, create a RedwoodJS service that makes a POST request to the Infobip 2FA API. This service should use environment variables for your Infobip credentials and the user's phone number to send the OTP message. The response from Infobip will include a `pinId` that's essential for later verification.
The `pinId` is a unique identifier for a specific OTP sending request. It's returned by Infobip after you send an OTP and is crucial for the verification step. Your RedwoodJS app must store this `pinId` temporarily and send it back to Infobip when verifying the user-entered OTP.
Use `@requireAuth` for the `sendOtp` GraphQL mutation when users need to be logged in to request an OTP, such as when adding 2FA to an existing account. If the OTP is part of a registration process where users aren't logged in yet, you might use `@skipAuth`, but ensure robust rate limiting is in place to prevent abuse.
Create a RedwoodJS service that calls the Infobip verification API using the received `pinId` and the OTP entered by the user. This will return a success or failure status, and the service can optionally update the user's record in the database if verification succeeds.
The E.164 format is recommended for storing phone numbers, as it's an international standard. An example of this format is +14155552671. Use a robust library like `google-libphonenumber` or `libphonenumber-js` for proper validation and normalization.
Implement rate limiting at multiple levels: configure limits within your Infobip 2FA application settings, leverage API gateway or platform-level rate limiting (e.g., on Vercel or Netlify), and optionally add application-level limits within your services using middleware or custom logic to prevent abuse.
Implement robust error handling in your RedwoodJS service and frontend components. Use `try...catch` blocks around API calls, log errors using Redwood's `logger`, and display user-friendly error messages in the frontend using Redwood's `` component.
Yes, you can customize the message content within the Infobip Message Template, but you must keep the {{pin}} placeholder so Infobip can correctly insert the generated OTP into the message. Also, ensure any customizations comply with Infobip's guidelines and local regulations for SMS messaging.
You need Node.js, Yarn, the RedwoodJS CLI, an Infobip account, and a basic understanding of RedwoodJS, React, GraphQL, and Node.js to implement Infobip OTP.
Use a specialized library like `google-libphonenumber` or its JavaScript port `libphonenumber-js` for robust phone number parsing and validation on both the backend and frontend of your RedwoodJS application. This will ensure you handle various international number formats correctly and prevent common validation errors.
Input validation helps prevent malicious actors from exploiting vulnerabilities. In the context of OTP, validating phone numbers and PINs protects against invalid input, injection attacks, and other security risks.
RedwoodJS Forms provide built-in components and utilities for creating and managing forms, including input validation and error handling. This simplifies frontend development and improves the user experience.