How to make Token-Based Auth Rest API using Django rest framework

Photo by Pakata Goh on Unsplash

Hello, Django Developers..!!

Rest API is an important aspect as we see it from a backend point of view. There are many frameworks from where you can make rest API. Django is one of the powerful frameworks to build an API. It provides you a cool library that will help you to build an easy token-based or session-based authentication system. It has many other built-in packages too that will make the developer’s work easy. Let’s build a Django Rest API auth system with all endpoints. Grab your seat and take a cup of coffee and start this Django project.

Set-up of Django Rest Auth API

1. Create a virtual environment and name it “Auth”.
2. Activate the environment.
3. Install Django.
4. Install Django Rest Framework.
You will see something like this

I use Linux so the output is something like this.
5. Start the Django project: django-admin startproject AuthProject
6. Move to the directory “AuthProject”
7. write the command in terminal: python manage.py startapp mainApp

The tree format of this project is:
.
| — AuthProject
| | — __init__.py
| | — __pycache__
| | | — __init__.cpython-38.pyc
| | ` — settings.cpython-38.pyc
| | — asgi.py
| | — settings.py
| | — urls.py
| ` — wsgi.py
| — mainApp
| | — __init__.py
| | — admin.py
| | — apps.py
| | — migrations
| | ` — __init__.py
| | — models.py
| | — tests.py
| ` — views.py
` — manage.py

8. Install one more package by using this command: pip install django-rest-auth

Code of Django Authentication REST API

Now open this project in your favorite text editor.
Update the Installed Apps in settings.py

INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',#third part apps'rest_framework','rest_framework.authtoken','rest_auth',#Apps'mainApp',]

Now open the urls.py in the “AuthProject” folder. and update the following code accordingly.

from django.contrib import adminfrom django.urls import path, includeurlpatterns = [path('admin/', admin.site.urls),path('rest-auth/', include('rest_auth.urls'))]

We are good to go for now. Open the terminal and migrate the apps.
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
Something like this:

Now if you want to enable the standard registration process then you have to install some other packages too. So, let’s install it first.

pip install django-rest-auth[with_social]

After the completion of this package. Now, add some third-party apps in Installed apps in settings.py.
And set the SITE_ID = 1.

INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',#third part apps'rest_framework','rest_framework.authtoken','rest_auth','django.contrib.sites','allauth','allauth.account','rest_auth.registration',#Apps'mainApp',]SITE_ID = 1

For social authentication, you can add the apps too. Because we already installed the desired package.
Change the urls.py too. Add one line of code to it.

urlpatterns = [path('admin/', admin.site.urls),path('rest-auth/', include('rest_auth.urls')),path('rest-auth/registration/',include('rest_auth.registration.urls'))]

Now migrate once again and start the localhost server. The API endpoints are:

1. User Registration
http://localhost:8000/rest-auth/registration/

2. User Login
http://localhost:8000/rest-auth/login/

3. User Logout
http://localhost:8000/rest-auth/logout/

4. Change Password
http://localhost:8000/rest-auth/password/change/

5. Password Reset
http://localhost:8000/rest-auth/password/reset/

6. Password Reset Confirmation
http://localhost:8000/rest-auth/password/reset/confirm/

7. User
http://localhost:8000/rest-auth/user/

Congrats..!! you successfully made a Django Rest API Authentication System. It’s a simple Django project with simple python code. Now you can handle the authentication of Django REST API.

Thank You.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store