A robust FastAPI backend for a task management application with user authentication and CRUD operations for tasks. Built with SQLAlchemy ORM, PostgreSQL database, and JWT-based authentication via HTTP-only cookies.
-
π User Authentication
- User registration (
POST /auth/) - Secure login with JWT tokens (
POST /auth/token) - Logout functionality (
POST /auth/logout)
- User registration (
-
π Task Management
- Create tasks (
POST /taskmanager/) - Get specific task (
GET /taskmanager/{task_id}) - List all user tasks (
GET /taskmanager/tasks) - Update tasks (
PUT/PATCH /taskmanager/{task_id}) - Delete tasks (
DELETE /taskmanager/{task_id})
- Create tasks (
-
π‘οΈ Security
- bcrypt password hashing
- JWT tokens in HTTP-only cookies
- CORS protection
- Input validation with Pydantic
Taskmanager/
βββ pyproject.toml # Project dependencies and metadata
βββ uv.lock # Dependency lock file
βββ README.md
βββ src/
βββ taskmanager/
βββ main.py # FastAPI application entry point
βββ controller/ # API route handlers
β βββ auth_controller.py
β βββ task_controller.py
βββ service/ # Business logic layer
β βββ auth_service.py
β βββ task_service.py
βββ model/ # SQLAlchemy models
β βββ user.py
β βββ task.py
βββ database/ # Database configuration
β βββ db.py
βββ core/ # Core configuration
βββ config.py
βββ deps.py
Create a .env file in the root directory:
# Server Configuration
HOST=localhost
PORT=3510
# Application Settings
APP_NAME=Task Manager API
APP_VERSION=1.0.0
ENVIRONMENT=development
DEBUG=True
# Frontend Configuration
FRONTEND_URL=http://localhost:5173
# Database Configuration
DATABASE_URL=postgresql+psycopg2://postgres:password@localhost:5433/postgres
# Authentication
AUTH_SECRET_KEY=your_secret_key_here
AUTH_ALGORITHM=HS256| Method | Endpoint | Description |
|---|---|---|
POST |
/auth/ |
Register new user |
POST |
/auth/token |
Login (returns JWT in cookie) |
POST |
/auth/logout |
Logout (clears JWT cookie) |
| Method | Endpoint | Description |
|---|---|---|
GET |
/taskmanager/tasks |
Get all user tasks |
GET |
/taskmanager/{task_id} |
Get specific task |
POST |
/taskmanager/ |
Create new task |
PUT |
/taskmanager/{task_id} |
Update task (complete) |
PATCH |
/taskmanager/{task_id} |
Update task (partial) |
DELETE |
/taskmanager/{task_id} |
Delete task |
- Python 3.11+
- PostgreSQL database
uvpackage manager
-
Clone the repository
git clone <your-repo-url> cd Taskmanager
-
Install dependencies
uv sync
-
Set up environment
# Copy and edit the environment file cp .env.example .env # Edit .env with your database credentials
-
Run the application
uv run python -m src.taskmanager.main
# Using uvicorn directly
uvicorn src.taskmanager.main:app --reload --host localhost --port 3510
# Using Python module
python -m src.taskmanager.main- API Base: http://localhost:3510
- Interactive Docs: http://localhost:3510/docs
- Alternative Docs: http://localhost:3510/redoc
- Health Check: http://localhost:3510/health
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR UNIQUE NOT NULL,
hashed_password VARCHAR NOT NULL,
created_date TIMESTAMP DEFAULT NOW()
);CREATE TABLE tasks (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id),
title VARCHAR NOT NULL,
description TEXT,
category VARCHAR NOT NULL,
priority VARCHAR NOT NULL,
is_completed BOOLEAN DEFAULT FALSE,
due_date TIMESTAMP,
created_date TIMESTAMP DEFAULT NOW()
);curl -X POST "http://localhost:3510/auth/" \
-H "Content-Type: application/json" \
-d '{"email": "user@example.com", "password": "password123"}'curl -X POST "http://localhost:3510/auth/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=user@example.com&password=password123"curl -X POST "http://localhost:3510/taskmanager/" \
-H "Content-Type: application/json" \
-H "Cookie: jwt=your_jwt_token" \
-d '{
"title": "Complete project",
"description": "Finish the task management API",
"category": "work",
"priority": "high",
"is_completed": false,
"due_date": "2024-01-20T18:00:00"
}'- Password Security: bcrypt hashing with salt
- JWT Authentication: Configurable token expiration
- Cookie Security: HTTP-only, secure cookies
- CORS Protection: Configurable origin restrictions
- Input Validation: Pydantic model validation
- Modular Design: Clear separation of concerns
- Type Safety: Full type hints throughout
- Dependency Injection: FastAPI's dependency system
- Clean Architecture: Service layer pattern
- Models: Add to
src/taskmanager/model/ - Services: Add business logic to
src/taskmanager/service/ - Controllers: Add API endpoints to
src/taskmanager/controller/ - Dependencies: Update
src/taskmanager/core/deps.py
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Task Management Backend β Built with β€οΈ using FastAPI + PostgreSQL