Simple JWT-based authentication designed for Django and Django Ninja. This package aims to provide a minimalistic approach to JWT authentication with the least amount of dependencies, making it easy to integrate and use in your projects.
pip install django-ninja-jwt-basic
Add the following settings to your Django settings:
JWT_SECRET_KEY = 'your_secret key' # Required
Add the app to your INSTALLED_APPS
in your Django settings:
INSTALLED_APPS = [
...
'django_ninja_jwt_basic',
...
]
Next, add router to your Django Ninja API and protect your endpoints
from ninja import NinjaAPI
from django_ninja_jwt_basic import JWTAuth
api = NinjaAPI(auth=JWTAuth())
api.add_router('/auth', 'django_ninja_jwt_basic.router')
This will add the following endpoint to your API:
-
/auth/login
- POST - Login endpoint-
Request body:
{ "username": "your_username", "password": "your_password" }
-
Response body:
{ "token": "your_access_token" }
-
If you don't want protect all endpoints, you can use JWTAuth
class directly in your endpoints or routers like below:
from ninja import Router
from django_ninja_jwt_basic import JWTAuth
router = Router(auth=JWTAuth())
@router.get('/protected')
def protected(request):
return {'message': 'This is a protected endpoint'}
from django_ninja_jwt_basic import JWTAuth
@api.get('/protected', auth=JWTAuth())
def protected(request):
return {'message': 'This is a protected endpoint'}
You can find more information about protecting endpoints in the Django Ninja documentation