Skip to content
100rabheimerPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

πŸš€ Task API

Secure β€’ Containerized β€’ Documented

A modern RESTful Task Management API built with Node.js, Express.js, PostgreSQL, Docker, Supabase Auth, and JWT.


Node.js Express PostgreSQL Docker Supabase



JWT Swagger OpenAPI Docker Compose

✨ Project at a Glance

Task API evolved from a basic CRUD exercise into a persistent, containerized, authenticated backend.

Stage Evolution
A1 In-memory REST CRUD API
A2 SQLite persistence
A3 PostgreSQL + Docker Compose
A4 Supabase Auth + JWT-protected routes

Current capabilities

  • πŸ” Supabase email/password authentication
  • 🎟️ JWT bearer authorization
  • πŸ›‘οΈ Reusable requireAuth middleware
  • πŸ“¦ Protected task CRUD operations
  • 🐘 PostgreSQL persistence
  • 🐳 Docker + Docker Compose
  • πŸ’Ύ Persistent Docker volume
  • 🧷 Parameterized SQL queries
  • ❀️ Real database health check
  • πŸ“– Swagger UI with Authorize support
  • βš™οΈ .env-based configuration

🧰 Tech Stack


Node.js

Express

PostgreSQL

Docker

Supabase

JWT

Swagger

OpenAPI

Postman

Git

πŸ—οΈ Architecture

Client / Swagger UI
        β”‚
        β”œβ”€β”€ signup / login ──────► Supabase Auth
        β”‚                              β”‚
        β”‚                         JWT access token
        β”‚                              β”‚
        β—„β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚
        β”‚ Authorization: Bearer <JWT>
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Node.js + Express API  β”‚
β”‚      Port 3000         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
            β”‚
      requireAuth
            β”‚
            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Protected Task Routes  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
            β”‚ parameterized SQL
            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ PostgreSQL Container   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
            β”‚
            β–Ό
     Docker Volume
       taskdata

πŸ” Authentication

The API uses Supabase Auth for user authentication. After login, Supabase returns a JWT access token.

Protected requests use:

Authorization: Bearer <ACCESS_TOKEN>

Without a valid token, protected routes return 401 Unauthorized.

Authentication flow

Email + Password
      ↓
POST /auth/login
      ↓
Supabase Auth
      ↓
JWT access_token
      ↓
Bearer Authorization
      ↓
requireAuth middleware
      ↓
Protected API

Security: Never commit .env, passwords, Supabase secrets, or real JWT access tokens.


πŸ“‘ API Endpoints

Method Endpoint Access Purpose
POST /auth/signup 🌐 Public Create account
POST /auth/login 🌐 Public Login and receive JWT
POST /auth/logout πŸ”’ Protected Logout
GET / 🌐 Public API information
GET /health 🌐 Public API + DB health
GET /tasks πŸ”’ Protected Get all tasks
GET /tasks/:id πŸ”’ Protected Get task by ID
POST /tasks πŸ”’ Protected Create task
PUT /tasks/:id πŸ”’ Protected Update task
DELETE /tasks/:id πŸ”’ Protected Delete task

πŸš€ Quick Start

1. Clone

git clone https://github.com/100rabheimer/task-api.git
cd task-api

2. Configure .env

Copy .env.example to .env and add your own values:

DATABASE_URL=postgres://postgres:your_password@db:5432/tasks
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_KEY=your_supabase_anon_or_publishable_key
PORT=3000

3. Start with Docker

docker compose up --build

Then open:

Resource Local URL
API http://localhost:3000
Health http://localhost:3000/health
Swagger UI http://localhost:3000/docs

Stop the stack with:

docker compose down

πŸ—„οΈ PostgreSQL

Schema:

CREATE TABLE IF NOT EXISTS tasks (
    id SERIAL PRIMARY KEY,
    title TEXT NOT NULL,
    done BOOLEAN NOT NULL DEFAULT FALSE
);

Example task:

{
  "id": 1,
  "title": "Learn Express",
  "done": false
}

The app seeds initial records only when the table is empty.


πŸ§ͺ Login & Protected Route Example

Login

POST /auth/login
Content-Type: application/json

{
  "email": "user@example.com",
  "password": "StrongPassword123!"
}

Then use the returned access_token:

GET /tasks
Authorization: Bearer <ACCESS_TOKEN>

PowerShell

$login = Invoke-RestMethod `
  -Uri "http://localhost:3000/auth/login" `
  -Method Post `
  -ContentType "application/json" `
  -Body '{"email":"user@example.com","password":"StrongPassword123!"}'

$token = $login.access_token

Invoke-RestMethod `
  -Uri "http://localhost:3000/tasks" `
  -Method Get `
  -Headers @{ Authorization = "Bearer $token" }

πŸ“– Swagger UI

Open:

http://localhost:3000/docs

For protected endpoints:

  1. Run POST /auth/login.
  2. Copy the returned access_token.
  3. Click Authorize πŸ”’.
  4. Paste the JWT.
  5. Run any protected /tasks endpoint.

Swagger sends the bearer token automatically after authorization.

Crop or hide JWT values before adding Swagger screenshots to a public repository.


❀️ Health Check

GET /health executes:

SELECT 1;

Expected response:

{
  "status": "ok",
  "database": "connected"
}

This verifies both the API and PostgreSQL connection.


πŸ“ Project Structure

task-api/
β”‚
β”œβ”€β”€ assets/
β”‚   └── postgres-database.png
β”‚
β”œβ”€β”€ authMiddleware.js
β”œβ”€β”€ supabaseClient.js
β”œβ”€β”€ db.js
β”œβ”€β”€ index.js
β”œβ”€β”€ openapi.json
β”‚
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ compose.yaml
β”œβ”€β”€ .dockerignore
β”œβ”€β”€ .env.example
β”œβ”€β”€ .gitignore
β”‚
β”œβ”€β”€ package.json
β”œβ”€β”€ package-lock.json
└── README.md

πŸ“Έ Database Preview

Inspect PostgreSQL:

docker compose exec db psql -U postgres -d tasks

Inside psql:

\dt
SELECT * FROM tasks;

PostgreSQL Database Preview


πŸ§ͺ Verified

  • βœ… Signup and login
  • βœ… JWT access token returned
  • βœ… Protected routes reject missing authorization
  • βœ… Valid JWT grants task access
  • βœ… Swagger Bearer authorization works
  • βœ… PostgreSQL CRUD works
  • βœ… Parameterized queries
  • βœ… Database health check
  • βœ… Docker Compose startup
  • βœ… Persistent database volume
  • βœ… .env kept outside Git

🧠 What I Learned

Backend: REST design, Express middleware, CRUD, validation, HTTP status codes.

Database: PostgreSQL, node-postgres, parameterized queries, initialization, persistence.

Security: Supabase Auth, JWTs, bearer authorization, protected routes, environment secrets.

DevOps: Docker, Docker Compose, container networking, volumes, reproducible local setup.

Documentation: OpenAPI 3 and Swagger UI with authenticated endpoint testing.


πŸ”’ Security Notes

  • .env is Git-ignored.
  • .env.example contains placeholders only.
  • Password authentication is handled by Supabase.
  • No custom password hashing is implemented in this API.
  • SQL values use parameterized queries.
  • Protected endpoints require bearer authentication.
  • Real JWTs and credentials must not appear in public screenshots.

πŸ‘¨β€πŸ’» Author

Saurabh Pandey

Backend Development β€’ REST APIs β€’ Node.js β€’ PostgreSQL β€’ Docker

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages