Skip to content

Epic-Codebase/django_react_auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

πŸš€ django-react-auth

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.


✨ Features

πŸ”Ή Backend (Django + DRF)

  • πŸ” 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.

πŸ”Ή Frontend (React + Axios)

  • πŸ”‘ 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.

πŸš€ Getting Started

πŸ“Œ Prerequisites

  • 🐍 Python 3.x
  • πŸ—„οΈ PostgreSQL
  • πŸ’» Node.js & npm/yarn

πŸ› οΈ Backend Setup

  1. Clone the repository:
    git clone https://github.com/yourusername/django-react-auth.git
    cd django-react-auth/backend
  2. Install dependencies using Poetry:
    poetry install
  3. Configure .env file for database settings.
  4. Apply migrations:
    poetry run python manage.py migrate
  5. Create a superuser:
    poetry run python manage.py createsuperuser
  6. Run the development server:
    poetry run python manage.py runserver

🎨 Frontend Setup

  1. Navigate to the frontend folder:
    cd ../frontend
  2. Install dependencies:
    npm install  # or yarn install
  3. 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.


πŸ”„ API Authentication Flow

  1. πŸ”‘ User logs in and receives an access & refresh token.
  2. πŸ“₯ The access token is stored in memory for requests; the refresh token is stored securely.
  3. πŸ”„ Axios interceptors automatically append the token to API requests.
  4. ⏳ If the access token expires, Axios attempts to refresh it using the refresh token.
  5. πŸšͺ If the token refresh fails, the user is redirected to the login page.
  6. πŸ”’ Protected routes ensure only authenticated users can access certain pages.

πŸ“ Folder Structure

β”œβ”€β”€ 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

πŸ“œ License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published