Headless MFA
Introduction
This guide will show you how to add MFA to your app using your own custom UI. You can also refer to the general MFA guide here to learn more about this feature (note that this guide is for account level MFA, rather than transaction level MFA).
For a working example, see: https://codesandbox.io/p/sandbox/kh8g8s
General Flow
- User logs in
- User is redirected to the MFA view
- User adds a device
- User is redirected to the OTP view
- User enters the OTP
- User is redirected to the backup codes view
- User acknowledges the backup codes
The user must accept the backup codes before the MFA flow is complete.
Step-by-Step Implementation
Step 1: Import Necessary Modules and Components
Start by importing the required modules and components:
Step 2: Define Types
Define the types for the MFA registration data.
Step 3: Create the MfaView
Component
This component handles the main logic for MFA, including device management, QR code generation, OTP submission, and backup codes.
Explanation:
-
State Management:
userDevices
: Stores the list of MFA devices associated with the user.mfaRegisterData
: Stores the data required for MFA registration (QR code URI and secret).currentView
: Tracks the current view (e.g., ‘devices’, ‘qr-code’, ‘otp’, ‘backup-codes’).backupCodes
: Stores the backup codes for MFA.error
: Stores any error messages.
-
Hooks:
useIsLoggedIn
: Checks if the user is logged in.useMfa
: Provides functions for managing MFA (e.g.,addDevice
,authDevice
,getUserDevices
,getRecoveryCodes
,completeAcknowledgement
).useDynamicContext
: Provides user context and logout function.useEffect
: Refreshes user devices when the user logs in.useSyncMfaFlow
: Synchronizes the MFA flow based on the user’s authentication status.
-
Functions:
refreshUserDevices
: Fetches and sets the user’s MFA devices.onAddDevice
: Initiates the process of adding a new MFA device.onQRCodeContinue
: Proceeds to the OTP view after displaying the QR code.onOtpSubmit
: Authenticates the device using the provided OTP and fetches backup codes.
-
Return Statement:
- Renders the UI components based on the current view and state.
Step 4: Create Supporting Components
Define the supporting components for displaying backup codes, QR code, and OTP input.
Explanation:
-
BackupCodesView:
- Displays the backup codes and provides an “Accept” button to acknowledge them.
-
LogIn:
- Displays a message indicating that the user is not logged in and shows the
DynamicWidget
for login.
- Displays a message indicating that the user is not logged in and shows the
-
QRCodeView:
- Displays the QR code for MFA setup and the secret key. Provides a “Continue” button to proceed to the OTP view.
-
OTPView:
- Provides a form for the user to enter the OTP. On form submission, it calls the
onSubmit
function with the entered OTP.
- Provides a form for the user to enter the OTP. On form submission, it calls the
Step 5: Create the Main HeadlessMfaView
Component
This component checks if the user is logged in and displays the appropriate view.
Explanation:
-
HeadlessMfaView:
- Checks if the user is logged in or has missing information that requires additional authentication.
- If the user is logged in, it renders the
MfaView
component. - If the user is not logged in, it renders the
LogIn
component.
-
App:
- Your main app entry point.
- Make sure to set you environment id.
Conclusion
By following this detailed breakdown, you should have a clear understanding of how the provided code implements a headless MFA solution using a custom UI. Each part of the code is explained, including the purpose of each component, hook, and function. This should help you understand the overall flow and how to customize it for your needs.
Was this page helpful?