This repository provides a boilerplate template for setting up JWT authentication in a Django project using the Django REST Framework (DRF). It's a starting point for building secure, token-based authentication systems for Django REST APIs.
- JWT Authentication: Implements secure JSON Web Token-based authentication.
- Django REST Framework: Provides a robust API setup.
- User Registration & Login: Supports authentication using email & password.
- Token Refresh: Includes token refresh mechanism.
- Swagger API Documentation: Auto-generated API docs for easy testing.
- Monitoring & Debugging: Integrated Sentry Middleware for real-time error tracking and performance monitoring.
- Performance Profiling: Planning to integrate cProfile Middleware for in-depth performance analysis.
- Code Quality Tools: Pre-configured Pylint, Black, and Pre-commit hooks for maintaining clean code.
- Type Checking & Import Sorting (optional): Uses Mypy for enhanced development.
- Unit Testing: Uses pytest, pytest-django for testing
- Python 3.10+
- Django 4.x or higher
- Django REST Framework
- PostgreSQL or SQLite (for database)
- Virtual environment (recommended)
-
Clone the repository:
git clone https://github.com/madhvi-in/django-jwt-auth-template.git cd django-jwt-auth-template
-
Set up a virtual environment:
python -m venv venv source venv/bin/activate # For macOS/Linux venv\Scripts\activate # For Windows
-
Install the dependencies:
pip install -r requirements.txt
-
Apply migrations:
python manage.py migrate
-
Create a superuser to access the admin panel:
python manage.py createsuperuser
-
Run the development server:
python manage.py runserver
-
Access the API at
http://127.0.0.1:8000/api/v1/auth/
. -
Set Up Google OAuth Credentials
- Go to the Google Cloud Console → Create a new project. 2 .Navigate to APIs & Services → Credentials.
- Click Create Credentials → OAuth 2.0 Client ID.
- Set Authorized redirect URIs:
- Save the Client ID and Client Secret.
- POST
/api/v1/auth/register/
: Register a new user. - POST
/api/v1/auth/login/
: Login with your credentials to get a JWT token. - POST
/api/v1/auth/token/refresh/
: Refresh the JWT token. - GET
api/docs
: API documentation
This template comes with pre-configured pre-commit hooks to ensure code quality:
-
Install pre-commit:
pip install pre-commit
-
Set up the hooks:
pre-commit install && pre-commit install-hooks
-
Run hooks manually for all files:
pre-commit run --all-files
The hooks will now automatically run before every commit.
-
You can pass the environment variables when building the Docker image:
docker build --env-file .env -t django-jwt-auth-template .
-
Then, when running the container, pass the environment variables to the container:
docker run -d -p 8000:8000 --env-file .env --name django-auth django-jwt-auth-template