# π Login Backend with Express, AWS Lambda and Dynamo DB
Source: Dev.to
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 an 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
| Component | Technology |
|---|---|
| Runtime | Node.js |
| Framework | Express.js |
| Database | AWS DynamoDB |
| Authentication | JWT (JSON Web Tokens) |
| Password Hashing | bcryptjs |
| Payment | Stripe |
| Brevo (formerly Sendinblue) | |
| Deployment | Serverless (AWS Lambda compatible) |
π Prerequisites
- Node.js (v14 or higher)
- npm or yarn
- AWS Account (for DynamoDB)
- Stripe Account (for payment processing)
- Brevo Account (for email services)
π Installation
1. Clone the repository
git clone https://github.com/puffer-git/login-dynamo-db.git
cd login-dynamo-db
2. Install dependencies
npm install
3. 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
4. Set up DynamoDB tables
Create a DynamoDB table named users with:
- Partition Key:
id(String) - Pointβinβtime recovery: enabled (recommended for production)
π Running the Application
Development Mode
npm run dev
The server starts at http://localhost:4000 with autoβreload enabled.
Production Mode
npm start
Serverless Deployment
When ENVIRONMENT=production, the application exports a serverless handler suitable 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 exists400 Bad Requestβ Validation error500 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 credentials400 Bad Requestβ Validation error500 Internal Server Errorβ Server error
Authentication Header
For protected routes, include the JWT token in the Authorization header:
Authorization: Bearer <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
β βββ ... (other folders/files)
βββ config/
β βββ ... (configuration files)
βββ routes/
β βββ ... (route definitions)
βββ services/
β βββ ... (business logic, e.g., Stripe, Brevo)
βββ tests/
β βββ ... (unit/integration tests)
βββ .env.example
βββ package.json
βββ README.md
Additional structure:
βββ app/
β βββ 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 tokenauthorize(roles)β Checks if the user has specific role(s)requireMinimumRole(role)β Checks if the user meets a 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 a feature branch:
git checkout -b feature/your-feature-name -
Follow the MVC architecture:
- Models β
app/models/ - Controllers β
app/controllers/ - Routes β
app/routes/ - Middleware β
app/middleware/
- Models β
-
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
βοΈ Support
If you find this project helpful, please consider giving it a star!
