This project is a simple OTP (One-Time Password) Verification system built with Node.js, Express, and MongoDB. Users can request an OTP, which is then emailed to them. Users can then use the OTP to log in. The system uses JWT tokens for authentication after login.
- OTP Generation and email delivery
- Login with OTP
- OTPs are valid for 5 minutes
- OTPs cannot be reused
- Users are blocked for 1 hour after 5 consecutive wrong OTPs
- There should be a minimum 1 min gap between two OTP generation requests
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
- Node.js
- npm
- MongoDB
- Clone the repository:
git clone https://github.com/yourusername/otp-verification.git
- Install dependencies:
Navigate to the project directory and run:
npm install
- Setup environment variables:
Create a .env file in your project root and add the following:
EMAIL_SERVICE_USER=<your-email-address>
EMAIL_SERVICE_PASS=<your-email-password>
SECRET_KEY=<your-secret-key-for-jwt>
DB_CONNECTION_STRING=<your-mongo-db-connection-string>
- Run the server:
npm start
The server will run on port 5000.
Postman or CURL can be used for testing the API endpoints.
- To generate an OTP:
POST request to http://localhost:5000/generate-otp
with request body:
{
"email": "<your-email>"
}
An OTP will be sent to the provided email.
- To login:
POST request to http://localhost:5000/login
with request body:
{
"email": "<your-email>",
"OTP": "<your-OTP>"
}
If the OTP is correct, a JWT token will be returned.
create otp
curl --location --request POST 'https://nodejs-email-auth.vercel.app/auth/generate-otp' \
--header 'Content-Type: application/json' \
--data-raw '{
"email": "<user-email>"
}'
login
curl --location --request POST 'https://nodejs-email-auth.vercel.app/auth/login' \
--header 'Content-Type: application/json' \
--data-raw '{
"email": "<user-email>",
"OTP": ""<user-otp>""
}'
If you want to contribute to this project, you're always welcome! You can create a pull request or issue, and i will take a look at it.
This project is licensed under the MIT License - see the LICENSE.md file for details