A robust, scalable backend API for authentication and user management built with Node.js and Express. Features secure login/signup, role-based access control, and seamless integration with AWS DynamoDB, Stripe, and Brevo.
β¨ Features
- π Authentication System - Secure user registration and login with JWT tokens
- π₯ Role-Based Access Control - Four-tier role system (User, Agent, Master, Super Admin)
- π Password Security - Bcrypt password hashing for secure password storage
- π DynamoDB Integration - NoSQL database operations with AWS DynamoDB
- π³ Payment Processing - Stripe integration for payment handling
- π§ Email Services - Brevo integration for email communications
- π Serverless Ready - Can be deployed as AWS Lambda function
- π‘οΈ Security Middleware - JWT authentication and authorization middleware
- π Input Validation - Request validation for signup and login endpoints
- ποΈ MVC Architecture - Clean separation of concerns with Models, Views, and Controllers
π οΈ Tech Stack
- Runtime: Node.js
- Framework: Express.js
- Database: AWS DynamoDB
- Authentication: JWT (JSON Web Tokens)
- Password Hashing: bcryptjs
- Payment: Stripe
- Email: Brevo (formerly Sendinblue)
- Deployment: Serverless (AWS Lambda compatible)
π Prerequisites
Before you begin, ensure you have the following installed:
- Node.js (v14 or higher)
- npm or yarn
- AWS Account (for DynamoDB)
- Stripe Account (for payment processing)
- Brevo Account (for email services)
π Installation
- Clone the repository
git clone https://github.com/puffer-git/login-dynamo-db.git
cd login-dynamo-db
- Install dependencies
npm install
- Set up environment variables
Create a .env file in the root directory with the following variables:
# Server Configuration
ENVIRONMENT=development
PORT=4000
# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key-change-this-in-production
JWT_EXPIRES_IN=7d
# AWS DynamoDB Configuration
AWSREGION=us-east-1
AWSENDPOINT=https://dynamodb.us-east-1.amazonaws.com
AWSACCESSKEYID=your-aws-access-key-id
AWSSECRETKEY=your-aws-secret-access-key
# Stripe Configuration (optional)
STRIPE_SECRET_KEY=your-stripe-secret-key
# Brevo Configuration (optional)
BREVO_API_KEY=your-brevo-api-key
- Set up DynamoDB tables
Create a DynamoDB table named users with:
- Partition Key:
id(String) - Enable point-in-time recovery (recommended for production)
π Running the Application
Development Mode
npm run dev
The server will start on http://localhost:4000 with auto-reload enabled.
Production Mode
npm start
Serverless Deployment
When ENVIRONMENT=production, the application exports a serverless handler for AWS Lambda deployment.
π API Documentation
Base URL
- Development:
http://localhost:4000 - Production: Your deployed endpoint
Authentication Endpoints
Register a New User
POST /auth/signup
Content-Type: application/json
{
"player_name": "johndoe",
"email": "john@example.com",
"password": "securePassword123",
"name": "John Doe" // optional
}
Response (201 Created)
{
"success": true,
"message": "User created successfully",
"data": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
}
Error Responses
-
409 Conflict- Player name or email already exists -
400 Bad Request- Validation error -
500 Internal Server Error- Server error
Login
POST /auth/login
Content-Type: application/json
{
"identifier": "johndoe", // Can be email or player_name
"password": "securePassword123"
}
Response (200 OK)
{
"success": true,
"message": "Login successful",
"data": {
"user": {
"role": "user",
"player_name": "johndoe",
"email": "john@example.com",
"name": "John Doe"
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
}
Error Responses
-
401 Unauthorized- Invalid credentials -
400 Bad Request- Validation error -
500 Internal Server Error- Server error
Authentication Header
For protected routes, include the JWT token in the Authorization header:
Authorization: Bearer <your-jwt-token>
ποΈ Project Structure
login-dynamo-db/
βββ app/
β βββ constants/
β β βββ roles.js # Role definitions and hierarchy
β β βββ tables.js # DynamoDB table configurations
β βββ controllers/
β β βββ auth/
β β βββ login/
β β β βββ login.js
β β β βββ loginValidation.js
β β βββ signup/
β β βββ signup.js
β β βββ signupValidation.js
β βββ db/
β β βββ dynamoClient.js # DynamoDB client configuration
β β βββ index.js # Database exports
β βββ middleware/
β β βββ auth.js # Authentication & authorization middleware
β βββ models/
β β βββ BaseModel.js # Base model for DynamoDB operations
β β βββ UserModel.js # User model with business logic
β βββ routes/
β β βββ auth.js # Authentication routes
β β βββ index.js # Route aggregator
β βββ utils/
β β βββ userUtils.js # User utility functions
β βββ index.js # Express app configuration
βββ index.js # Application entry point
βββ package.json
βββ README.md
π Role System
The application supports a four-tier role hierarchy:
- USER - Basic user role (default)
- AGENT - Agent-level permissions
- MASTER - Master-level permissions
- SUPER_ADMIN - Highest level of access
Roles are checked using middleware:
-
authenticate- Verifies JWT token -
authorize(roles)- Checks if user has specific role(s) -
requireMinimumRole(role)- Checks if user has minimum role level
π§ͺ Development
Code Style
- Follow existing code patterns
- Use meaningful variable and function names
- Add JSDoc comments for functions
- Keep functions focused and single-purpose
Adding New Features
- Create feature branch:
git checkout -b feature/your-feature-name - Follow MVC architecture:
- Models in
app/models/ - Controllers in
app/controllers/ - Routes in
app/routes/ - Middleware in
app/middleware/
- Models in
- Add validation for user inputs
- Write clear error messages
- Test your changes thoroughly
- Submit a pull request
π€ Contributing
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Contribution Guidelines
- Write clear, readable code
- Add comments for complex logic
- Follow the existing code structure
- Test your changes before submitting
- Update documentation if needed
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π§ Contact
Developer: Puffer
- π§ Email: devpuffer0807@gmail.com
- π¬ Telegram: @devpuffer0807
π Acknowledgments
- Built with Express.js
- Database powered by AWS DynamoDB
- Payment processing by Stripe
- Email services by Brevo
π License
MIT License - feel free to use this project for your own purposes!
β If you find this project helpful, please consider giving it a star!
Top comments (0)