Django-Rest User Authentication in 5 Minutes

How to add user authentication feature into your Django Rest application.

Background photo from Unsplash

Before we start, I’m strongly assuming you have
1) Basic understanding of Python
2) Basic understanding of Django and have worked a bit with Django-rest-framework
3) Already have python installed on your laptop

If the above assumptions are correct, then lets proceed.

First of all what is Authentication and why would you need it when your are building either a web or mobile application?

Authentication is a basically a gateway into your application. Normally on a lot of websites out there, you would be allowed to access some kind of informative pages about what the application e.g Pricing Page, Home Page, About Us Page, Contact Us Page etc. but when you need to access couple of other pages, you’d discover that you are asked to either login or register because at this point, they platform owners will;

1) Need a way to identify who is who on the platform
2) Create customize functionalities for users based on their submitted information
3) Store user information
4)Perform so kind of analysis with this information when necessary.

So at a point in the life-cycle of your application you will need to add “this gateway” to be able to stay informed on how and who is using your system and the growth of your application in relation to number of users. It is because of the above mentioned reasons and many more that authentication features are integrated into systems.

How do i add this authentication feature to my Django Rest application then?

Let’s begin by opening our preferred terminal and typing the following

This will create a folder called djangoauthproject, we then navigate into the folder and create our virtual environment and then go ahead to activate it. With our virtualenv activated, we installed django,djangorestframework, PyJWT and created our project.

You should see the following folder structure when you navigate to your project

Next create an app called app_account using the following command

At this point your project structure should be looking like this

Head over to the INSTALLED_APPS in your DjangoUserAuth file and register the new app you just created:

Open the file in your DjangoUserAuth folder and add the following lines of code

Next add a and in your app_account folder and your folder should be looking like this at this point

Inside your app_account add the following code

I normally like extending the default django User using AbstractUser in case there is need to add more things to the model in the future.

Next in your app_account file add the following code

for the Login, we are using pyjwt library, you can checkout more on their official doc
In your app_account file add the following

Add the following in your app_account file

Finally in your DjangoUserAuth file add the following

We ensured django knows to use our defined custom user model and not the default user model. At this point we can go ahead and makemigrations, migrate and start our server

With our server running, if you navigate to your browser and visit, you should see your django rest generated registration page. Go ahead a register a new user. Then navigate to and login in using the user credentials. A token should be generated for you on successfully login, you can copy the token and visit and paste the encoded token and it will be decoded for you.

At this point we have successfully added an authentication feature to our django rest application.

I hope you found this article helpful. Thanks for reading.

A fullstack software developer constantly building shit tone of things with python and javascript and occasionally do some writing.

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