📌 FastAPI + JWT Authentication 🚀 Overview This project is a FastAPI backend with JWT authentication, PostgreSQL database, and Docker Compose integration. It supports:
User registration and login with secure password hashing.
JWT token generation and authentication.
Form submission and retrieval endpoints.
Interactive API documentation via Swagger UI.
⚙️ Tech Stack FastAPI – Web framework
Uvicorn – ASGI server
SQLAlchemy – ORM
PostgreSQL – Database
Docker Compose – Container orchestration
Passlib – Password hashing
Python-Jose – JWT handling
Pydantic v2 – Data validation
📂 Project Structure Code assignment_Task/ │── app/ │ ├── main.py # FastAPI app & routes │ ├── auth.py # JWT token creation │ ├── crud.py # Database operations │ ├── models.py # SQLAlchemy models │ ├── schemas.py # Pydantic schemas │ ├── utils.py # Password hashing │ ├── config.py # Environment variables │ └── database.py # DB engine & session │── requirements.txt # Python dependencies │── Dockerfile # Backend image build │── docker-compose.yml # Services definition │── .env # Environment variables 🔑 Environment Variables (.env) Code DATABASE_URL=postgresql://postgres:Jeetu@3010@db:5432/assignment_task SECRET_KEY=69676c404a0cc8a8a4f68c1f5eca95dc558b45461fd9385003511fc164532631 ALGORITHM=HS256 ACCESS_TOKEN_EXPIRE_MINUTES=30 🛠 Local Setup (without Docker) Create venv:
powershell python -m venv venv .\venv\Scripts\activate Install dependencies:
powershell pip install -r requirements.txt Run server:
powershell uvicorn app.main:app --reload Access:
Root: http://127.0.0.1:8000
Docs: http://127.0.0.1:8000/docs
🐳 Docker Setup Start services:
powershell docker compose up --build Services:
Backend → http://localhost:8000
Postgres → localhost:5432 (postgres / Jeetu@3010 / assignment_task)
Stop services:
powershell
docker compose down
🔐 API Endpoints
Root
GET /
Response:
json
{"message": "API is live"}
Register
POST /register
Request:
json { "username": "alice", "password": "secret123" } Response:
json
{
"id": 1,
"username": "alice",
"hashed_password": "$2b$12$..."
}
Login
POST /login
Request:
json { "username": "alice", "password": "secret123" } Response:
json
{
"access_token": "jwt_token_here",
"token_type": "bearer"
}
Submit Form
POST /submit-form
Request:
json { "name": "Palak", "phone": "9876543210", "remarks": "Testing form" } Response:
json
{
"id": 1,
"name": "Palak",
"phone": "9876543210",
"remarks": "Testing form"
}
Get Form
GET /get-form/{form_id}
Response:
json { "id": 1, "name": "Palak", "phone": "9876543210", "remarks": "Testing form" }