Multi-Factor Authentication (MFA) adds an extra layer of security to Embedded Wallets by requiring users to verify their identity through a secondary authentication method. The SDK supports Time-based One-Time Password (TOTP) authentication using authenticator apps like Google Authenticator, Authy, or 1Password.
MFA is optional but strongly recommended for production applications handling significant value or sensitive operations. It provides defense against account takeover attacks even if the primary authentication method is compromised.
Embedded Wallets use TOTP (Time-based One-Time Password) for multi-factor authentication:
Enrollment: Users scan a QR code or manually enter a secret in their authenticator app
Setup verification: Users confirm setup by entering a 6-digit code from their app
Future authentication: Users provide a 6-digit code for sensitive operations
Important: Users must be authenticated (signed in) before they can enroll in MFA or perform MFA verification. MFA is an additional security layer on top of existing authentication methods.
import { initiateMfaEnrollment, submitMfaEnrollment, getCurrentUser} from "@coinbase/cdp-core";async function enrollUserInMfa() { // Step 1: Initiate MFA enrollment (user must be signed in) const enrollment = await initiateMfaEnrollment({ mfaMethod: "totp" }); // Display QR code for user to scan with their authenticator app console.log("Scan this QR code URL:", enrollment.authUrl); // Or display the secret for manual entry console.log("Or enter this secret manually:", enrollment.secret); // Step 2: After user adds to their authenticator app, verify with the 6-digit code const result = await submitMfaEnrollment({ mfaMethod: "totp", mfaCode: "123456" // The 6-digit code from the user's authenticator app }); // After successful enrollment, the user object is updated with MFA information console.log("MFA enrolled for user:", result.user.userId); console.log("MFA enrollment info:", result.user.mfaMethods?.totp); // Output: { enrolledAt: "2024-01-01T00:00:00Z" } // The current user now has MFA enabled const user = await getCurrentUser(); console.log("User MFA status:", user.mfaMethods);}
Report incorrect code
Copy
Ask AI
import { initiateMfaEnrollment, submitMfaEnrollment, getCurrentUser} from "@coinbase/cdp-core";async function enrollUserInMfa() { // Step 1: Initiate MFA enrollment (user must be signed in) const enrollment = await initiateMfaEnrollment({ mfaMethod: "totp" }); // Display QR code for user to scan with their authenticator app console.log("Scan this QR code URL:", enrollment.authUrl); // Or display the secret for manual entry console.log("Or enter this secret manually:", enrollment.secret); // Step 2: After user adds to their authenticator app, verify with the 6-digit code const result = await submitMfaEnrollment({ mfaMethod: "totp", mfaCode: "123456" // The 6-digit code from the user's authenticator app }); // After successful enrollment, the user object is updated with MFA information console.log("MFA enrolled for user:", result.user.userId); console.log("MFA enrollment info:", result.user.mfaMethods?.totp); // The current user now has MFA enabled const user = await getCurrentUser(); console.log("User MFA status:", user.mfaMethods);}
You can check if a user has MFA enabled by examining their user object:
TypeScript
React
Report incorrect code
Copy
Ask AI
import { getCurrentUser } from "@coinbase/cdp-core";async function checkMfaStatus() { const user = await getCurrentUser(); if (user.mfaMethods?.totp) { console.log("MFA is enabled"); console.log("Enrolled at:", user.mfaMethods.totp.enrolledAt); } else { console.log("MFA is not enabled"); }}