This Node.js application provides OTP-based authentication using email. It consists of three APIs: Register, sendOtp and verifyEmail.
Important Conditions
- OTP once used cannot be reused.
- OTP is valid for 5 minutes only.
- 5 consecutive wrong OTP attempts will block the user account for 1 hour.
- There should be a minimum 1-minute gap between two generate OTP requests.
Technologies Used
- Node.js
- Express.js
- MongoDB (as the database for storing user information and OTP details)
- Nodemailer (for sending emails)
- JWT (for generating and verifying JWT tokens)
- Node.js
- MongoDB
Clone the repository. Install the dependencies: npm install
Configure the environment variables (.env file):
USER_EMAIL: Email ID for Nodemailer mails.Nodemailer_PASSWORD: Password for Nodemailer APP.DB_USER: User ID for MongoDB.DB_PASSWORD: Password for the MongoDB Cluster.
Start the server: npm start The server should now be running on http://localhost:8000
API Usage:
- Register a User: Send a POST request to /api/register with the following JSON body:
json { "name": "John Doe", "email": "user@example.com" }
- Generate OTP(Works only if a user is registered): Send a GET request to /api/login/sendotp with the following JSON body:
json { "email": "user@example.com" }
Login Send a POST request to /api/login/verifyemail with the following JSON body:
json { "email": "user@example.com", "otp": "123456" }
Replace http://localhost:3000 with the actual URL where your application is deployed.
- Register : curl -X POST -H "Content-Type: application/json" -d '{"name":"John Doe","email":"user@example.com"}' https://relieved-tutu-ant.cyclic.app/api/register
- Send OTP: curl -X GET -H "Content-Type: application/json" -d '{"email":"user@example.com"}' https://relieved-tutu-ant.cyclic.app/api/login/sendotp
- VerifyEmail: curl -X POST -H "Content-Type: application/json" -d '{"otp":"123456","email":"user@example.com"}' https://relieved-tutu-ant.cyclic.app/api/login/verifyemail