JWT Authentication Example

A snippet showing how to enable JWT authentication in DRF.


# Install SimpleJWT
pip install djangorestframework-simplejwt
      

# settings.py

REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": [
        "rest_framework_simplejwt.authentication.JWTAuthentication",
    ],
}
      

# urls.py

from django.urls import path
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView

urlpatterns = [
    path("api/jwt/token/", TokenObtainPairView.as_view(), name="jwt-obtain"),
    path("api/jwt/refresh/", TokenRefreshView.as_view(), name="jwt-refresh"),
]
      
Explanation:
  • Obtain access/refresh tokens via /api/jwt/token/; refresh to get a new access token.
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.