This project is a fullstack web application using Python and JavaScript technologies. It leverages Django for the backend and React for the frontend. The application implements JWT authentication for secure user management and includes deployment instructions.
- Backend
- Frontend
- End Goal
- Deployment Notes
- Django Project Setup
- Frontend Setup
- JWT Authentication Flow
- Application Screenshots
The backend is built with Django and handles API requests and data storage.
- Django: Framework for building the backend.
- django-cors-headers: Middleware for handling cross-origin requests.
- djangorestframework-simplejwt: Package for JWT authentication.
- psycopg2-binary: PostgreSQL adapter for Django.
- python-dotenv: For managing environment variables.
The frontend is built with React and provides user-facing components to interact with the backend API (e.g., sign-in, sign-out, registration).
- React: JavaScript library for building user interfaces.
- Axios: Library for making HTTP requests.
- axios-interceptor: Intercepts requests to automatically add authentication headers.
The ultimate goal is to deploy both the backend and frontend and ensure they are properly connected.
- Ensure
django-cors-headers
is configured to resolve cross-origin request issues. - Utilize
djangorestframework-simplejwt
for managing JWT authentication. - Use
psycopg2-binary
for PostgreSQL database interaction. - Manage environment variables using
python-dotenv
. - JWT Authentication:
- JWT tokens are used to manage user authentication. The frontend will request tokens from the backend, which are then used to access protected endpoints.
- Two types of tokens are issued: access token (for making requests) and refresh token (for obtaining a new access token).
- Django ORM and Serializers:
- ORM (Object-Relational Mapping): Maps Python objects to database tables, simplifying database operations.
- Serializers: Convert Python objects to JSON format and vice versa, facilitating communication between the API and frontend.
- Create a Django App:
python manage.py startapp api
- This command creates a new directory
api
inside thebackend
directory for custom views and code.
- This command creates a new directory
- Axios:
- Use Axios for making HTTP requests to the backend.
- Implement
axios-interceptor
to automatically add JWT tokens to request headers.
-
Setup Virtual Environment and Install Dependencies: Navigate to the backend folder.
cd backend/ python -m virtualenv venv source venv/bin/activate pip install -r requirements.txt
-
Environment Setup:
- Setup the backend using choreo. Create a PostgreSQL database, get the environment variables, and include them in the
.env
file inside thebackend/backend
directory.
# Example .env file DB_HOST=your-db-host DB_PORT=your-db-port DB_USER=your-db-user DB_NAME=your-db-name DB_PWD=your-db-password
- Setup the backend using choreo. Create a PostgreSQL database, get the environment variables, and include them in the
-
Migrations:
- Create migration files:
python manage.py makemigrations
- Apply migrations to the database:
python manage.py migrate
- Run server:
python manage.py runserver
- Create migration files:
-
Install Dependencies and Start Development Server:
cd frontend/ npm install npm run dev
-
Configure Environment Variables:
Create a
.env
file in thefrontend
directory and set theVITE_API_URL
variable:VITE_API_URL="http://127.0.0.1:8000"
This environment variable allows to dynamically set the base URL for all the API requests. By using
import.meta.env.VITE_API_URL
in the code, API URL can easily be loaded or changed based on the environment (development, production, etc.) application is running in. This simplifies the configuration and ensures consistency across your application.
-
Frontend Authentication:
- The frontend sends user credentials to the backend.
- The backend responds with access and refresh tokens if credentials are valid.
-
Token Storage:
- Store tokens securely on the frontend to manage authentication.
-
Token Expiry:
- Access Token: Valid for a short period (e.g., 30 minutes). Use the refresh token to obtain a new access token when it expires.
- Refresh Token: Valid for a longer period (e.g., 1 day). Requires re-authentication upon expiry.
By following these instructions, you will have a complete setup for a Django and React application with JWT authentication and deployment capabilities.