Frequently Asked Questions
Yes. Check Sinch's documentation, as the length and format of the OTP may be configurable in your Sinch account settings. Ensure that your frontend validation and the backend handling align with the configured OTP length from Sinch.
Two-factor authentication (2FA) can be implemented in RedwoodJS using the Sinch Verification API. This involves modifying your RedwoodJS schema, setting up your Sinch account and configuring your RedwoodJS application to communicate with the Sinch API. This will add an extra layer of security by requiring an OTP sent via SMS in addition to the standard password login.
The Sinch Verification API is used to send and verify one-time passwords (OTPs) via SMS messages for two-factor authentication. Sinch handles OTP generation, sending, and verification, simplifying the implementation process in RedwoodJS. You'll need a Sinch account and API credentials to use this service.
Sinch offers dedicated verification features that handle OTP generation and verification. This offloads complexity from your RedwoodJS application and allows you to focus on the integration and user flow rather than managing OTP complexities directly. Sinch is a third-party service specifically designed for this purpose.
To integrate Sinch, obtain your API key and secret from your Sinch dashboard, and store them securely in your `.env` file. Create a Redwood service to encapsulate Sinch API calls (initiate verification and verify OTP). Modify your `dbAuth` handler to trigger OTP requests after successful password validation and add GraphQL mutations for requesting and verifying OTPs on the frontend.
You need to add `phone` (String, optional initially) and `isPhoneVerified` (Boolean, default false) to your `User` model in `schema.prisma`. The `phone` field stores the user's phone number, while `isPhoneVerified` tracks whether their phone has been verified through the OTP process.
Sinch generates the OTP and sends it directly to the user's phone number via SMS. Your RedwoodJS application only needs to initiate the verification request and then verify the code entered by the user against the Sinch API, no need for server-side OTP generation or storage.
Users receive an OTP after successfully entering their correct password *only if* they have a registered phone number, and the `isPhoneVerified` flag on their user record is set to `true`. This ensures that OTP is only enforced for users who have completed the phone verification process.
Add state variables to manage UI transitions between the login form and the OTP input form. Catch the `AUTH_OTP_REQUIRED` error thrown by the `dbAuth` handler after successful password validation to trigger the OTP input state. Implement a submission handler for the OTP form that calls a `verifyOtp` GraphQL mutation.
This specific error is thrown by the custom login handler in `api/src/functions/auth.js` after a successful password check *if* the user has a verified phone number and requires OTP. It signals to the frontend to transition to the OTP input state.
Implement a `requestOtp` GraphQL mutation that calls the Sinch API to resend an OTP to the user's registered phone number. Add a button to your OTP input form that triggers this mutation. Include loading state handling to disable the resend button while the request is in progress.
After completing the setup and implementation steps, create a user account, register a phone number, and set the `isPhoneVerified` flag to `true`. Then, test the login process by entering the correct password, and verify that you are prompted for the OTP sent via SMS.
The `isPhoneVerified` flag should be updated in the `verifyOtp` resolver after Sinch successfully verifies the user-provided OTP. This typically occurs during the user's initial phone verification or in a profile update where the user adds/changes their phone number.
The "fire and forget" approach used in the guide for sending the initial SMS relies heavily on logging to track failures. For production, using a background job queue (like Redis with BullMQ) for sending SMS with retry mechanisms is recommended.
Content Loading Error
We encountered an error while processing this content.