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 themfa_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 withmfa_required, redirect the user to complete the step-up challenge:
app/dashboard/page.tsx
MFA Tenant Configuration
Recommended Configuration
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 onmfaContextTtl) 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
- Use HTTPS: Always use HTTPS in production to protect MFA tokens in transit
- Short TTL: Keep
mfaContextTtlshort (5-10 minutes) to limit exposure - Validate Tokens: Always validate
mfa_tokenon the server before using - Rate Limiting: Implement rate limiting on MFA verification endpoints
- Clear Context: Clear MFA context after successful verification
User Experience
- Clear Messaging: Explain why MFA is required for specific resources
- Session Persistence: Don’t require MFA too frequently for the same user
- Error Handling: Provide clear error messages for expired or invalid tokens
- Fallback Options: Offer multiple MFA methods when possible
- Remember Device: Consider implementing “remember this device” functionality
Implementation Tips
- Conditional Enforcement: Only require MFA for truly sensitive operations
- Action-Based: Use Auth0 Actions for flexible MFA enforcement rules
- Testing: Test MFA flows thoroughly in development before production
- Monitoring: Monitor MFA success/failure rates to detect issues
- Documentation: Document which resources require MFA for your team