Overview
TheEnrollMfa component provides a comprehensive solution for implementing multi-factor authentication enrollment flows. It guides users through setting up two-factor authentication (currently TOTP-based) with support for QR code scanning or manual secret entry.
The core features include:
- TOTP (Time-based One-Time Password) enrollment via authenticator apps
- QR code generation for easy setup with authenticator apps
- Manual secret entry as an alternative to QR scanning
- Multi-step enrollment process (method selection, setup, verification)
- Composable UI that gives developers full control over layout and styling
- Session timeout handling for security
- Centralized state management for the enrollment process
Architecture
The component is built using a composition pattern that allows for maximum flexibility while providing sensible defaults when customization is not needed.Composition model
TheEnrollMfa component is composed of several subcomponents that work together to create the complete enrollment experience. This approach allows developers to customize the UI structure while maintaining the underlying functionality.
The main components are:
EnrollMfa: The root wrapper component that provides theEnrollMfaContextand manages stateEnrollMfaTitle: Renders the title for the current stepEnrollMfaDescription: Renders the description for the current stepEnrollMfaImage: Renders the image/icon for the current stepEnrollMfaFlow: Manages the multi-step flow with transitions between stepsEnrollMfaFlowBackButton: A button to navigate back in the flowEnrollMfaItems: Renders the list of available MFA methodsEnrollMfaItem: Renders an individual MFA method itemEnrollMfaError: Displays error messagesEnrollMfaFooter: The “Secured by Coinbase” footer
EnrollMfa
TheEnrollMfa component accepts a children prop that can be either React nodes or a render function. When using a render function, it receives the current EnrollMfaState as an argument, providing access to all state values without needing to use the useEnrollMfaContext hook directly.
Example of children as a render function:
State management (EnrollMfaProvider and EnrollMfaContext)
The entire enrollment flow’s state is managed byEnrollMfaProvider and accessed via the useEnrollMfaContext hook. This context contains:
method: The currently selected MFA methodmethods: Array of available MFA methods for enrollmentstep: The current step (list,setup, orsetup-verification)flowDirection: The direction of the transition animationmfaCode: The verification code entered by the userauthUrl: The otpauth:// URL for QR code generationsecret: The base32-encoded secret for manual entryinitiatedAt: Timestamp when enrollment was initiated (for timeout)isExpired: Whether the enrollment session has expirederror: Any error that occurred during the processisPending: Whether an async operation is in progressisSuccess: Whether the enrollment was successful
EnrollMfaFlow
TheEnrollMfaFlow component manages the display of different views based on the current step. It handles transitions between:
list: The initial view showing available MFA methodssetup: The QR code/secret display for setting up the authenticatorsetup-verification: The code verification step
EnrollMfaFlow provides a children render prop that receives an object containing the current step, method, and the Content component.
EnrollMfaTitle & EnrollMfaDescription
These components work similarly. By default, they useuseEnrollMfaContext to get the current step and method and render the appropriate title or description.
They also accept step and method props, which will override the values from the context. This is useful during transitions when you need to display the correct content for each transitioning view.
Enrollment lifecycle
The enrollment process follows these steps:- List: User sees available MFA methods and selects one
- Setup: User scans QR code or manually enters secret in their authenticator app
- Setup-verification: User enters the 6-digit code from their authenticator to verify setup
Example: Basic usage
Implement a simple MFA enrollment interface:Example: Custom layout with page title
Customize the layout and add a page-level title:Example: Accessing state
Display messages based on the enrollment state:API Reference
Optional Props
onEnrollSuccess: Callback function invoked when MFA enrollment is successfulresetOnSuccess: Whether to reset the enrollment state when successful (defaults totrue)children: React nodes for custom layout, or a function that receivesEnrollMfaStateclassName: Additional CSS classes to apply to the root element
Notes
- Session timeout: Enrollment sessions expire after 5 minutes for security. If expired, the user must start over.
- Currently supports TOTP only: The component is designed to support multiple MFA methods, but currently only TOTP (authenticator app) is available.
- Authenticator app required: Users need an authenticator app (like Google Authenticator, Authy, or 1Password) to complete enrollment.