This is a serverless application built with Node.js, Express, and MongoDB. It provides endpoints for user registration, login, and protected routes using JSON Web Tokens (JWT) for authentication. The application is designed to run on AWS Lambda using the Serverless Framework.
- User registration with MongoDB storage for usernames and hashed passwords.
- User login with JWT authentication.
- Protected routes for authenticated users.
- Local development environment setup with
serverless-offline.
Before running this application, make sure you have the following installed:
- Node.js
- npm (Node.js package manager)
- MongoDB Atlas account (for production deployment)
- AWS CLI and AWS account (for deployment to AWS Lambda)
# Clone the repository
git clone <repository-url>
# Navigate to the project directory
cd serverless-auth
# Install dependencies
npm install-
Create a
.envfile in the root directory with the following environment variables:MONGO_USERNAME=your_mongodb_username MONGO_PASSWORD=your_mongodb_passwordReplace
your_mongodb_usernameandyour_mongodb_passwordwith your MongoDB Atlas username and password. -
Update the MongoDB connection URI in
db.jsto use the environment variables:const connectionString = `mongodb+srv://${process.env.MONGO_USERNAME}:${process.env.MONGO_PASSWORD}@your-mongodb-uri`;
Replace
your-mongodb-uriwith your MongoDB connection URI.
# Start the local server
npm startTest the endpoints using tools like Postman or cURL:
- Register a new user:
POST /register - Login:
POST /login - Access protected routes:
GET /validate - Simple GET Route:
GET /hello
To deploy the application to AWS Lambda, make sure you have configured the AWS CLI and have your AWS credentials set up. Then, run:
serverless deployThis project is licensed under the MIT License - see the LICENSE file for details.