Skip to content

FastAPI backend for task management with authentication and CRUD operations

Notifications You must be signed in to change notification settings

bagalshreerang/task-management-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“‹ Task Management Backend (FastAPI)

🧭 Overview

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.


πŸš€ Features

  • πŸ” User Authentication

    • User registration (POST /auth/)
    • Secure login with JWT tokens (POST /auth/token)
    • Logout functionality (POST /auth/logout)
  • πŸ“‹ 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})
  • πŸ›‘οΈ Security

    • bcrypt password hashing
    • JWT tokens in HTTP-only cookies
    • CORS protection
    • Input validation with Pydantic

πŸ—‚οΈ Project Structure

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

βš™οΈ Environment Variables

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

πŸ§ͺ API Endpoints

πŸ” Authentication

Method Endpoint Description
POST /auth/ Register new user
POST /auth/token Login (returns JWT in cookie)
POST /auth/logout Logout (clears JWT cookie)

πŸ“‹ Task Management

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

πŸƒ Quick Start

Prerequisites

  • Python 3.11+
  • PostgreSQL database
  • uv package manager

Installation

  1. Clone the repository

    git clone <your-repo-url>
    cd Taskmanager
  2. Install dependencies

    uv sync
  3. Set up environment

    # Copy and edit the environment file
    cp .env.example .env
    # Edit .env with your database credentials
  4. Run the application

    uv run python -m src.taskmanager.main

Alternative Run Commands

# Using uvicorn directly
uvicorn src.taskmanager.main:app --reload --host localhost --port 3510

# Using Python module
python -m src.taskmanager.main

🌐 Access Points


πŸ“Š Database Schema

Users Table

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    email VARCHAR UNIQUE NOT NULL,
    hashed_password VARCHAR NOT NULL,
    created_date TIMESTAMP DEFAULT NOW()
);

Tasks Table

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()
);

πŸ§ͺ Testing Examples

Create User

curl -X POST "http://localhost:3510/auth/" \
  -H "Content-Type: application/json" \
  -d '{"email": "user@example.com", "password": "password123"}'

Login

curl -X POST "http://localhost:3510/auth/token" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "username=user@example.com&password=password123"

Create Task

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"
  }'

πŸ›‘οΈ Security Features

  • 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

πŸ”§ Development

Project Structure Benefits

  • Modular Design: Clear separation of concerns
  • Type Safety: Full type hints throughout
  • Dependency Injection: FastAPI's dependency system
  • Clean Architecture: Service layer pattern

Adding New Features

  1. Models: Add to src/taskmanager/model/
  2. Services: Add business logic to src/taskmanager/service/
  3. Controllers: Add API endpoints to src/taskmanager/controller/
  4. Dependencies: Update src/taskmanager/core/deps.py

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


Task Management Backend β€” Built with ❀️ using FastAPI + PostgreSQL

About

FastAPI backend for task management with authentication and CRUD operations

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages