Skip to content

dj rest auth

dj-rest-auth package

First we will add log in, log out, and password reset API endpoints. These come out-of-the-box with the popular dj-rest-auth package.

Step 1: Installation

pip install dj-rest-auth==2.1.11

Step 2: Updated INSTALLED_APPS

Add the new app to the INSTALLED_APPS config in our django_project/settings.py file.

settings.py
INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",

    # 3rd-party apps
    "rest_framework",
    "corsheaders",
    "rest_framework.authtoken",
    "dj_rest_auth", # new

    # Local
    "accounts.apps.AccountsConfig",
    "posts.apps.PostsConfig",
]

Step 3: Update urls.py

Update our django_project/urls.py file with the dj_rest_auth package. We’re setting the URL routes to api/v1/dj-rest-auth. Make sure to note that URLs should have a dash - not an underscore _, which is an easy mistake to make.

urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path("admin/", admin.site.urls),
    path("api/v1/", include("posts.urls")),
    path("api-auth/", include("rest_framework.urls")),
    path("api/v1/dj-rest-auth/", include("dj_rest_auth.urls")), # new
]

And we’re done! If you have ever tried to implement your own user authentication endpoints, it is truly amazing how much time and headache dj-rest-auth saves for us. Now we can spin up the server to see what dj-rest-auth has provided.

Run the server

python manage.py runserver

API Log In Endpoint

# Login
http://127.0.0.1:8000/api/v1/dj-rest-auth/login/

# Logout
http://127.0.0.1:8000/api/v1/dj-rest-auth/logout/

# Password Reset
http://127.0.0.1:8000/api/v1/dj-rest-auth/password/reset

# Password Reset Confirm
http://127.0.0.1:8000/api/v1/dj-rest-auth/password/reset/confirm

Reference