Email Magic Link with Multi-Factor Authentication
This guide shows you how to implement authentication with Email Magic Link and TOTP-based multi-factor authentication in your React application.
Overview
When a user has MFA enabled with an authenticator app (TOTP), the login flow requires the following steps:
- The user enters their email address to request a magic link
- If MFA is enabled for their account, they’re prompted to enter the 6-digit TOTP code from their authenticator app (e.g., Google Authenticator)
- After entering a valid TOTP code, a magic link is sent to their email
- The user clicks the magic link from email to complete authentication
- Upon successful verification, the user is authenticated and redirected to the appropriate page
This two-factor approach provides an additional layer of security beyond a standard magic link.
Implementation
Step 1: Initialize Authentication and Handle MFA Required Error
First, attempt to authenticate with email. If MFA is required, an error will be thrown. You can handle this error by prompting the user to enter their TOTP code.
Step 2: Submit TOTP Code and Complete Magic Link Authentication
Once we have the MFA data from the first step, we can complete the authentication by submitting the TOTP code with the multiFactorId. You must prompt the user to enter their TOTP code (from their authenticator app) and then submit it with the multiFactorId.
Step 3: Handle the Magic Link Redirect
When the user clicks the magic link in their email, your application needs to handle the redirect and complete the authentication.
The magic link will redirect to your application with a bundle parameter. You must submit this bundle to the authenticate
function to complete the authentication.