A robust backend application for a banking system, built with Node.js, Express, and MongoDB. This system handles secure user authentication, account creation, and money transfers with transactional integrity.
- User Authentication: Secure Register and Login with JWT and Cookies.
- Account Management: valid user can create an account and check balance.
- Secure Transactions: Money transfers between accounts using MongoDB Transactions (ACID properties) to ensure data consistency.
- Idempotency: Prevents duplicate transactions using
idempotencyKey. - Email Notifications: Integration with Gmail to send alerts for registration and transactions.
- System Funds: endpoint to inject initial funds into accounts.
- Runtime: Node.js
- Framework: Express.js
- Database: MongoDB (Mongoose)
- Authentication: JSON Web Tokens (JWT)
The system ensures that money is safely transferred by using a database session. If any step fails (e.g., insufficient funds, database error), the entire operation is rolled back.
sequenceDiagram
participant User
participant API
participant DB as MongoDB
participant Email as Email Service
User->>API: POST /api/transactions
API->>DB: Check Sender Balance & Account Status
alt Insufficient Balance / Invalid
API-->>User: Error (400/404)
else Valid verification
API->>DB: Start Session & Transaction
API->>DB: Create Ledger (Debit Sender)
API->>DB: Create Ledger (Credit Receiver)
API->>DB: Update Transaction Status -> Completed
API->>DB: Commit Transaction
API->>Email: Send Transaction Alert
API-->>User: Success (200)
end
Base URL: /api/auth
| Method | Endpoint | Description | Body Parameters |
|---|---|---|---|
| POST | /register |
Register a new user | name, email, password |
| POST | /login |
Login user & get cookie | email, password |
Base URL: /api/accounts (Protected Routes)
| Method | Endpoint | Description | Body Parameters |
|---|---|---|---|
| POST | / |
Create a new bank account | None (Uses logged-in user) |
| GET | / |
Get my account details | None |
| GET | /balance/:accountid |
Get balance by Account ID | None |
Base URL: /api/transactions (Protected Routes)
| Method | Endpoint | Description | Body Parameters |
|---|---|---|---|
| POST | / |
Transfer money between accounts | fromAccount, toAccount, amount, idempotencyKey |
| POST | /system/initial-funds |
Inject initial funds (System) | toAccount (or userId), amount, idempotencyKey |
Register User
{
"name": "Jane Doe",
"email": "jane@example.com",
"password": "securepassword123"
}Transfer Money
{
"fromAccount": "65d4...",
"toAccount": "65d5...",
"amount": 500,
"idempotencyKey": "unique-key-123"
}-
Clone the repository:
git clone <your-repo-url> cd BankTransactions
-
Install dependencies:
npm install
-
Setup Environment Variables: Create a
.envfile in the root directory and add:PORT=3000 MONGO_URI=your_mongodb_connection_string JWT_SECRET=your_jwt_secret GMAIL_USER=your_email@gmail.com GMAIL_PASS=your_app_password
-
Start the server:
npm run dev # or npm start