How to Use JWT Authentication in Django REST framework

JWT authentication is a popular authentication method for web applications and APIs. It is a stateless authentication mechanism, which means that the user's credentials are stored on the client side and not on the server side. This makes it more secure and easier to use. In this tutorial, we will learn how to use JWT authentication in Django REST framework.

Install the Django REST framework JWT package

The first step is to install the Django REST framework JWT package. This package provides a set of tools for implementing JWT authentication in Django REST framework. To install the package, run the following command in your terminal:

pip install djangorestframework-jwt

Once the package is installed, you can add it to your Django project by adding it to the INSTALLED_APPS list in your settings.py file:

INSTALLED_APPS = [ ... 'rest_framework_jwt', ]

Add the JWT authentication settings to your Django project

Once the package is installed, you can add the JWT authentication settings to your Django project. To do this, add the following settings to your settings.py file:

JWT_AUTH = { 'JWT_EXPIRATION_DELTA': datetime.timedelta(days=7), 'JWT_ALLOW_REFRESH': True, 'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=14), }

The JWT_EXPIRATION_DELTA setting specifies the amount of time a JWT token is valid for. The JWT_ALLOW_REFRESH setting specifies whether or not a JWT token can be refreshed. The JWT_REFRESH_EXPIRATION_DELTA setting specifies the amount of time a refreshed JWT token is valid for.

Add the JWT authentication middleware to your Django project

Once the JWT authentication settings are added to your Django project, you can add the JWT authentication middleware to your Django project. To do this, add the following middleware to your MIDDLEWARE list in your settings.py file:

MIDDLEWARE = [ ... 'rest_framework_jwt.middleware.JWTMiddleware', ]

This middleware will handle the authentication of JWT tokens. It will check for a valid JWT token in the request and authenticate the user if the token is valid.

Add the JWT authentication URLs to your Django project

Once the JWT authentication middleware is added to your Django project, you can add the JWT authentication URLs to your Django project. To do this, add the following URLs to your urls.py file:

urlpatterns = [ ... path('api-token-auth/', obtain_jwt_token), path('api-token-refresh/', refresh_jwt_token), ]

The obtain_jwt_token URL is used to obtain a JWT token. The refresh_jwt_token URL is used to refresh a JWT token.

Conclusion

In this tutorial, we have learned how to use JWT authentication in Django REST framework. We have installed the Django REST framework JWT package, added the JWT authentication settings to our Django project, added the JWT authentication middleware to our Django project, and added the JWT authentication URLs to our Django project. With these steps, we can now use JWT authentication in our Django REST framework applications.

Useful Links