DjangoRestFramework (DRF) User Registration, Login, Logout, and Password Reset with dj-rest-auth and django-allauth

Posted by Ray on May 11, 2022

Updated Feb 14, 2024

This document is a guide for creating a user handling feature - specifically user registration, login, logout, and password reset for Django with DjangoRestFramework. This can be a continuation of the previous walkthrough: “Dockerized Django and PostgreSQL Setup for REST API through DjangoRestFramework (DRF)”.

Pre-requisites

  1. Python
  2. Docker

Install dj-rest-auth for user login/logout/password reset

We use dj-rest-auth since it is currently maintained.

1
2
3
4
5
# Install dj-rest-auth outside docker container
$ docker exec backend_api_web pipenv install dj-rest-auth

# when inside docker container, use the command:
$ pipenv install dj-rest-auth

Add newly installed applications into config/settings.py

1
2
3
4
5
6
7
8
9
10
INSTALLED_APPS = (
    ...,
    # Third-party
    'rest_framework',

    # Authentication  # new
    'rest_framework.authtoken',  # new
    'dj_rest_auth',  # new
    ...
)

Import include in config/urls.py and, modify the urls:

1
2
3
4
5
6
7
8
9
10
# config/urls.py
from django.contrib import admin
from django.urls import path, include  # add include to import

urlpatterns = [
    path('admin/', admin.site.urls),

    # Authentication  # new
    path('auth/', include('dj_rest_auth.urls')),  # new
]

Stop (ctrl + c) and rebuild the docker container (using terminal1).

Migrate the changes

1
2
3
4
5
# Migrate changes
$ docker exec backend_api_web python manage.py migrate

# when inside docker container, use the command:
$ python manage.py migrate

Install django-allauth for user registration

1
2
3
4
5
# Install dj-rest-auth[with_social]
$ docker exec backend_api_web pipenv install 'dj-rest-auth[with_social]'

# when inside docker container, use the command:
$ pipenv install 'dj-rest-auth[with_social]'

Add SITE_ID = 1 and newly installed applications into config/settings.py,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
INSTALLED_APPS = (
    ...,
    'django.contrib.staticfiles',

    # Third-party
    # Authentication
    'django.contrib.sites',  # new
    'allauth',  # new
    'allauth.account',  # new
    'allauth.socialaccount',  # new
    'dj_rest_auth.registration',  # new
    'rest_framework',
    'rest_framework.authtoken',
    'dj_rest_auth',
    ...
)

SITE_ID = 1  # new
...

Add registration path to config/urls.py

1
2
3
4
5
6
7
8
# config/urls.py
...

urlpatterns = [
    ...
    path('auth/', include('dj_rest_auth.urls')),
    path('auth/registration/', include('dj_rest_auth.registration.urls')),  # new
]

Stop (ctrl + c) and rebuild the docker container (using terminal1).
Apply migrations

*** If django.core.exceptions.ImproperlyConfigured: allauth.account.middleware.AccountMiddleware must be added to settings.MIDDLEWARE shows, edit config/settings.py MIDDLEWARE to:

1
2
3
4
5
6
MIDDLEWARE = [
    ...
    'django.middleware.clickjacking.XFrameOptionsMiddleware',

    'allauth.account.middleware.AccountMiddleware',  # new
]

Add REST_FRAMEWORK settings on config/settings.py

1
2
3
4
5
6
7
8
9
10
# config/settings.py
...
REST_FRAMEWORK = {  # new
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication',
    ]
}

SITE_ID = 1
...