A sample project demonstrating Django DRF authentication using JWT and integrating it with a React frontend. This repository showcases best practices for handling authentication, including token-based authentication, API connections with Axios interceptors, and protected routes in React.
- π Authentication: Uses JWT authentication via
djangorestframework-simplejwt
. - ποΈ Database: PostgreSQL for robust data storage.
- π CORS Handling: Configured using
django-cors-headers
to allow frontend API requests. - π‘ REST API: Built with Django Rest Framework (DRF) to provide secure endpoints.
- π JWT Authentication: Handles user login, logout, and token storage.
- β‘ Axios Interceptors: Automatically injects the authorization token into requests.
- π Protected Routes: Uses a
ProtectedRoute
component to restrict access to authenticated users. - π Login Page Redirection: Redirects users to the login page when accessing protected routes if not authenticated or if the token has expired.
- π Automatic Token Refresh: Axios interceptors attempt to refresh the token before redirecting to login if the access token has expired.
- βοΈ React Hooks & Context API: Manages authentication state efficiently.
- π Python 3.x
- ποΈ PostgreSQL
- π» Node.js & npm/yarn
- Clone the repository:
git clone https://github.com/yourusername/django-react-auth.git cd django-react-auth/backend
- Install dependencies using Poetry:
poetry install
- Configure
.env
file for database settings. - Apply migrations:
poetry run python manage.py migrate
- Create a superuser:
poetry run python manage.py createsuperuser
- Run the development server:
poetry run python manage.py runserver
- Navigate to the frontend folder:
cd ../frontend
- Install dependencies:
npm install # or yarn install
- Start the React development server:
npm start # or yarn start
π Deploying to Choreo
Choreo makes it easy to deploy your Django and React applications with minimal configuration. Follow these steps to deploy your project:
πΉ Backend Deployment
Push your code to a GitHub repository.
Sign in to Choreo.
Create a new project and select Deploy a Backend Service.
Connect your GitHub repository and choose the Django backend.
Configure the environment variables (database settings, secret keys, etc.).
Deploy the backend and monitor logs for any issues.
πΉ Frontend Deployment
Build the React application:
npm run build
Deploy the frontend using Choreo's Web App Hosting.
Link the frontend to the backend API to complete the deployment.
Use a custom domain or Choreo's provided URL to access your app.
- π User logs in and receives an access & refresh token.
- π₯ The access token is stored in memory for requests; the refresh token is stored securely.
- π Axios interceptors automatically append the token to API requests.
- β³ If the access token expires, Axios attempts to refresh it using the refresh token.
- πͺ If the token refresh fails, the user is redirected to the login page.
- π Protected routes ensure only authenticated users can access certain pages.
βββ backend
β βββ manage.py
β βββ pyproject.toml
β βββ poetry.lock
β βββ backend (Django project folder)
β βββ api (Django app folder)
β βββ ...
βββ frontend
β βββ src
β β βββ components
β β βββ pages
β β βββ styles
β β βββ api.js (Axios instance & API calls)
β β βββ context (Auth context)
β β βββ ...
β βββ package.json
β βββ ...
βββ README.md
This project is licensed under the MIT License.