A serverless Todo application backend built with AWS Lambda, Serverless Framework, and DynamoDB. This application supports user registration, login with email verification, JWT authentication, and CRUD operations for managing todos.
- User Registration: Users can register using their email, which receives a verification code for authentication.
- Email Verification: Sends a six-digit verification code via EmailJS.
- JWT Authentication: After email verification, users receive a JWT for authorization in further requests.
- Todo Management: Users can create, view, complete, and delete todo items. Each todo has a name, description, priority, due date, and completion status.
- Serverless: Fully serverless application using AWS Lambda, DynamoDB, and Serverless Framework.
- AWS Lambda - For serverless functions
- AWS DynamoDB - Database to store user, login code, and todo information
- EmailJS - To send verification codes to user email addresses
- JWT - For secure user authentication and authorization
- Serverless Framework - To deploy and manage AWS resources
- TypeScript - For type safety and maintainability
.
├── src
│ ├── handlers # Lambda function handlers
│ │ ├── createTodo.ts
│ │ ├── deleteTodo.ts
│ │ ├── getAllTodos.ts
│ │ ├── completeTodo.ts
│ │ ├── registerUser.ts
│ │ ├── userLogin.ts
│ │ └── verifyEmail.ts
│ ├── services # Business logic and helper functions
│ │ ├── authMiddleware.ts
│ │ ├── authService.ts
│ │ ├── codeService.ts
│ │ └── smtpService.ts
│ └── utils # Utility functions and constants
├── serverless.yml # Serverless configuration file
└── package.json # Node.js dependencies and scripts
- Node.js (v14 or higher)
- Serverless Framework installed globally:
npm install -g serverless
- AWS CLI configured with sufficient permissions to deploy Lambda functions and manage DynamoDB tables.
Create a .env
file in the root directory with the following environment variables:
JWT_SECRET=your_jwt_secret
EMAILJS_PUBLIC_KEY=your_emailjs_public_key
EMAILJS_PRIVATE_KEY=your_emailjs_private_key
EMAILJS_SERVICE_ID=your_emailjs_service_id
EMAILJS_TEMPLATE_ID=your_emailjs_template_id
-
Install dependencies:
npm install
-
Compile TypeScript:
npm run build
-
Deploy the application:
serverless deploy
This will deploy the Lambda functions, configure API Gateway routes, and create necessary DynamoDB tables (
Users
,Todos
, andLoginCodes
).
Function | Path | Method | Description |
---|---|---|---|
Register User | /register-user |
POST | Registers a new user with email |
Login User | /login |
POST | Initiates login by sending a code to email |
Verify Code | /verify-email |
POST | Verifies the login code and returns a JWT |
Get Todos | /todos |
GET | Retrieves all todos for an authenticated user |
Create Todo | /todos |
POST | Creates a new todo |
Complete Todo | /todos/complete |
POST | Marks a todo as complete |
Delete Todo | /todos |
DELETE | Deletes a specified todo |
- User Registration: Users initiate login by providing their email. A verification code is sent via EmailJS.
- Email Verification: The user submits the verification code. If valid, a JWT is issued.
- JWT Authorization: The JWT is used to authorize requests for creating, viewing, completing, and deleting todos.
This file configures the Serverless Framework, defining the provider, environment variables, IAM permissions, and the Lambda functions.
- IAM Role Permissions:
- DynamoDB: Permissions to read and write to the
Users
,Todos
, andLoginCodes
tables. - SES: Permission to send emails using Amazon SES (optional, since EmailJS is used here).
- DynamoDB: Permissions to read and write to the
Each file in the handlers
folder is a Lambda function handler for specific API endpoints. For example:
- createTodo.ts: Handles the creation of a new todo item.
- userLogin.ts: Sends a login code to the user's email or validates an entered code.
- verifyEmail.ts: Verifies the email code and issues a JWT if successful.
A middleware function to authenticate incoming requests by decoding the JWT and extracting the user’s email. This middleware is used to secure todo-related endpoints.
A service to send emails using EmailJS. It is used to send the six-digit login code to users' email addresses.
{
"email": "user@example.com"
}
- Request to send code:
{ "email": "user@example.com" }
- Request to verify code:
{ "email": "user@example.com", "sixDigitCode": "123456" }
Headers: Authorization: Bearer <JWT_TOKEN>
{
"name": "Sample Task",
"description": "This is a sample task",
"priority": 3,
"dueDate": "2024-12-31"
}
Headers: Authorization: Bearer <JWT_TOKEN>
{
"todoId": "unique-todo-id"
}
Headers: Authorization: Bearer <JWT_TOKEN>
{
"todoId": "unique-todo-id"
}
To test the functions locally, you can use the Serverless framework's offline capabilities or invoke functions directly with sample payloads.
For example, to test createTodo
:
serverless invoke local -f createTodo -p path/to/sample-event.json
To update the deployed Lambda functions, run:
serverless deploy
This project is licensed under the MIT License.