Skip to main content
Multi-factor authentication (MFA) adds an extra layer of security by requiring users to authenticate using two different methods before gaining access.

Configuration

You can enable MFA in your auth collection settings:
Default configuration:
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:
You cannot enable MFA with only one authentication method. Enable at least two of: Password, OAuth2, or OTP.

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 mfaId.
Success response (200):

MFA model

MFA sessions are stored in the _mfas system collection:

MFA check implementation

The MFA check happens in the RecordAuthResponse function:

First-time authentication

When a user authenticates for the first time (without an mfaId), PocketBase creates an MFA session:

Second-time authentication

When authenticating with an mfaId, 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 the rule field to apply MFA only to specific users:
The rule is evaluated in the 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

OAuth2 + Password

Passing the mfaId

You can pass the mfaId either in the request body or as a query parameter:
The implementation checks both locations:

Configuration example

Security considerations

MFA sessions expire after the configured duration. Users must complete both authentication steps within this time window.

Best practices

  1. Short MFA duration: Keep the MFA session duration reasonable (15-30 minutes) to minimize the risk of session hijacking.
  2. Clear user communication: Inform users that they need to authenticate twice and explain why.
  3. Selective MFA: Use the MFA rule to apply MFA only to high-privilege accounts if needed.
  4. Monitor MFA usage: Track MFA authentications using hooks to detect unusual patterns.
  5. 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:

Email/Password auth

Configure password authentication

OAuth2

Set up social login

OTP

Enable one-time passwords

API rules

Configure access control