A full-featured task management REST API built with Next.js 16, Prisma ORM, and PostgreSQL. This API provides user authentication and CRUD operations for managing tasks with JWT-based authorization.
- User Authentication: Register and login with secure password hashing (bcrypt)
- JWT Authorization: Secure API endpoints with JSON Web Tokens
- Task Management: Create, read, update, and delete tasks
- Task Status Tracking: Track tasks with three statuses:
PENDING,IN_PROGRESS,COMPLETED - User Isolation: Users can only access and manage their own tasks
- Database: PostgreSQL with Prisma ORM
- Framework: Next.js 16 (App Router)
- Database: PostgreSQL
- ORM: Prisma
- Authentication: JWT (jsonwebtoken)
- Password Hashing: bcryptjs
- Language: TypeScript
- Node.js 18+ or Bun
- PostgreSQL database
- Clone the repository:
git clone https://github.com/cypherab01/task-manager-api
cd task-manager-api- Install dependencies:
npm install
# or
bun install- Set up environment variables:
Create a .env file in the root directory:
DATABASE_URL="postgresql://username:password@localhost:5432/task_manager"
JWT_SECRET="your-secret-key-here"- Run database migrations:
npx prisma migrate deploy
# or generate the Prisma client
npx prisma generate- Start the development server:
npm run dev
# or
bun devThe API will be available at http://localhost:3000
POST /api/auth/register
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com",
"password": "securepassword"
}Response (201):
{
"user": {
"id": "clxxx...",
"email": "john@example.com"
}
}POST /api/auth/login
Content-Type: application/json
{
"email": "john@example.com",
"password": "securepassword"
}Response (200):
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": "clxxx...",
"email": "john@example.com"
}
}All task endpoints require authentication. Include the JWT token in the Authorization header:
Authorization: Bearer <your-token>
GET /api/tasks
Authorization: Bearer <token>Response (200):
[
{
"id": "clxxx...",
"title": "Complete project",
"description": "Finish the task manager API",
"status": "IN_PROGRESS",
"userId": "clxxx...",
"createdAt": "2024-11-02T10:00:00.000Z",
"updatedAt": "2024-11-02T11:00:00.000Z"
}
]POST /api/tasks
Authorization: Bearer <token>
Content-Type: application/json
{
"title": "New Task",
"description": "Task description"
}Response (201):
{
"message": "Task created successfully",
"task": {
"title": "New Task",
"description": "Task description",
"status": "PENDING"
}
}PATCH /api/tasks/[id]
Authorization: Bearer <token>
Content-Type: application/json
{
"status": "COMPLETED"
}Valid Status Values:
PENDINGIN_PROGRESSCOMPLETED
Response (200):
{
"id": "clxxx...",
"title": "Task title",
"description": "Task description",
"status": "COMPLETED",
"userId": "clxxx...",
"createdAt": "2024-11-02T10:00:00.000Z",
"updatedAt": "2024-11-02T12:00:00.000Z"
}DELETE /api/tasks/[id]
Authorization: Bearer <token>Response (200):
{
"message": "Task deleted successfully"
}model User {
id String @id @default(cuid())
name String
email String @unique
password String
tasks Task[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}model Task {
id String @id @default(cuid())
title String
description String?
status TaskStatus @default(PENDING)
user User @relation(fields: [userId], references: [id])
userId String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
enum TaskStatus {
PENDING
IN_PROGRESS
COMPLETED
}The API uses standard HTTP status codes:
200- Success201- Created400- Bad Request (invalid input)401- Unauthorized (missing or invalid token)404- Not Found500- Internal Server Error
Error Response Format:
{
"error": "Error message description"
}npm run dev- Start development servernpm run build- Build for productionnpm run start- Start production servernpm run lint- Run ESLint
npx prisma migrate dev- Create and apply migrationsnpx prisma generate- Generate Prisma Clientnpx prisma studio- Open Prisma Studio (database GUI)
- Push your code to GitHub
- Import your repository on Vercel
- Add environment variables in Vercel dashboard:
DATABASE_URLJWT_SECRET
- Deploy!
Make sure to set these environment variables in your production environment:
DATABASE_URL- Your PostgreSQL connection stringJWT_SECRET- A strong, random secret key for JWT signing
- Passwords are hashed using bcrypt with a salt factor of 10
- JWT tokens expire after 7 days
- All task operations verify user ownership
- SQL injection protection through Prisma ORM
MIT
Contributions are welcome! Please feel free to submit a Pull Request.