Django REST Auth
dj-rest-auth provides a set of customizable REST API endpoints for user authentication and registration in Django REST Framework, leveraging django-allauth for extended functionality. It is actively maintained with frequent releases, currently at version 7.2.0, supporting modern Django and Python versions.
Warnings
- breaking Version 7.0.0 dropped support for Python versions older than 3.10 and Django versions older than 4.2. Ensure your environment meets these new minimum requirements.
- gotcha When using `dj-rest-auth.registration` (which depends on `django-allauth`), ensure `allauth.account.middleware.AccountMiddleware` is correctly configured and placed in your `MIDDLEWARE` settings, typically after `django.contrib.sessions.middleware.SessionMiddleware`.
- gotcha As of v7.1.0, email and username requirement checks for registration are primarily managed through the `SIGNUP_FIELDS` setting within the `REST_AUTH` dictionary. Relying on older methods or direct `django-allauth` settings for these might lead to unexpected behavior.
- gotcha Multi-Factor Authentication (MFA/2FA) support, introduced in v7.2.0, is an opt-in feature. It requires installing the extra `dj-rest-auth[with_mfa]` and `django-mfa` package, and then including `dj_rest_auth.mfa` in `INSTALLED_APPS`.
- gotcha The `REST_AUTH` settings provide extensive customization options for serializers (e.g., `LOGIN_SERIALIZER`, `USER_DETAILS_SERIALIZER`). Incorrectly overriding these or using an incompatible custom serializer can break core authentication flows.
Install
-
pip install dj-rest-auth -
pip install "dj-rest-auth[with_social]" -
pip install "dj-rest-auth[with_jwt]" -
pip install "dj-rest-auth[with_mfa]"
Imports
- LoginView
from dj_rest_auth.views import LoginView
- RegisterView
from dj_rest_auth.registration.views import RegisterView
- UserDetailsView
from dj_rest_auth.views import UserDetailsView
- PasswordResetConfirmView
from dj_rest_auth.views import PasswordResetConfirmView
Quickstart
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api-auth/', include('rest_framework.urls')), # Optional: for browsable API
path('dj-rest-auth/', include('dj_rest_auth.urls')), # For login, logout, password reset etc.
path('dj-rest-auth/registration/', include('dj_rest_auth.registration.urls')) # For registration
]
# In your settings.py:
# INSTALLED_APPS = [
# # ... Django and DRF apps
# 'rest_framework',
# 'rest_framework.authtoken', # For TokenAuthentication
# 'dj_rest_auth',
# 'dj_rest_auth.registration',
# 'allauth', # Required for dj_rest_auth.registration
# 'allauth.account',
# 'allauth.socialaccount', # Optional: if using social login
# # ... other apps
# ]
#
# REST_FRAMEWORK = {
# 'DEFAULT_AUTHENTICATION_CLASSES': [
# 'rest_framework.authentication.SessionAuthentication',
# 'rest_framework.authentication.TokenAuthentication',
# # 'dj_rest_auth.jwt_auth.JWTAuthentication', # If using JWT
# ],
# 'DEFAULT_PERMISSION_CLASSES': [
# 'rest_framework.permissions.IsAuthenticated',
# ],
# }
#
# # django-allauth settings (required by dj_rest_auth.registration)
# ACCOUNT_AUTHENTICATION_METHOD = 'username_email'
# ACCOUNT_EMAIL_REQUIRED = True
# ACCOUNT_EMAIL_VERIFICATION = 'optional' # or 'mandatory'
# ACCOUNT_LOGOUT_ON_PASSWORD_CHANGE = True
#
# # dj-rest-auth specific settings
# REST_AUTH = {
# 'USE_JWT': False, # Set to True if you installed with_jwt
# 'USER_DETAILS_SERIALIZER': 'dj_rest_auth.serializers.UserDetailsSerializer',
# 'LOGIN_SERIALIZER': 'dj_rest_auth.serializers.LoginSerializer',
# 'REGISTER_SERIALIZER': 'dj_rest_auth.registration.serializers.RegisterSerializer',
# }