Skip to main content

Overview

Multi-Factor Authentication (MFA) adds an additional layer of security by requiring users to provide multiple forms of verification. The SDK supports step-up authentication, where users can access basic resources but must complete MFA to access sensitive data.

Step-up Authentication

Step-up authentication is a pattern where an application allows access to some resources, but requires the user to authenticate with a stronger mechanism (like MFA) to access sensitive resources. The SDK handles the mfa_required error from Auth0 when an API requires higher security. This typically happens when you use an Auth0 Action or Rule to enforce MFA for specific audiences or scopes.

Handling MfaRequiredError

When you request an Access Token for a resource that requires MFA, Auth0 returns a 403 Forbidden with an mfa_required error code. The SDK automatically catches this and throws an MfaRequiredError containing the mfa_token needed to resolve the challenge.

Server-Side Error Handling (API Route)

app/api/protected/route.ts

Client-Side MFA Challenge Flow

When the client receives the 403 with mfa_required, redirect the user to complete the step-up challenge:
app/dashboard/page.tsx

MFA Tenant Configuration

The SDK relies on background token refreshes to maintain user sessions. For these non-interactive requests to succeed, configure your MFA policies appropriately.Enforcing “Always” or “All Applications” in your global Tenant MFA Policy will block background refresh requests, as they cannot satisfy an interactive MFA challenge.
1

Set Tenant MFA Policy

In your Auth0 Dashboard, set the Tenant MFA Policy to “Adaptive” or “Never”.
2

Create Auth0 Action for Conditional MFA

Use Auth0 Actions to enforce MFA conditionally based on the resource being accessed.
3

Configure Action Trigger

Attach the Action to the Login flow in your Auth0 Dashboard.
For more information on customizing MFA flows using post-login Actions, see the Auth0 documentation.

MFA Error Types

The SDK provides specific error classes for different MFA scenarios:

Handling Different MFA Errors

app/api/mfa/verify/route.ts

MFA Context Configuration

Configure MFA token TTL via constructor options or environment variables:
lib/auth0.ts
.env.local
Default TTL is 300 seconds (5 minutes), matching Auth0’s mfa_token expiration.

Session Context Management

When MFA is required, the SDK automatically stores MFA context in the session keyed by a hash of the raw token.

Automatic Cleanup

The MFA context is cleaned up automatically when the session is written. Expired contexts (based on mfaContextTtl) are removed to prevent session bloat.

Context Structure

Complete MFA Flow Example

Step 1: Protected API Route

app/api/sensitive-data/route.ts

Step 2: MFA Challenge Page

app/mfa-challenge/page.tsx

Step 3: MFA Verification API Route

app/api/mfa/verify/route.ts

Best Practices

Security Considerations

  1. Use HTTPS: Always use HTTPS in production to protect MFA tokens in transit
  2. Short TTL: Keep mfaContextTtl short (5-10 minutes) to limit exposure
  3. Validate Tokens: Always validate mfa_token on the server before using
  4. Rate Limiting: Implement rate limiting on MFA verification endpoints
  5. Clear Context: Clear MFA context after successful verification

User Experience

  1. Clear Messaging: Explain why MFA is required for specific resources
  2. Session Persistence: Don’t require MFA too frequently for the same user
  3. Error Handling: Provide clear error messages for expired or invalid tokens
  4. Fallback Options: Offer multiple MFA methods when possible
  5. Remember Device: Consider implementing “remember this device” functionality

Implementation Tips

  1. Conditional Enforcement: Only require MFA for truly sensitive operations
  2. Action-Based: Use Auth0 Actions for flexible MFA enforcement rules
  3. Testing: Test MFA flows thoroughly in development before production
  4. Monitoring: Monitor MFA success/failure rates to detect issues
  5. Documentation: Document which resources require MFA for your team

Troubleshooting

Common Issues

Debug Tips

Enable logging to troubleshoot MFA issues:

Further Reading