Skip to content

This repository demonstrates the integration of a FastAPI backend with a React frontend, featuring JWT-based authentication. It includes user registration, login, and profile management functionalities. The project is structured with clear separation of concerns and follows best practices for security and configuration management.

Notifications You must be signed in to change notification settings

CynthiaWahome/fastapi-react-integration

Repository files navigation

FastAPI and React Integration

This project demonstrates integration between a FastAPI backend and React frontend with JWT authentication. It features user registration, login, and profile management functionality.

Project Structure

fastapi-react-integration/
├── backend/
│   ├── auth/
│   │   └── auth_handler.py  # JWT authentication logic
│   ├── routers/
│   │   ├── auth.py          # Authentication routes
│   │   └── user.py          # User profile routes
│   ├── database.py          # Database connection
│   ├── models.py            # SQLAlchemy models
│   └── schemas.py           # Pydantic schemas
├── frontend/
│   ├── src/
│   │   ├── components/      # React components
│   │   │   ├── Login.jsx
│   │   │   ├── Register.jsx
│   │   │   └── UserProfile.jsx
│   │   ├── contexts/
│   │   │   └── AuthContext.jsx  # Authentication state management
│   │   ├── api.js           # API integration functions
│   │   └── App.jsx          # Main application component
│   └── package.json
├── main.py                  # FastAPI application entry point
└── .env                     # Environment variables

API Endpoints

Endpoint Method Description Request Body Response
/ GET Health check None {"message": "API is working!"}
/auth/register POST Register new user {"username": "string", "email": "string", "password": "string"} User object
/auth/token POST Login and get token Form data with username/password {"access_token": "string", "token_type": "bearer"}
/users/me/ GET Get current user None (requires auth token) User object

Setup Instructions

Backend Setup

  1. Create a virtual environment and activate it:

    python -m venv venv
    # Windows
    venv\Scripts\activate
    # macOS/Linux
    source venv/bin/activate
  2. Install backend dependencies:

    pip install -r backend/requirements.txt
  3. Copy the example file to create your .env file:

# Windows
copy example.env .env

# macOS/Linux
cp example.env .env
  1. Run the FastAPI server:
    uvicorn main:app --reload

Frontend Setup

  1. Navigate to the frontend directory:

    cd frontend
  2. Install dependencies:

    npm install
  3. Ensure React Router is properly installed

    npm install react-router-dom
  4. Start the development server:

    npm run dev
  5. Access the application at http://localhost:5173

Features

  • User Authentication: JWT-based authentication system
  • User Registration: Create new user accounts
  • User Profile: View and manage user information
  • Protected Routes: Restrict access to authenticated users

Technologies Used

  • Backend:

    • FastAPI: Modern, fast web framework for building APIs
    • SQLAlchemy: SQL toolkit and ORM
    • PyJWT: JSON Web Token implementation
    • Python-decouple: Environment variable management
  • Frontend:

    • React: JavaScript library for building user interfaces
    • Axios: Promise-based HTTP client
    • React Router: Navigation and routing

Screenshots

Register User Login Registered User User Profile FastAPI docs

Development

To access the API documentation locally, visit: http://localhost:8000/docs

About

This repository demonstrates the integration of a FastAPI backend with a React frontend, featuring JWT-based authentication. It includes user registration, login, and profile management functionalities. The project is structured with clear separation of concerns and follows best practices for security and configuration management.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published