Skip to main content
Adapters in django-allauth provide a powerful way to customize the authentication flow without modifying the core library. They allow you to override specific behaviors such as user creation, email sending, password validation, and more.

Overview

django-allauth uses the adapter pattern to provide customization points throughout the authentication process. There are two main adapter classes:
  • DefaultAccountAdapter - Handles account-related functionality (allauth/account/adapter.py:52)
  • DefaultSocialAccountAdapter - Handles social authentication functionality (allauth/socialaccount/adapter.py:20)

Account Adapter

Configuration

To use a custom account adapter, set ACCOUNT_ADAPTER in your settings:

Creating a Custom Adapter

Create your adapter by subclassing DefaultAccountAdapter:

Key Adapter Methods

User Management

new_user(request)

Source: allauth/account/adapter.py:290 Instantiates a new User instance.

save_user(request, user, form, commit=True)

Source: allauth/account/adapter.py:321 Saves a new User instance using information provided in the signup form.

populate_username(request, user)

Source: allauth/account/adapter.py:297 Fills in a valid username, if required and missing.

Validation Methods

clean_username(username, shallow=False)

Source: allauth/account/adapter.py:360 Validates the username. Hook into this to restrict what usernames can be chosen.

clean_email(email)

Source: allauth/account/adapter.py:383 Validates an email value. Hook into this to restrict what email addresses can be chosen.

clean_password(password, user=None)

Source: allauth/account/adapter.py:390 Validates a password. Hook into this to restrict allowed password choices.

Signup Control

is_open_for_signup(request)

Source: allauth/account/adapter.py:281 Checks whether or not the site is open for signups.

Email Handling

send_mail(template_prefix, email, context)

Source: allauth/account/adapter.py:212 Sends an email message.

format_email_subject(subject)

Source: allauth/account/adapter.py:155 Formats the email subject.

get_from_email()

Source: allauth/account/adapter.py:165 Returns the ‘from’ email address for sending emails.

render_mail(template_prefix, email, context, headers=None)

Source: allauth/account/adapter.py:172 Renders an email message.

Authentication

authenticate(request, **credentials)

Source: allauth/account/adapter.py:731 Authenticates a user. Handles rate limiting.

pre_authenticate(request, **credentials)

Source: allauth/account/adapter.py:720 Called before authentication. Can be used for rate limiting or other checks.

authentication_failed(request, **credentials)

Source: allauth/account/adapter.py:751 Called when authentication fails.

Login and Logout

pre_login(request, user, *, email_verification, signal_kwargs, email, signup, redirect_url)

Source: allauth/account/adapter.py:491 Called just before logging in the user.

post_login(request, user, *, email_verification, signal_kwargs, email, signup, redirect_url)

Source: allauth/account/adapter.py:505 Called right after logging in the user.

login(request, user)

Source: allauth/account/adapter.py:544 Performs the login.

logout(request)

Source: allauth/account/adapter.py:564 Performs the logout.

Redirect URLs

get_login_redirect_url(request)

Source: allauth/account/adapter.py:229 Returns the default URL to redirect to after logging in.

get_logout_redirect_url(request)

Source: allauth/account/adapter.py:247 Returns the URL to redirect to after logout.

get_signup_redirect_url(request)

Source: allauth/account/adapter.py:223 Returns the default URL to redirect to after signing up.

get_email_verification_redirect_url(email_address)

Source: allauth/account/adapter.py:256 The URL to return to after email verification.

Messages

add_message(request, level, message_template=None, message_context=None, extra_tags="", message=None)

Source: allauth/account/adapter.py:411 Wrapper of django.contrib.messages.add_message.

Social Account Adapter

Configuration

To use a custom social account adapter:

Creating a Custom Social Adapter

Key Social Adapter Methods

pre_social_login(request, sociallogin)

Source: allauth/socialaccount/adapter.py:45 Invoked just after a user successfully authenticates via a social provider.

on_authentication_error(request, provider, error=None, exception=None, extra_context=None)

Source: allauth/socialaccount/adapter.py:60 Invoked when there is an error in the authentication cycle.

new_user(request, sociallogin)

Source: allauth/socialaccount/adapter.py:88 Instantiates a new User instance.

save_user(request, sociallogin, form=None)

Source: allauth/socialaccount/adapter.py:94 Saves a newly signed up social login.

populate_user(request, sociallogin, data)

Source: allauth/socialaccount/adapter.py:109 Hook to further populate the user instance.

is_open_for_signup(request, sociallogin)

Source: allauth/socialaccount/adapter.py:156 Checks whether or not the site is open for signups via social accounts.

is_auto_signup_allowed(request, sociallogin)

Source: allauth/socialaccount/adapter.py:151 Determines if automatic signup is allowed.

Complete Example

Here’s a comprehensive example combining multiple customizations:

Error Messages

You can customize error messages by overriding the error_messages dictionary:
Available error message keys are defined in allauth/account/adapter.py:60.

Best Practices

  1. Always call super() - Unless you’re completely replacing functionality, call the parent method
  2. Use logging - Log important events for debugging and security auditing
  3. Handle exceptions - Wrap custom logic in try/except blocks
  4. Test thoroughly - Adapter methods are called during critical authentication flows
  5. Keep it focused - Each method should have a single responsibility
  6. Document customizations - Comment why custom logic is needed