Skip to main content

Overview

Django-allauth supports phone number authentication, allowing users to sign up and log in using their phone number instead of (or in addition to) email addresses. This guide covers complete implementation including SMS verification.

Basic Setup

Configure phone as a login method:
settings.py

Phone Number Storage

Django-allauth doesn’t provide a built-in phone model. You must implement storage yourself.

Option 1: Custom User Model

Store phone directly on the user model:
models.py
settings.py

Option 2: Separate Phone Model

Store phone numbers in a related model:
models.py

Adapter Implementation

Implement the required adapter methods for phone functionality:
adapters.py

SMS Provider Integration

Twilio

settings.py

AWS SNS

adapters.py

Phone Number Validation

Customize phone number validation and formatting:
adapters.py
Install phonenumbers library:

Phone Verification Configuration

settings.py

Login with Phone Only

Configure phone-only authentication:
settings.py

Custom Phone Input Widget

Create a better phone input experience:
widgets.py
widgets/phone_input.html

Phone Number Verification Flow

Customize the verification templates:
templates/account/phone_verification.html

Testing Phone Authentication

For development, create a test adapter that logs codes:
adapters.py
settings.py

Security Considerations

  1. Rate Limiting: Phone verification is automatically rate-limited. Configure as needed:
settings.py
  1. Phone Number Enumeration: Consider enabling prevention:
settings.py
  1. SMS Costs: Implement limits on SMS sends to prevent abuse.
  2. Verification Code Security:
    • Codes expire after ACCOUNT_PHONE_VERIFICATION_TIMEOUT
    • Limited attempts via ACCOUNT_PHONE_VERIFICATION_MAX_ATTEMPTS
    • Use strong random codes (handled automatically)

Complete Example

settings.py
This provides a complete phone authentication system with SMS verification.