|
1 | | -# @bitriel/backend |
| 1 | +# Bitriel Backend API |
2 | 2 |
|
3 | | -Simple Express API with JWT auth helpers. |
| 3 | +Node.js + Express + TypeScript backend API for Bitriel digital wallet application with Koompi OAuth integration. |
4 | 4 |
|
5 | | -## Getting started |
| 5 | +## Features |
6 | 6 |
|
7 | | -1. Copy `.env.example` to `.env` and set a strong `JWT_SECRET`. |
8 | | -2. Install dependencies from the repo root: `pnpm install`. |
9 | | -3. Start the API: `pnpm --filter @bitriel/backend dev`. |
| 7 | +- 🔐 Koompi OAuth 2.0 + PKCE authentication |
| 8 | +- 🎫 JWT token-based authorization |
| 9 | +- 👤 User profile management |
| 10 | +- 🗄️ MongoDB database integration |
| 11 | +- 📱 Separate endpoints for web and mobile platforms |
| 12 | +- 🛡️ TypeScript for type safety |
| 13 | +- 🏗️ Clean architecture with service layer |
10 | 14 |
|
11 | | -## Routes |
| 15 | +## Prerequisites |
12 | 16 |
|
13 | | -- `GET /health` – readiness probe. |
14 | | -- `POST /auth/token` – provide `{ "userId": "<id>", "roles": ["admin"] }` to receive a signed JWT. |
15 | | -- `GET /me` – attach `Authorization: Bearer <token>` to inspect the decoded claims. |
| 17 | +- Node.js 18+ and pnpm 9+ |
| 18 | +- MongoDB 5+ (local or cloud) |
| 19 | +- Koompi OAuth credentials (client ID & secret) |
16 | 20 |
|
17 | | -`JWT_EXPIRES_IN` (default `15m`) controls token lifetime. |
| 21 | +## Quick Start |
| 22 | + |
| 23 | +### 1. Install Dependencies |
| 24 | + |
| 25 | +```bash |
| 26 | +cd apps/backend |
| 27 | +pnpm install |
| 28 | +``` |
| 29 | + |
| 30 | +### 2. Environment Configuration |
| 31 | + |
| 32 | +Copy `.env.example` to `.env`: |
| 33 | + |
| 34 | +```bash |
| 35 | +cp .env.example .env |
| 36 | +``` |
| 37 | + |
| 38 | +Edit `.env` with your configuration: |
| 39 | + |
| 40 | +```env |
| 41 | +# Server |
| 42 | +PORT=4000 |
| 43 | +
|
| 44 | +# JWT |
| 45 | +JWT_SECRET=your-super-secret-jwt-key-change-this |
| 46 | +JWT_EXPIRES_IN=7d |
| 47 | +
|
| 48 | +# MongoDB |
| 49 | +MONGODB_URI=mongodb://localhost:27017/bitriel |
| 50 | +
|
| 51 | +# Koompi OAuth |
| 52 | +KOOMPI_CLIENT_ID=your-koompi-client-id |
| 53 | +KOOMPI_CLIENT_SECRET=your-koompi-client-secret |
| 54 | +KOOMPI_REDIRECT_URI=http://localhost:4000/api/oauth/callback |
| 55 | +KOOMPI_MOBILE_REDIRECT_URI=http://localhost:4000/api/oauth/callback?platform=mobile |
| 56 | +
|
| 57 | +# Frontend |
| 58 | +FRONTEND_URL=http://localhost:3000 |
| 59 | +FRONTEND_CALLBACK_PATH=/oauth/callback |
| 60 | +``` |
| 61 | + |
| 62 | +### 3. Start MongoDB |
| 63 | + |
| 64 | +```bash |
| 65 | +# Using Docker |
| 66 | +docker run -d -p 27017:27017 --name mongodb mongo:latest |
| 67 | + |
| 68 | +# Or use MongoDB Atlas (cloud) |
| 69 | +# Update MONGODB_URI in .env with your Atlas connection string |
| 70 | +``` |
| 71 | + |
| 72 | +### 4. Run Development Server |
| 73 | + |
| 74 | +```bash |
| 75 | +pnpm dev |
| 76 | +``` |
| 77 | + |
| 78 | +Server will start at `http://localhost:4000` |
| 79 | + |
| 80 | +## API Endpoints |
| 81 | + |
| 82 | +### Authentication |
| 83 | + |
| 84 | +| Endpoint | Method | Description | |
| 85 | +|----------|--------|-------------| |
| 86 | +| `/api/oauth/login` | GET | Initiates OAuth flow (query: `?platform=mobile\|web`) | |
| 87 | +| `/api/oauth/callback` | GET | OAuth callback (handles both web and mobile via `?platform=`) | |
| 88 | +| `/api/auth/me` | GET | Get authenticated user profile (requires JWT) | |
| 89 | + |
| 90 | +### Testing Endpoints |
| 91 | + |
| 92 | +```bash |
| 93 | +# Test OAuth login (redirects to Koompi) |
| 94 | +curl http://localhost:4000/api/oauth/login?platform=mobile |
| 95 | + |
| 96 | +# Test user profile (requires valid JWT) |
| 97 | +curl http://localhost:4000/api/auth/me \ |
| 98 | + -H "Authorization: Bearer YOUR_JWT_TOKEN" |
| 99 | +``` |
| 100 | + |
| 101 | +## Project Structure |
| 102 | + |
| 103 | +``` |
| 104 | +apps/backend/src/ |
| 105 | +├── controllers/ # HTTP request handlers |
| 106 | +│ ├── oauthController.ts |
| 107 | +│ └── userController.ts |
| 108 | +├── services/ # Business logic layer |
| 109 | +│ ├── oauthService.ts |
| 110 | +│ └── userService.ts |
| 111 | +├── models/ # MongoDB schemas |
| 112 | +│ └── User.ts |
| 113 | +├── middleware/ # Express middleware |
| 114 | +│ └── auth.ts |
| 115 | +├── routes/ # API route definitions |
| 116 | +│ ├── oauthRoutes.ts |
| 117 | +│ └── userRoutes.ts |
| 118 | +├── types/ # TypeScript types |
| 119 | +│ └── oauth.ts |
| 120 | +├── utils/ # Utility functions |
| 121 | +│ └── jwt.ts |
| 122 | +├── config.ts # Environment config |
| 123 | +└── index.ts # App entry point |
| 124 | +``` |
| 125 | + |
| 126 | +## Development |
| 127 | + |
| 128 | +### Available Scripts |
| 129 | + |
| 130 | +```bash |
| 131 | +pnpm dev # Start development server with hot reload |
| 132 | +pnpm build # Build for production |
| 133 | +pnpm start # Start production server |
| 134 | +pnpm lint # Run ESLint |
| 135 | +``` |
| 136 | + |
| 137 | +## Documentation |
| 138 | + |
| 139 | +- **Full OAuth Integration Guide:** See [KOOMPI_OAUTH_INTEGRATION.md](../../docs/KOOMPI_OAUTH_INTEGRATION.md) |
| 140 | +- **API Documentation:** See [API Endpoints](#api-endpoints) above |
| 141 | + |
| 142 | +## Troubleshooting |
| 143 | + |
| 144 | +### Common Issues |
| 145 | + |
| 146 | +1. **MongoDB Connection Failed** |
| 147 | + - Check MongoDB is running: `docker ps` |
| 148 | + - Verify `MONGODB_URI` in `.env` |
| 149 | + |
| 150 | +2. **OAuth Redirect Mismatch** |
| 151 | + - Verify redirect URIs in Koompi OAuth dashboard |
| 152 | + - Ensure exact match with `.env` values |
| 153 | + |
| 154 | +3. **Invalid JWT Token** |
| 155 | + - Check `JWT_SECRET` is set |
| 156 | + - Verify token format: `Bearer <token>` |
| 157 | + |
| 158 | +## Security |
| 159 | + |
| 160 | +- ✅ Never commit `.env` files |
| 161 | +- ✅ Use strong JWT secrets (32+ characters) |
| 162 | +- ✅ Enable HTTPS in production |
| 163 | +- ✅ Validate all user inputs |
| 164 | + |
| 165 | +## Support |
| 166 | + |
| 167 | +For detailed integration guide and troubleshooting: |
| 168 | +- See [docs/KOOMPI_OAUTH_INTEGRATION.md](../../docs/KOOMPI_OAUTH_INTEGRATION.md) |
| 169 | + |
| 170 | +--- |
| 171 | + |
| 172 | +**Last Updated:** 2025-01-21 |
0 commit comments