Configuration
You can enable OTP authentication in your auth collection settings: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
Rate limiting and abuse prevention
PocketBase implements several protections against OTP abuse:- Maximum active OTPs: Users can have a maximum of 10 non-expired OTPs. After that, the last issued OTP is reused:
- Background email sending: OTP emails are sent in the background to prevent timing attacks:
- Dummy responses: Returns a fake
otpIdif the email doesn’t exist to prevent enumeration:
Authenticate with OTP endpoint
POST/api/collections/{collection}/auth-with-otp
Validation and security
The OTP authentication endpoint performs several security checks: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:{APP_NAME}- Your application name{APP_URL}- Your application URL{OTP}- The one-time password{OTP_ID}- The OTP record ID
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:Use cases
Passwordless authentication
OTP is ideal for applications that want to offer passwordless authentication:Magic links
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
- Short expiration: Keep OTP duration short (3-10 minutes) to minimize the attack window.
- One-time use: OTPs are automatically deleted after successful authentication.
- Rate limiting: Built-in rate limiting prevents brute force attacks on OTP validation.
- HTTPS only: Always use HTTPS to prevent OTP interception.
- 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
Related topics
Email/Password auth
Traditional password authentication
OAuth2
Social login providers
MFA
Use OTP as second factor
Email templates
Customize email templates