A robust and highly secure RESTful API built with Node.js, Express, and MongoDB that simulates a core banking engine. This backend handles user authentication, account management, and peer-to-peer (P2P) financial transactions while ensuring strict data integrity and protection against race conditions.
- Secure Authentication & Session Management: JWT-based authentication with a highly secure logout mechanism using a database-backed Token Blacklist (optimized with MongoDB TTL indexes).
- ACID-Compliant Transactions: Utilizes MongoDB Sessions and Transactions to ensure all peer-to-peer transfers and ledger entries succeed or fail atomically.
- Idempotency Protection: Enforces idempotency keys on all transaction routes to prevent duplicate charges and protect against network retries or double-clicks.
- Race Condition Prevention: Bulletproof concurrency handling prevents users from double-spending or overdrawing accounts during simultaneous requests.
- Double-Entry Ledger: Maintains an immutable ledger tracking every credit and debit for accurate balance derivation and auditing.
- Automated Email Notifications: Integrates an email service to instantly notify users of successful deposits and withdrawals.
- System User Funding: Dedicated system-level endpoints to inject initial funds into the banking ecosystem.
- Runtime: Node.js
- Framework: Express.js
- Database: MongoDB & Mongoose
- Authentication: JSON Web Tokens (JWT) & bcrypt
- Notifications: Nodemailer (Email Service)
- Node.js installed on your local machine
- A MongoDB database (Local or MongoDB Atlas)
-
Clone the repository:
git clone https://github.com/RuDr8A/Banking-System-Backend.git cd Banking-System-Backend
-
Install the dependencies:
npm install
-
Create a .env file in the root directory and configure your environment variables:
PORT=3000 MONGODB_URI=your_mongodb_connection_string JWT_SECRET=your_super_secret_jwt_key EMAIL_USER=your_email_address EMAIL_PASS=your_email_app_password
-
Start the server:
npm run dev
- POST /register - Register a new user (triggers email verification).
- POST /login - Authenticate a user and return a JWT.
- POST /logout - Blacklist the current JWT to securely end the session.
- POST /create - Create a new banking account (Checking/Savings) for the logged-in user.
- GET /get-accounts - Retrieve all accounts owned by the logged-in user.
- GET /balance/:accountId - Fetch the real-time calculated balance of a specific account.
- POST /system/initial-fund - (Admin/System Only) Deposit initial funds into a user's account.
- POST /make-transaction - Execute a secure P2P transfer between two accounts. Requires an idempotencyKey.
This project addresses several advanced backend engineering challenges:
- The "Double-Spend" Problem: Solved by utilizing MongoDB's startTransaction() to lock account documents during balance verification and ledger insertion.
- Network Retries: Implemented Idempotency Keys so that if a client drops connection and retries a transfer, the server safely returns the original receipt instead of deducting funds twice.
- Stateless JWT Revocation: Standard JWTs cannot be destroyed before expiration. This API introduces a tokenBlacklist collection with a Time-To-Live (TTL) index that securely intercepts and rejects blacklisted tokens, dropping them from the database automatically once they naturally expire.
This project is open-source and available under the MIT License.