A robust, authentication service built with Express.js, providing user authentication through multiple methods with background email processing via AWS SNS/SQS/Lambda integration.
This authentication service is currently powering three production applications and provides:
- Dual Authentication Methods: Traditional email/password and Google OAuth 2.0
- JWT-based Session Management: Secure token-based authentication with RS256 signing
- Background Email Processing: Asynchronous email notifications via AWS SNS β SQS β Lambda pipeline
- Production-Ready Features: Rate limiting, comprehensive logging, CORS handling, and Docker deployment
- Security First: Input validation, password hashing, secure cookie handling, and environment-based configurations
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Client Apps β β Auth Service β β AWS Services β
β β β β β β
β βββββββββββββββ β β βββββββββββββββ β β βββββββββββββββ β
β β App 1 βββΌβββββΌββ Express.js βββΌβββββΌββ SNS Topic β β
β β App 2 β β β β MongoDB β β β β β β β
β β App 3 β β β β Rate Limiterβ β β β SQS Queue β β
β βββββββββββββββ β β β Winston Logsβ β β β β β β
βββββββββββββββββββ β βββββββββββββββ β β β Lambda Fn β β
βββββββββββββββββββ β β (Email) β β
β βββββββββββββββ β
βββββββββββββββββββ
- Email/Password Authentication: Traditional form-based authentication with bcrypt password hashing
- Google OAuth 2.0: Seamless Google sign-in integration
- Personal OAuth Route: Restricted OAuth access for specific users
- JWT Token Management: RS256 algorithm with 12-hour expiration
- Rate Limiting: Configurable request limiting (10 requests per 5 minutes for auth endpoints)
- CORS Protection: Multi-origin support for production applications
- Request Tracking: Unique request IDs for debugging and monitoring
- Input Validation: Comprehensive request validation and sanitization
- Security Headers: Proper cookie settings with httpOnly and secure flags
- SNS Integration: Publishes user events (registration/login) to AWS SNS
- Event-Driven Email: SQS subscribers trigger Lambda functions for email delivery
- Message Filtering: Event-type based message routing
- Error Handling: Robust error handling for external service failures
- Winston Logger: Structured JSON logging with daily file rotation
- Request Logging: Morgan middleware for HTTP request logging
- Separate Log Files: Success and error logs in different files
- Log Retention: Configurable log retention policies (7-14 days)
- Docker Support: Multi-stage Docker builds with security best practices
- Health Check Endpoint: Service health monitoring
- Environment-based Configuration: Secure environment variable management
- CI/CD Pipeline: GitHub Actions for automated testing and linting
- POST
/api/users/signup- Register new user - POST
/api/users/login- User login - POST
/api/users/logout- User logout
- GET
/api/auth/google/callback- Google OAuth callback - GET
/api/auth/google/verify- Verify JWT token
- GET
/health- Service health status
- Node.js 18+ or 20+ or 22+
- MongoDB database
- AWS account with SNS access
- Google OAuth 2.0 credentials
git clone <repository-url>
cd "Auth Service"npm installCreate a .env file based on .env.sample:
# Server Configuration
NODE_ENV=development
PORT=5000
# JWT Configuration
JWT_PRIVATE_KEY=<base64-encoded-private-key>
# Google OAuth
GOOGLE_CLIENT_ID=<your-google-client-id>
GOOGLE_CLIENT_SECRET=<your-google-client-secret>
# Database
MONGO_ATLAS_URI=<mongodb-connection-string>
# AWS SNS
TOPIC_ARN=<aws-sns-topic-arn>
AWS_ACCESS_KEY_ID=<aws-access-key>
AWS_SECRET_ACCESS_KEY=<aws-secret-key>
# CORS Origins
DEPLOYED_URL=<production-frontend-url>
DEPLOYED_BACKEND_URI=<production-backend-url>
Generate RSA key pair for JWT signing:
# Generate private key
openssl genrsa -out private.key 2048
# Generate public key
openssl rsa -in private.key -pubout -out public.key
# Convert private key to base64 for environment variable
base64 -i private.key -o private.key.b64- Create an SNS topic for user events
- Create SQS queues for different event types
- Set up Lambda functions to process email sending
- Configure proper IAM permissions
Ensure your MongoDB database is accessible and the connection string is configured in your environment variables.
npm run devnpm run buildnpm run lintcd Docker
docker-compose -f compose.dev.yaml up --buildcd Docker
docker-compose -f compose.yaml up -d- Multi-stage builds for optimized image size
- Non-root user execution for security
- Health checks for container monitoring
- Secrets management for production deployment
- Alpine Linux for minimal attack surface
- Authentication endpoints: 10 requests per 5 minutes
- Configurable via
createRateLimiterfunction
The service supports multiple client applications through configured CORS origins:
- Local development:
http://localhost:5173,http://localhost:4000 - Production applications: Configured via environment variables
{
httpOnly: true, // Prevent XSS
secure: true, // HTTPS only in production
sameSite: "lax", // CSRF protection
maxAge: 7 * 24 * 60 * 60 * 1000, // 7 days
domain: ".yourdomain.com" // Production domain
}- Success logs:
logs/auth-service/success-YYYY-MM-DD.log - Error logs:
logs/auth-service/error-YYYY-MM-DD.log - Request logs:
logs/requests/access-YYYY-MM-DD.log - Retention: 7 days for auth logs, 14 days for request logs
- Rotation: Daily with 20MB max file size
- User Action: User registers or logs in
- SNS Publishing: Service publishes event to SNS topic with message attributes
- SQS Filtering: SQS queues receive messages based on event type filters
- Lambda Processing: Lambda functions triggered by SQS messages
- Email Delivery: Lambda functions send appropriate welcome/login emails
user_registered: Sent when new user signs upuser_loggedIn: Sent when user logs in (configurable per application)
The project includes automated CI/CD pipeline that:
- Tests on Node.js versions 18.x, 20.x, 22.x
- Runs ESLint for code quality
- Executes on every push to
mainbranch
# Test health endpoint
curl http://localhost:5000/health
# Test signup
curl -X POST http://localhost:5000/api/users/signup \
-H "Content-Type: application/json" \
-d '{"name":"John Doe","email":"john@example.com","password":"password123","username":"johndoe","business":"Test App"}'
# Test login
curl -X POST http://localhost:5000/api/users/login \
-H "Content-Type: application/json" \
-d '{"email":"john@example.com","password":"password123","business":"Test App"}'This authentication service is currently serving three production applications:
- Resource Manager: Internal resource management system
- KS Application: Knowledge sharing platform
- Daily Activity Tracker: Personal productivity application
- Rolling Updates: Zero-downtime deployment strategy
- Monitoring: Comprehensive logging and health monitoring
- Security: Secrets management and environment isolation
- JWT RS256: Asymmetric key signing for token security
- Bcrypt Hashing: Secure password storage with salt rounds
- Rate Limiting: Protection against brute force attacks
- CORS Configuration: Restricted origin access
- Input Validation: Comprehensive request validation
- Security Headers: Proper HTTP security headers
- Environment Variables: Secure configuration management
- Docker Security: Non-root user execution
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow ESLint configuration
- Add appropriate error handling
- Update documentation for new features
- Test endpoints thoroughly
- Follow existing code patterns and structure
This project is licensed under the ISC License.
For support, please create an issue in the repository or contact the maintainer.
- v1.0.0: Initial production release
- Basic authentication with email/password
- Google OAuth integration
- SNS email processing
- Docker deployment support
- Comprehensive logging and monitoring
Note: This authentication service is actively maintained and deployed in production environments. For production deployment, ensure all environment variables are properly configured and security best practices are followed.