A comprehensive full-stack application featuring secure user authentication and API key management system. Built with Next.js frontend and Node.js/Express backend with SQLite database. Includes password hashing, JWT-like tokens, database storage, token revocation, and complete API key lifecycle management with quota tracking.
- Node.js (14.0 or later) - Download here
First time setup:
setup-nodejs.batStart the server:
start-nodejs-backend.batOr manually:
cd nodejs-backend
npm install
npm start✅ Backend runs on http://localhost:8080
cd userportal
npm install
npm run dev✅ Frontend runs on http://localhost:3000
- Frontend UI: http://localhost:3000
- Backend API: http://localhost:8080/api/health
- API Documentation: http://localhost:8080/api/docs
✅ WORKING - Node.js backend fully operational
✅ RUNNING - Backend service successfully started
✅ DATABASE - SQLite schema initialized
✅ QUOTA SYSTEM - Monthly limits and tracking active
✅ FRONTEND - Next.js UI with full functionality
✅ SCRIPTS - Windows batch scripts for easy setup
✅ TESTED - All endpoints verified and working
├── nodejs-backend/ # Node.js/Express authentication & API service
│ ├── server.js # 🌐 Main Express server & routing
│ ├── config.js # ⚙️ Configuration management
│ ├── database.js # 🗄️ SQLite database operations & schema
│ ├── auth.js # 🔐 Authentication utilities
│ ├── apikeys.js # 🔑 API key management & validation
│ ├── quota.js # 📊 Usage quota tracking & limits
│ ├── middleware.js # 🛡️ Authentication middleware
│ ├── routes/ # 📡 API route handlers
│ │ ├── auth.routes.js # Authentication endpoints
│ │ ├── apikeys.routes.js # API key management endpoints
│ │ └── public.routes.js # Public API endpoints
│ ├── database/ # 💾 SQLite database files
│ │ └── userportal.db # Main database (auto-created)
│ ├── package.json # 📦 Node.js dependencies
│ ├── .env # 🔐 Environment variables
│ └── README.md # 📖 Backend-specific documentation
├── userportal/ # Next.js frontend application
│ ├── app/ # 🎨 Next.js app router pages
│ ├── components/ # ⚛️ React UI components
│ ├── hooks/ # 🪝 Custom React hooks
│ ├── lib/ # 📚 Utility libraries & helpers
│ ├── public/ # 🌍 Static assets
│ └── package.json # 📦 Frontend dependencies
├── start-nodejs-backend.bat # 🪟 Windows startup script
├── setup-nodejs.bat # 🛠️ Node.js dependencies setup
├── setup-sqlite.bat # 🛠️ SQLite database setup
├── view-database.bat # 🔍 Database viewer script
└── README.md # 📖 Main project documentation
This project includes a complete authentication and API key management system with comprehensive quota management and usage tracking.
Node.js/Express Backend
- Located in
nodejs-backend/directory - Uses Express.js framework
- JavaScript-based with modern async/await patterns
- RESTful API design
- Start with:
npm startorstart-nodejs-backend.bat
- User Registration & Login System - Complete user account management with validation
- JWT-like Token Authentication - Secure token-based authentication with database tracking
- Password Security - bcrypt hashing for secure password protection
- SQLite Database Integration - Automatic database creation and management with proper indexing
- API Key Management System - Create, manage, and validate API keys (max 3 per user)
- Dynamic Rule Management - Update content policy rules for API keys after creation
- Quota Management System - Monthly usage limits (100 requests/month per key) with automatic reset
- Usage Tracking - Real-time tracking of API key usage with detailed analytics
- Token Revocation - Secure logout with immediate token invalidation
- Modular Architecture - Clean, organized Node.js/Express codebase with separated concerns:
server.js- Main Express server and routingauth.js- Authentication utilities and token managementdatabase.js- SQLite database operationsapikeys.js- API key management functionsquota.js- Quota tracking and reset logicmiddleware.js- Authentication middlewareroutes/- Organized API route handlers
- Database Tables Auto-Creation - Three tables created automatically on first run:
users- User account information with security featuresjwt_tokens- Token tracking and revocation for securityapi_keys- API key management with usage tracking and quota management
- Express.js Framework - Industry-standard web framework for Node.js
- RESTful API Design - Clean, predictable API endpoints
- Environment Configuration - Flexible configuration via .env file
- Secure Password Storage - Never store plain text passwords (bcrypt hashing)
- Token Signing & Validation - Cryptographic token security with database verification
- Database Token Tracking - All tokens tracked for security auditing and revocation
- API Key Security - Keys hashed in database, never stored in plain text
- API Key Limits - Maximum 3 API keys per user to prevent abuse
- Quota Enforcement - Monthly usage limits prevent API abuse
- Input Validation - Email format and password strength validation
- CORS Protection - Configured for frontend integration
- Secure Key Generation - Cryptographically secure API key generation
- React Authentication UI - Login, register, and profile components
- API Key Management UI - Complete interface for creating and managing API keys
- Quota Monitoring Dashboard - Real-time usage tracking and quota warnings
- Auth Context Provider - Global authentication state management
- TypeScript Support - Full type safety throughout
- Tailwind CSS Styling - Modern, responsive design with professional UI
- Token Expiry Handling - Automatic cleanup and re-authentication
- Real-time Updates - Live quota updates and usage statistics
- Startup Scripts - Easy service startup for Windows
- Database Viewer Script - Quick database inspection tools
- API Testing Scripts - Automated testing for API key functionality
- Comprehensive Documentation - Complete setup and usage guides
The application implements a secure JWT-like authentication system with database token tracking:
-
User Registration:
- User provides username, email, and password via
/api/auth/register - Password is hashed using bcrypt for security
- User data stored in SQLite
userstable with UUID as primary key - Returns success message with user info (password excluded for security)
- User provides username, email, and password via
-
User Login:
- User provides username and password via
/api/auth/login - Backend verifies username exists in database
- Password is compared against stored bcrypt hash
- If valid, a JWT-like token is generated containing user ID, username, email, and expiry time
- Token is signed with secret key (HMAC-SHA256)
- Token hash is stored in
jwt_tokenstable for tracking and revocation - Returns token to client along with expiry information
- User provides username and password via
-
Authenticated Requests:
- Client includes token in
Authorization: Bearer <token>header - Backend validates token signature and checks expiry time
- Backend verifies token exists in database and is not revoked
- If valid, request proceeds; otherwise returns 401 Unauthorized
- Client includes token in
-
Logout:
- Client sends logout request with token via
/api/auth/logout - Backend marks token as revoked in
jwt_tokenstable - Client discards token
- Token immediately becomes invalid for all future requests
- Security: Revoked tokens cannot be reused even if stolen
- Client sends logout request with token via
The application includes a complete API key lifecycle management system with usage quotas:
-
API Key Creation:
- User must be authenticated (JWT token required)
- User can create up to 3 API keys via
/api/apikeysendpoint - Each key has a name, optional description, and access rules
- System generates a secure API key with prefix
ak_(e.g.,ak_abc123...) - API key is hashed before storing in database (original key shown only once)
- Each key starts with 100 requests/month quota
- Quota resets automatically on the 1st of each month
- Returns the plain API key (shown only once - must be saved by user)
-
Using API Keys:
- Public API endpoints require API key authentication
- Client includes key in
X-API-Keyheader orAuthorization: ApiKey <key>header - Backend validates API key hash against database
- Backend checks if key status is 'active'
- Backend checks if monthly quota is not exceeded
- If valid and quota available, request proceeds
- Usage counter is incremented for tracking
-
Managing API Keys:
- List all keys:
GET /api/apikeys(shows masked keys, never full keys) - Update status:
PUT /api/apikeys/:id/status(activate/deactivate keys) - Update rules:
PUT /api/apikeys/:id/rules(modify access permissions) - Delete key:
DELETE /api/apikeys/:id(revokes key - cannot be undone) - Check quota:
GET /api/apikeys/:id/quota(view usage and remaining quota)
- List all keys:
-
Quota Management:
- Each API key has 100 requests/month
- Usage is tracked in
current_month_usagecolumn - Quota resets automatically on 1st of each month
- When quota exceeded, API returns HTTP 429 (Too Many Requests)
- Users can check remaining quota via dashboard or API endpoint
The SQLite database includes three main tables:
CREATE TABLE users (
id TEXT PRIMARY KEY,
username TEXT UNIQUE NOT NULL,
email TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
is_active BOOLEAN DEFAULT 1
);CREATE TABLE jwt_tokens (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
token_hash TEXT NOT NULL,
expires_at DATETIME NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
is_revoked BOOLEAN DEFAULT 0,
FOREIGN KEY (user_id) REFERENCES users(id)
);CREATE TABLE api_keys (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
name TEXT NOT NULL,
key_hash TEXT NOT NULL,
description TEXT,
rules TEXT,
status TEXT DEFAULT 'active',
usage_count INTEGER DEFAULT 0,
monthly_quota INTEGER DEFAULT 100,
current_month_usage INTEGER DEFAULT 0,
quota_reset_date TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id)
);GET /api/health- Server health status
POST /api/auth/register- Register new userPOST /api/auth/login- Login userGET /api/auth/profile- Get user profile (requires JWT)POST /api/auth/logout- Logout user (requires JWT)
POST /api/apikeys- Create new API keyGET /api/apikeys- List all user's API keysPUT /api/apikeys/:keyId/status- Update API key statusPUT /api/apikeys/:keyId/rules- Update API key rulesDELETE /api/apikeys/:keyId- Delete (revoke) API keyPOST /api/apikeys/validate- Validate API keyGET /api/apikeys/:keyId/quota- Get quota status
GET /api/users- Get all usersGET /api/users/:userId- Get user by IDGET /api/projects- Get all projectsPOST /api/projects- Create new projectGET /api/analytics/summary- Get analytics dataPOST /api/moderate-content/text/v1- Content moderationGET /api/docs- API documentation (no auth required)
Edit nodejs-backend/.env to configure:
# Server Configuration
PORT=8080
NODE_ENV=development
# Database Configuration
DB_PATH=./database/userportal.db
# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key-change-in-production
JWT_EXPIRY_TIME=3600
JWT_ISSUER=userportal-auth
JWT_AUDIENCE=userportal-users
# CORS Configuration
CORS_ORIGINS=http://localhost:3000,http://localhost:3001cd nodejs-backend
npm run devview-database.batOr manually:
cd nodejs-backend\database
sqlite3 userportal.db
.tables
SELECT * FROM users;Use the provided PowerShell scripts or test with curl:
# Test health endpoint
curl http://localhost:8080/api/health
# Register a new user
curl -X POST http://localhost:8080/api/auth/register \
-H "Content-Type: application/json" \
-d '{"username":"testuser","email":"test@example.com","password":"password123"}'
# Login
curl -X POST http://localhost:8080/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"testuser","password":"password123"}'If port 8080 is in use, change it in .env:
PORT=3001Delete and recreate:
cd nodejs-backend
Remove-Item -Recurse -Force database
npm startReinstall dependencies:
cd nodejs-backend
Remove-Item -Recurse -Force node_modules
npm install- ✅ Change JWT_SECRET in production
- ✅ Use HTTPS in production
- ✅ Never commit .env files
- ✅ Implement rate limiting for production
- ✅ Regular security audits of dependencies
- ✅ Monitor API usage for abuse
- ✅ Backup database regularly
ISC
Built with ❤️ using Node.js, Express, and Next.js