5.8 KiB
5.8 KiB
Work Allocation Backend - Deno TypeScript
A secure, type-safe backend for the Work Allocation System built with Deno and TypeScript.
Features
Security Improvements
- Strong Password Hashing: bcrypt with configurable rounds (default: 12)
- JWT Authentication: Secure token-based authentication with HMAC-SHA256
- Rate Limiting: Configurable request rate limiting to prevent abuse
- CORS Protection: Configurable cross-origin resource sharing
- Security Headers: X-Frame-Options, X-Content-Type-Options, XSS protection
- Input Sanitization: Protection against XSS and injection attacks
- Strict TypeScript: Full type safety with strict compiler options
Technical Stack
- Runtime: Deno 2.x
- Framework: Oak (Deno's Express-like framework)
- Database: MySQL 8.0 with mysql2 driver
- Authentication: JWT with djwt library
- Password Hashing: bcrypt
Prerequisites
- Deno 2.0 or higher
- MySQL 8.0 (via Docker or local installation)
Installation
-
Install Deno (if not already installed):
curl -fsSL https://deno.land/install.sh | sh -
Configure environment:
cp .env.example .env # Edit .env with your database credentials -
Start the database (if using Docker):
cd .. && docker-compose up -d
Running the Server
Development Mode (with auto-reload)
deno task dev
Production Mode
deno task start
Seed the Database
deno task seed
API Endpoints
Authentication
POST /api/auth/login- User loginGET /api/auth/me- Get current userPOST /api/auth/change-password- Change password
Users
GET /api/users- List users (filtered by role)GET /api/users/:id- Get user by IDPOST /api/users- Create user (Admin/Supervisor)PUT /api/users/:id- Update user (Admin/Supervisor)DELETE /api/users/:id- Delete user (Admin/Supervisor)
Departments
GET /api/departments- List departmentsGET /api/departments/:id- Get departmentGET /api/departments/:id/sub-departments- Get sub-departmentsPOST /api/departments- Create department (SuperAdmin)POST /api/departments/:id/sub-departments- Create sub-department (SuperAdmin)
Work Allocations
GET /api/work-allocations- List allocations (role-filtered)GET /api/work-allocations/:id- Get allocationPOST /api/work-allocations- Create allocation (Supervisor/Admin)PUT /api/work-allocations/:id/status- Update status (Supervisor/Admin)DELETE /api/work-allocations/:id- Delete allocation (Supervisor/Admin)
Attendance
GET /api/attendance- List attendance recordsGET /api/attendance/:id- Get attendance recordPOST /api/attendance/check-in- Check in employee (Supervisor/Admin)POST /api/attendance/check-out- Check out employee (Supervisor/Admin)GET /api/attendance/summary/stats- Get attendance summary
Contractor Rates
GET /api/contractor-rates- List ratesGET /api/contractor-rates/contractor/:id/current- Get current ratePOST /api/contractor-rates- Set rate (Supervisor/Admin)PUT /api/contractor-rates/:id- Update rate (Supervisor/Admin)DELETE /api/contractor-rates/:id- Delete rate (Supervisor/Admin)
Health Check
GET /health- Server health status
Environment Variables
| Variable | Description | Default |
|---|---|---|
PORT |
Server port | 3000 |
DB_HOST |
Database host | localhost |
DB_USER |
Database user | root |
DB_PASSWORD |
Database password | admin123 |
DB_NAME |
Database name | work_allocation |
DB_PORT |
Database port | 3306 |
JWT_SECRET |
JWT signing secret | (change in production!) |
JWT_EXPIRES_IN |
Token expiration | 7d |
BCRYPT_ROUNDS |
Password hash rounds | 12 |
RATE_LIMIT_WINDOW_MS |
Rate limit window | 900000 (15 min) |
RATE_LIMIT_MAX_REQUESTS |
Max requests per window | 100 |
CORS_ORIGIN |
Allowed CORS origins | http://localhost:5173 |
NODE_ENV |
Environment | development |
Security Best Practices
For Production
-
Change JWT Secret: Use a strong, random secret
JWT_SECRET=$(openssl rand -base64 64) -
Enable HTTPS: Use a reverse proxy (nginx) with SSL
-
Set Production Environment:
NODE_ENV=production -
Increase bcrypt rounds (if performance allows):
BCRYPT_ROUNDS=14 -
Configure CORS for your domain:
CORS_ORIGIN=https://yourdomain.com
Project Structure
backend-deno/
├── config/
│ ├── database.ts # Database connection pool
│ └── env.ts # Environment configuration
├── middleware/
│ ├── auth.ts # JWT authentication & authorization
│ └── security.ts # Security middleware (CORS, rate limit, etc.)
├── routes/
│ ├── auth.ts # Authentication routes
│ ├── users.ts # User management routes
│ ├── departments.ts # Department routes
│ ├── work-allocations.ts
│ ├── attendance.ts
│ └── contractor-rates.ts
├── scripts/
│ └── seed.ts # Database seeding script
├── types/
│ └── index.ts # TypeScript type definitions
├── main.ts # Application entry point
├── deno.json # Deno configuration
└── .env # Environment variables
Differences from Node.js Backend
| Feature | Node.js | Deno |
|---|---|---|
| Runtime | Node.js | Deno |
| Package Manager | npm | Built-in (JSR/npm) |
| TypeScript | Requires compilation | Native support |
| Security | Manual setup | Secure by default |
| Permissions | Full access | Explicit permissions |
| Framework | Express | Oak |
License
MIT