Capsule supports Two-Factor Authentication (2FA) for secure wallet recovery, adding an extra layer of security to your users’ accounts. This guide will walk you through the process of implementing 2FA in your application using Capsule.

Overview

Two-Factor Authentication is a security measure that requires users to provide two different authentication factors to verify their identity. In the context of Capsule, 2FA is used primarily for secure wallet recovery.

If you’re using the CapsuleModal, 2FA is enabled by default. To disable this feature, refer to the

Customize Capsule section

.

For developers using the Web and Mobile SDKs directly, Capsule provides several methods to implement 2FA. This guide will cover how to use these methods effectively.

Implementing 2FA

Here’s a step-by-step guide to implement 2FA in your application:

1. Check 2FA Status

Before setting up 2FA, you should check if it’s already enabled for the user:

const checkTwoFactorStatus = async () => {
  const { isSetup } = await capsuleClient.check2FAStatus();
  if (isSetup) {
    // Proceed to verification if needed
  } else {
    // Proceed to 2FA setup
  }
};

2. Set Up 2FA

If 2FA isn’t set up, you can initiate the setup process:

const setupTwoFactor = async () => {
  const { uri } = await capsuleClient.setup2FA();
  if (uri) {
    // Display the QR code to the user or provide the secret for manual entry
  } else {
    // Handle case where no URI is returned
  }
};

The uri returned by setup2FA() can be used to generate a QR code or extracted to provide the secret key to the user.

3. Enable 2FA

After the user has added the 2FA secret to their authenticator app, you need to verify and enable 2FA:

const enableTwoFactor = async (verificationCode: string) => {
  await capsuleClient.enable2FA(verificationCode);
  // Notify the user of successful 2FA setup
};

4. Verify 2FA

When authenticating, you’ll need to verify the user’s 2FA code:

const verifyTwoFactor = async (email: string, verificationCode: string) => {
  await capsuleClient.verify2FA(email, verificationCode);
  // Proceed with the authentication process
};

Conclusion

Implementing Two-Factor Authentication with Capsule adds an essential layer of security to your application’s wallet recovery process. By following this guide and best practices, you can provide your users with a secure and user-friendly 2FA experience.