A Node.js REST API for financial dashboard management with role-based access control (RBAC). Built with Express.js, Prisma ORM, and Supabase PostgreSQL. Features JWT authentication, schema validation, rate limiting, and comprehensive API documentation via Swagger UI.
- JWT Authentication: Secure token-based authentication with password hashing (bcryptjs).
- Role-Based Access Control (RBAC): Three user roles with granular permissions:
- VIEWER: Access dashboard overview.
- ANALYST: View financial records and dashboard insights.
- ADMIN: Full CRUD on records, user management.
- Schema Validation: Input validation using Zod for users and transactions.
- Rate Limiting: Global rate limit of 10 requests per minute per IP to prevent abuse.
- Database: Prisma ORM with Supabase PostgreSQL for data persistence.
- API Documentation: Interactive Swagger UI at
/api-docsfor testing and exploring endpoints. - Middleware: Authentication, role authorization, CORS, and error handling.
- Backend: Node.js, Express.js
- Database: Supabase (PostgreSQL) via Prisma ORM
- Authentication: JWT (jsonwebtoken)
- Password Hashing: bcryptjs
- Validation: Zod
- Rate Limiting: express-rate-limit
- CORS: Enabled
- Documentation: Swagger UI (swagger-ui-express)
- Node.js (v14 or higher)
- npm or yarn
- Supabase account and project
git clone <repository-url>
cd FinTrack_API
npm installCreate a .env file in the root directory by copying the provided .env.example:
cp .env.example .envThen update the values in .env to match your local setup.
Example .env structure:
DATABASE_URL="postgresql://postgres:postgres@db:5432/fintrack"
DIRECT_URL="postgresql://postgres:postgres@db:5432/fintrack"
JWT_SECRET="Secret_Key_For_JWT"
PORT=3000If you are not using a
dbhost alias, replacedbwith the hostname or IP address of your PostgreSQL server.
This project includes a docker-compose.yml that builds the API container and loads environment variables from .env.
- Build and start the container:
docker compose up --build- Visit the API at:
http://localhost:3000
- For API docs, open:
http://localhost:3000/api-docs
- To stop the container, press
Ctrl+Cin the terminal or run:
docker compose down- Create a Supabase project at supabase.com, or use a local PostgreSQL instance.
- Run Prisma migrations to set up the database schema:
npm run prisma:migrate
npm run prisma:generate- (Optional) Seed the database:
npm run prisma:seedIf you want to run the app outside Docker, use one of the commands below:
# Development mode (with nodemon)
npm run dev
# Production mode
npm startThe server runs on http://localhost:3000.
All endpoints return JSON responses with success (boolean) and message (string) fields. Protected routes require a Bearer token in the Authorization header.
-
POST /api/auth/register
- Body:
{ "name": "string", "email": "string", "password": "string" } - Response: User creation confirmation.
- Public endpoint.
- Body:
-
POST /api/auth/login
- Body:
{ "email": "string", "password": "string" } - Response: JWT token and user info.
- Public endpoint.
- Body:
-
GET /api/records (ANALYST, ADMIN)
- Query params:
type(INCOME/EXPENSE),category,from(date),to(date) - Response: List of records (filtered).
- Query params:
-
POST /api/records (ADMIN)
- Body:
{ "amount": number, "description": "string", "category": "string" } - Response: Record creation confirmation.
- Body:
-
PATCH /api/records/:id (ADMIN)
- Body: Partial update fields (amount, description, category).
- Response: Update confirmation.
-
DELETE /api/records/:id (ADMIN)
- Response: Soft delete confirmation.
-
GET /api/users
- Response: List of all users.
-
PATCH /api/users/:id/role
- Body:
{ "role": "VIEWER" | "ANALYST" | "ADMIN" } - Response: Role update confirmation.
- Body:
-
PATCH /api/users/:id/status
- Body:
{ "status": "ACTIVE" | "INACTIVE" } - Response: Status update confirmation.
- Body:
-
GET /api/dashboard/overview (VIEWER, ANALYST, ADMIN)
- Query:
trendType("monthly" | "weekly") - Response: Global financial overview with trends.
- Query:
-
GET /api/dashboard/summary (ANALYST, ADMIN)
- Response: User-specific financial summary.
- GET /
- Response: Basic server status.
- Public endpoint.
| Endpoint | VIEWER | ANALYST | ADMIN |
|---|---|---|---|
| /api/auth/* | ✅ | ✅ | ✅ |
| /api/records (GET) | ❌ | ✅ | ✅ |
| /api/records (POST/PATCH/DELETE) | ❌ | ❌ | ✅ |
| /api/users/* | ❌ | ❌ | ✅ |
| /api/dashboard/overview | ✅ | ✅ | ✅ |
| /api/dashboard/summary | ❌ | ✅ | ✅ |
- Authentication: JWT tokens expire in 1 day. Inactive users are blocked.
- Authorization: Role-based middleware enforces permissions.
- Validation: Zod schemas validate all inputs (users: name, email, password; transactions: amount, type, category, etc.).
- Rate Limiting: 10 requests/minute/IP to mitigate abuse.
- CORS: Enabled for cross-origin requests.
- Error Handling: Centralized error middleware with consistent JSON responses.
Access interactive API docs at http://localhost:3000/api-docs. Includes endpoint details, request/response schemas, and a built-in tester.
- Create an account using
POST /api/auth/register. - Login using
POST /api/auth/loginand copy the returned JWT token. - Open the Swagger UI and click the
Authorizebutton. - Paste the token in the authorization field as
Bearer <token>. - Click
Authorizeand thenClose. - You can now call protected endpoints directly from Swagger.
id(UUID, PK)name(string)email(string, unique)passwordHash(string)role(enum: VIEWER, ANALYST, ADMIN)status(enum: ACTIVE, INACTIVE)createdAt(timestamp)
id(UUID, PK)userId(UUID, FK to users)amount(decimal)description(text)category(string)createdAt(timestamp)
npm start: Start production server.npm run dev: Start development server with auto-reload.npm run prisma:generate: Generate Prisma client.npm run prisma:migrate: Run database migrations.npm run prisma:studio: Open Prisma Studio for DB management.
└── arjung352-fintrack_api/
├── README.md
├── package.json
└── src/
├── index.js
├── swagger.js
├── middleware/
│ ├── authMiddleware.js
│ ├── rateLimitMiddleware.js
│ └── roleMiddleware.js
├── modules/
│ ├── auth/
│ │ ├── authController.js
│ │ └── authRoute.js
│ ├── dashboard/
│ │ ├── dashboardController.js
│ │ └── dashboardRoutes.js
│ ├── record/
│ │ ├── recordController.js
│ │ └── recordRoutes.js
│ └── user/
│ ├── userController.js
│ └── userRoutes.js
├── prisma/
│ ├── prismaClient.js
│ ├── schema.prisma
│ ├── seed.js
│ └── migrations/
│ ├── migration_lock.toml
│ └── 20260405073824_init/
│ └── migration.sql
└── schemaValidation/
├── transactionSchema.js
├── userAuthSchema.js
└── userUpdationValidation.js
- README.md: This file, containing project documentation and setup instructions.
- package.json: Node.js project configuration with dependencies and scripts.
- src/index.js: Main entry point of the application, sets up the Express server.
- src/swagger.js: Configuration for Swagger UI API documentation.
- src/middleware/authMiddleware.js: Middleware for JWT authentication verification.
- src/middleware/rateLimitMiddleware.js: Middleware for implementing rate limiting.
- src/middleware/roleMiddleware.js: Middleware for role-based access control.
- src/modules/auth/authController.js: Controller handling authentication logic (register, login).
- src/modules/auth/authRoute.js: Routes for authentication endpoints.
- src/modules/dashboard/dashboardController.js: Controller for dashboard-related logic.
- src/modules/dashboard/dashboardRoutes.js: Routes for dashboard endpoints.
- src/modules/record/recordController.js: Controller for financial record operations.
- src/modules/record/recordRoutes.js: Routes for record management endpoints.
- src/modules/user/userController.js: Controller for user management operations.
- src/modules/user/userRoutes.js: Routes for user-related endpoints.
- src/prisma/prismaClient.js: Prisma client instance for database interactions.
- src/prisma/schema.prisma: Prisma schema defining the database models.
- src/prisma/seed.js: Script to seed the database with initial data.
- src/prisma/migrations/migration_lock.toml: Prisma migration lock file.
- src/prisma/migrations/20260405073824_init/migration.sql: Initial database migration SQL.
- src/schemaValidation/transactionSchema.js: Zod schema for transaction validation.
- src/schemaValidation/userAuthSchema.js: Zod schema for user validation.
- src/schemaValidation/userUpdationValidation.js: Additional user validation logic.
- Fork the repo.
- Create a feature branch.
- Commit changes.
- Push and open a PR.
ISC