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
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.
API Log In Endpoint