Configuration
You can enable MFA in your auth collection settings:MFA duration must be between 10 and 86400 seconds (24 hours). This is the time window allowed between the first and second authentication.
Requirements
To enable MFA, you must have at least 2 authentication methods enabled:MFA authentication flow
The MFA process requires two separate authentication steps:1
First authentication
User authenticates using any enabled method (password, OAuth2, or OTP).Response (401 Unauthorized):
Instead of returning an auth token, PocketBase returns an
mfaId when MFA is required.2
Second authentication
User authenticates again using a different method, including the Success response (200):
mfaId.MFA model
MFA sessions are stored in the_mfas system collection:
MFA check implementation
The MFA check happens in theRecordAuthResponse function:
First-time authentication
When a user authenticates for the first time (without anmfaId), PocketBase creates an MFA session:
Second-time authentication
When authenticating with anmfaId, PocketBase validates the MFA session:
Users must use a different authentication method for the second factor. You cannot authenticate twice with the same method.
MFA rule (selective MFA)
You can use therule field to apply MFA only to specific users:
wantsMFA function:
If the MFA rule evaluation fails, PocketBase defaults to requiring MFA as a security precaution.
MFA cleanup
MFA sessions are automatically cleaned up in several scenarios:Successful authentication
After successful second-factor authentication, the MFA session is deleted:Expired sessions
A cron job runs every hour to delete expired MFA sessions:Password changes
All MFA sessions are deleted when a user changes their password:Example MFA flows
Password + OTP
- Step 1: Password
- Step 2: OTP
OAuth2 + Password
- Step 1: OAuth2
- Step 2: Password
Passing the mfaId
You can pass themfaId either in the request body or as a query parameter:
Configuration example
Security considerations
Best practices
- Short MFA duration: Keep the MFA session duration reasonable (15-30 minutes) to minimize the risk of session hijacking.
- Clear user communication: Inform users that they need to authenticate twice and explain why.
- Selective MFA: Use the MFA rule to apply MFA only to high-privilege accounts if needed.
- Monitor MFA usage: Track MFA authentications using hooks to detect unusual patterns.
- Backup authentication: Ensure users have access to multiple authentication methods in case one fails.
Custom MFA hooks
You can customize MFA behavior using the auth hooks:Error handling
Handle MFA-related errors appropriately:Related topics
Email/Password auth
Configure password authentication
OAuth2
Set up social login
OTP
Enable one-time passwords
API rules
Configure access control