/django-jwt

Primary LanguagePythonMIT LicenseMIT

django-jwt

A Django simple Application with Authentication

In this repo the_site is project, having two apps 'auth' and 'home'

Steps how JWT based authentication is implemented

  1. pip install djangorestframework_simplejwt

  2. Settings.py

INSTALLED_APPS = [
    'rest_framework',
]

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ],
}
  1. urls.py
In the_site.auth.urls
    path('hello/', views.HelloView.as_view(), name='hello'),
In the_site.urls
    path('api/token/', jwt_views.TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('api/token/refresh/', jwt_views.TokenRefreshView.as_view(), name='token_refresh'),
  1. In auth.views.py
from django.views import View

class HelloView(APIView):
    permission_classes = (IsAuthenticated,)

    def get(self, request):
        content = {'message': 'Hello, World!'}
        return Response(content)
  1. Do below steps:
  2. python manage.py migrate
  3. python manage.py createsuperuser
  4. pip3 install httpie
  5. Generate Tokens by visiting webpage: http://127.0.0.1:8000/api/token/ OR from cli do curl -u username http://127.0.0.1:8000/api/token/
  6. http http://127.0.0.1:8000/auth/hello/ "Authorization: Bearer "

Screenshot 2021-07-22 at 19 03 22

  1. After 5 minutes, access token will expire. We can get new access token using refresh token http post http://127.0.0.1:8000/api/token/refresh/ refresh=

Screenshot 2021-07-22 at 19 05 02