Authenticating your Django REST Framework with “Djoser”

Eyuel Nigussie
2 min readJul 14, 2023

--

A step by step guide on how to install Djoser in a Django REST Application

Eyuel Nigussie — System Engineer

Introduction

Today we are going to dive into “Django REST framework” and explore the power of “Djoser “ , a versatile token based authentication and user management library. The main focus will be in what Djoser is and how to set it up.

Django REST framework (DRF)

DRF is a powerful and flexible toolkit for building Web APIs using Django framework. It serves as a bridge between Django, a popular python web framwork, and the modern requirements of building RESTful APIs. DRF provides a set of tools and libraries that enables developers to quickly and efficiently develop APIs, handling tasks such as serialization, authentication, permission and more.

Now that we know what DRF is, lets see what Djoser is and directly go into setting it up

Djoser

Djoser is a simple yet powerful authentication and user management library for Django Rest Framework. It provides a comprehensive set of features and functionalities to handle user registration, login, password reset, account activation and other authentication-related tasks in.

Setting up Djoser

Assuming you have a Django project and you have already created an environment variable, I will directly start from installing Djoser and Django Rest Framework to your already created Django project.

1. Installing Djoser

Open command line, make sure your environment variable is activated then install the following back to back.

pip install djoser
pip install djangorestframework

2. Configuring Djoser and Rest Framework

Add the following in the settings.py. Open the file and locate ‘Installed up section’. Then drop this code

INSTALLED_APPS = [
...
'rest_framework',
'djoser',
...
]

3. Add djoser to urls.py

In your main project folder, add djoser urls and url jwt to your url list

urlpatterns = patterns('',
...
path('auth/', include('djoser.urls')),
path('auth/', include('djoser.urls.jwt')),
...
)

4. Migrate to Database

Run database migration command to create necessary tables for Djoser.

python manage.py makemigrations
python manage.py migrate

Now the basic installation is done. Basic endpoint I listed below are accessible for you. All you need to now is customize Djoser as your needs. Refer to the docs for more: https://djoser.readthedocs.io/en/latest/base_endpoints.html .

PS: Stay Tuned as I will be posting advanced topics about advanced djoser stuff and other mouth watering topics.

/users/

/users/me/

/users/confirm/

/users/resend_activation/

/users/set_password/

/users/reset_password/

/users/reset_password_confirm/

/users/set_username/

/users/reset_username/

/users/reset_username_confirm/

/token/login/ (Token Based Authentication)

/token/logout/ (Token Based Authentication)

/jwt/create/ (JSON Web Token Authentication)

/jwt/refresh/ (JSON Web Token Authentication)

/jwt/verify/ (JSON Web Token Authentication)

Cheers.

--

--