A scalable backend system for handling financial transactions, ledger entries, and account management with JWT-based authentication and MongoDB transactional integrity.
The system uses MongoDB transactions to maintain financial consistency and a TTL-based blacklist collection for automatically expiring revoked JWT tokens
Backend_Ledger/
│
├── server.js ← Entry point
├── package.json
├── .env ← Environment variables (not committed)
│
└── src/
├── app.js ← Express app setup, route mounting
│
├── config/
│ └── db.js ← MongoDB connection
│
├── controllers/
│ ├── auth.controller.js
│ ├── account.controller.js
│ └── transaction.controller.js
│
├── middleware/
│ └── auth.middleware.js ← JWT verification (user + system)
│
├── models/
│ ├── user.model.js
│ ├── account.model.js
│ ├── transaction.model.js
│ └── ledger.model.js
└── blacklist.model.js ← Revoked JWTs with TTL expiration
│
├── routes/
│ ├── auth.routes.js
│ ├── account.routes.js
│ └── transaction.routes.js
│
└── services/
└── email.service.js ← Nodemailer email service
| Method | Endpoint | Access | Description |
|---|---|---|---|
POST |
/api/auth/register |
Public | Register a new user |
POST |
/api/auth/login |
Public | Login and receive JWT cookie |
| Method | Endpoint | Access | Description |
|---|---|---|---|
POST |
/api/accounts/ |
🔒 Protected | Create a new account |
GET |
/api/accounts/all-accounts |
🔒 Protected | Get all accounts for logged-in user |
GET |
/api/accounts/balance/:accountId |
🔒 Protected | Get balance of a specific account |
| Method | Endpoint | Access | Description |
|---|---|---|---|
POST |
/api/transactions/ |
🔒 Protected | Create a new transaction |
POST |
/api/transactions/system/inital-funds |
🔒 System Only | Seed initial funds (system middleware) |
🔒 Protected = requires valid JWT token in cookie
🔒 System Only = requires system-level JWT (authSystemUserMiddleware)
Client Request
│
▼
authMiddleware (auth.middleware.js)
│
├── Reads JWT from cookie
├── Verifies token
└── Attaches user to req → Controller
Two middleware levels:
authMiddleware— for regular authenticated usersauthSystemUserMiddleware— for internal system operations (e.g. seeding funds)
git clone https://github.com/amitava-code/Backend_Ledger.git
cd Backend_Ledgernpm installPORT=3000
MONGO_URI=mongodb://localhost:27017/ledger_db
JWT_SECRET=your_jwt_secret_here
JWT_SYSTEM_SECRET=your_system_jwt_secret_here
EMAIL_USER=your_email@gmail.com
EMAIL_PASS=your_email_password# Development (with nodemon)
npm run dev
# Production
npm start# Register
curl -X POST http://localhost:3000/api/auth/register \
-H "Content-Type: application/json" \
-d '{"name":"John","email":"john@example.com","password":"123456"}'
# Login
curl -X POST http://localhost:3000/api/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"john@example.com","password":"123456"}'
# Create Account (with cookie from login)
curl -X POST http://localhost:3000/api/accounts/ \
-H "Content-Type: application/json" \
--cookie "token=<your_jwt_here>"| Model | Description |
|---|---|
user.model.js |
User credentials and profile |
account.model.js |
Financial account per user |
transaction.model.js |
Debit/credit transaction records |
ledger.model.js |
Double-entry ledger entries |
Amitava Biswas — Backend Developer
This is a personal learning project. Contributions and suggestions are welcome — feel free to open an issue or PR.