Skip to main content
OTP (One-Time Password) authentication allows users to sign in using a temporary code sent to their email, providing a passwordless authentication experience.

Configuration

You can enable OTP authentication in your auth collection settings:
Default configuration:
OTP duration must be between 10 and 86400 seconds (24 hours). The minimum password length is 4 characters.

OTP authentication flow

The OTP authentication process consists of two steps:
1

Request OTP

User submits their email to request an OTP code.
Response (200):
The API always returns a 200 response with an otpId (real or dummy) to prevent email enumeration attacks.
2

Receive email

User receives an email with the OTP code (usually 8 digits by default).
3

Authenticate with OTP

User submits the OTP ID and password to complete authentication.
Success response (200):

Request OTP endpoint

POST /api/collections/{collection}/request-otp
Request validation:

Rate limiting and abuse prevention

PocketBase implements several protections against OTP abuse:
  1. Maximum active OTPs: Users can have a maximum of 10 non-expired OTPs. After that, the last issued OTP is reused:
  1. Background email sending: OTP emails are sent in the background to prevent timing attacks:
  1. Dummy responses: Returns a fake otpId if the email doesn’t exist to prevent enumeration:

Authenticate with OTP endpoint

POST /api/collections/{collection}/auth-with-otp
Request body:

Validation and security

The OTP authentication endpoint performs several security checks:
Extra rate limiting: OTP validation has an additional rate limit to prevent brute force attacks:
Users are limited to 5 OTP validation attempts per 3 minutes per record. After exceeding the limit, they must request a new OTP.

Email verification

When a user successfully authenticates with an OTP sent to their email, PocketBase automatically verifies their email if it matches:

OTP cleanup

After successful authentication, the OTP is automatically deleted:

Email template customization

You can customize the OTP email template:
Available placeholders:
  • {APP_NAME} - Your application name
  • {APP_URL} - Your application URL
  • {OTP} - The one-time password
  • {OTP_ID} - The OTP record ID
Example template:

OTP model

The OTP record is stored in the _otps system collection:

Integration with MFA

OTP can be used as one of the authentication methods for multi-factor authentication:
If MFA is enabled, users can authenticate first with password, then with OTP as the second factor.

Use cases

Passwordless authentication

OTP is ideal for applications that want to offer passwordless authentication:
You can implement magic link authentication by encoding the OTP in the email link:

Account recovery

OTP can serve as an alternative recovery method when users forget their password.

Security best practices

OTP codes should be random and unpredictable. By default, PocketBase uses 8-digit numeric codes.
  1. Short expiration: Keep OTP duration short (3-10 minutes) to minimize the attack window.
  2. One-time use: OTPs are automatically deleted after successful authentication.
  3. Rate limiting: Built-in rate limiting prevents brute force attacks on OTP validation.
  4. HTTPS only: Always use HTTPS to prevent OTP interception.
  5. Consider longer codes: For high-security applications, increase the OTP length beyond 8 characters.

Custom OTP hooks

You can customize OTP behavior using event hooks:

Configuration example

Email/Password auth

Traditional password authentication

OAuth2

Social login providers

MFA

Use OTP as second factor

Email templates

Customize email templates