Skip to content

django-allauth for web apps & dj-rest-auth for API

dj-rest-auth and django-allauth are related but serve different purposes in a Django project, particularly when it comes to user authentication and account management:

Installation
  1. django-allauth:

    • Purpose: django-allauth is a comprehensive authentication solution for Django projects. It provides a set of views, forms, and templates to handle user authentication, registration, password management, social account authentication (OAuth), and email confirmation.
    • Features: Supports various authentication methods (username, email, social accounts), email verification, password management, and more.
    • Integration: It integrates seamlessly with Django's authentication system and provides a customizable set of templates and views.
  2. dj-rest-auth:

    • Purpose: dj-rest-auth is an extension of Django REST Framework (DRF) that provides RESTful endpoints for authentication. It includes views and serializers to handle login, logout, registration, password reset, and user details.
    • Features: Specifically designed for RESTful APIs, it supports token-based authentication (JWT, Token), social authentication using Django-allauth's social accounts, and integrates well with DRF serializers and views.
    • Integration: It can work alongside django-allauth to provide API endpoints for user authentication and registration.
  3. djoser

    • Purpose: djoser is a REST implementation of Django authentication system. It provides API endpoints for authentication and user management, aiming to be flexible and customizable.
    • Features: Offers endpoints like /token/ (for token-based authentication), /token/refresh/, /user/ (for user details), /user/create/, /user/delete/, and more.
    • Integration: Designed to integrate easily with existing Django projects and allows for extensive customization.

Choosing Between dj-rest-auth and djoser

Use dj-rest-auth if:

  • You need a quick setup of authentication endpoints with token-based authentication.
  • Integration with django-allauth for social authentication is desired.

Use djoser if:

  • You prefer a more flexible and customizable solution for authentication and user management endpoints.
  • You want more control over the implementation details of authentication views and serializers.

First, install the python package. If you do not need any of the social account related functionality, install using:

pip install django-allauth

Otherwise, install using:

pip install "django-allauth[socialaccount]"

Then, assuming you have a Django project up and running, add the following to the settings.py of your project:

# Specify the context processors as follows:
TEMPLATES = [
  {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [],
    'APP_DIRS': True,
    'OPTIONS': {
      'context_processors': [
        # Already defined Django-related contexts here

        # `allauth` needs this from django
        'django.template.context_processors.request',
      ],
    },
  },
]

AUTHENTICATION_BACKENDS = [
  ...
  # Needed to login by username in Django admin, regardless of `allauth`
  'django.contrib.auth.backends.ModelBackend',

  # `allauth` specific authentication methods, such as login by email
  'allauth.account.auth_backends.AuthenticationBackend',
  ...
]

INSTALLED_APPS = [
  ...
  # The following apps are required:
  'django.contrib.auth',
  'django.contrib.messages',

  'allauth',
  'allauth.account',
]

MIDDLEWARE = (
  ...
  # Add the account middleware:
  "allauth.account.middleware.AccountMiddleware",
)

# Provider specific settings
SOCIALACCOUNT_PROVIDERS = {
  'google': {
    # For each OAuth based provider, either add a ``SocialApp``
    # (``socialaccount`` app) containing the required client
    # credentials, or list them here:
    'APP': {
      'client_id': '123',
      'secret': '456',
      'key': ''
    }
  }
}

Additionally, add this to your project urls.py:

urlpatterns = [
  ...
  path('accounts/', include('allauth.urls')),
  ...
]

Note that you do not necessarily need the URLs provided by django.contrib.auth.urls. Instead of the URLs login, logout, and password_change (among others), you can use the URLs provided by allauth: account_login, account_logout, account_set_password

Post-Installation

python manage.py migrate

  1. Install package:

    pip install dj-rest-auth
    
  2. Add dj_rest_auth app to INSTALLED_APPS in your django settings.py:

    INSTALLED_APPS = (
      ...,
      'rest_framework',
      'rest_framework.authtoken',
      ...,
      'dj_rest_auth'
    )
    

This project depends on django-rest-framework library, so install it if you haven’t done yet. Make sure also you have installed rest_framework and rest_framework.authtoken apps

  1. Add dj_rest_auth urls:

    urlpatterns = [
      ...,
      path('dj-rest-auth/', include('dj_rest_auth.urls'))
    ]
    
  2. Migrate your database

    python manage.py migrate
    
Registration (optional)
  1. If you want to enable standard registration process you will need to install django-allauth by using pip install 'dj-rest-auth[with_social]'.
  2. Add django.contrib.sites, allauth, allauth.account, allauth.socialaccount and dj_rest_auth.registration apps to INSTALLED_APPS in your django settings.py:
  3. Add SITE_ID = 1 to your django settings.py
INSTALLED_APPS = (
  ...,
  'django.contrib.sites',
  'allauth',
  'allauth.account',
  'allauth.socialaccount',
  'dj_rest_auth.registration',
)

SITE_ID = 1
  1. Add dj_rest_auth.registration urls:
urlpatterns = [
  ...,
  path('dj-rest-auth/', include('dj_rest_auth.urls')),
  path('dj-rest-auth/registration/', include('dj_rest_auth.registration.urls'))
]
Social Authentication (optional)

Using django-allauth, dj-rest-auth provides helpful class for creating social media authentication view.

Points 1 and 2 are related to django-allauth configuration, so if you have already configured social authentication, then please go to step 3. See django-allauth documentation for more details.

  1. Add allauth.socialaccount and allauth.socialaccount.providers.facebook or allauth.socialaccount.providers.twitter apps to INSTALLED_APPS in your django settings.py:
INSTALLED_APPS = (
  ...,
  'rest_framework',
  'rest_framework.authtoken',
  'dj_rest_auth'
  ...,
  'django.contrib.sites',
  'allauth',
  'allauth.account',
  'dj_rest_auth.registration',
  ...,
  'allauth.socialaccount',
  'allauth.socialaccount.providers.facebook',
  'allauth.socialaccount.providers.twitter',

)


Using Both vs. Using One

Using Both (django-allauth and dj-rest-auth):

You can use both django-allauth and dj-rest-auth together in a project:

  • User Registration: Use django-allauth for web-based user registration and email confirmation.
  • API Authentication: Use dj-rest-auth for REST API endpoints to handle token-based authentication, login, and user details retrieval.

Using Only One:

Depending on your project requirements, you can choose to use only one of them:

  • If you're building a web application: You might primarily use django-allauth for its comprehensive user authentication and social authentication features.
  • If you're building a RESTful API: You might choose to use dj-rest-auth for its tailored API endpoints and token-based authentication support.

Considerations

  • Integration: dj-rest-auth can leverage django-allauth's social authentication providers (django-allauth's social accounts) for OAuth-based authentication.
  • Customization: Both packages are highly customizable. You can override views, serializers, and templates to fit your project's specific requirements.
  • Dependency: dj-rest-auth does not require django-allauth to function, but they complement each other well when used together, especially in projects that require both web-based authentication and API-based authentication.

Conclusion

In summary, dj-rest-auth and django-allauth can be used together or separately depending on your project's needs. If you're building a complex application that requires both web-based authentication and REST API endpoints, using both can provide a comprehensive solution. However, if your focus is primarily on APIs, dj-rest-auth alone may suffice for handling token-based authentication and user management. Choose the approach that best fits your project's architecture and requirements.


Reference