This Node.js and Express.js API provides user authentication functionality, including sign-up, sign-in, email verification, and password encryption. The system uses MongoDB
for data storage and Nodemailer
for sending verification emails.
- Sign-Up: Allows new users to register. Passwords are hashed using bcrypt for security.
- Email Verification: A verification email is sent to confirm the user's identity. Verification links expire after 6 hours.
- Sign-In: Users can log in if they have verified their email addresses.
- Error Handling: Clear and descriptive error messages help users troubleshoot issues with registration, verification, and login.
- Node.js with Express: Backend server
- MongoDB: Data storage
- Nodemailer: Email sending for verification
- dotenv: Environment variable management
- bcrypt: Password hashing for security
Ensure you have the following installed:
- Node.js
- MongoDB
- NPM (Node Package Manager)
-
Clone this repository:
git clone <repository_url> cd <repository_name>
-
Install dependencies:
npm install
-
Create a
.env
file and configure the following environment variables:AUTH_EMAIL=<your_email> AUTH_PASSWORD=<your_email_password> MONGO_URI=<your_mongo_connection_uri>
-
Ensure MongoDB is running.
-
Start the server:
npm start
- Method:
POST
- Endpoint:
/signup
- Request Body:
{ "name": "User Name", "email": "user@example.com", "password": "user_password", "dateOfBirth": "YYYY-MM-DD" }
- Response: JSON with success/failure message.
- Method:
GET
- Endpoint:
/verify/:userId/:uniqueString
- Purpose: Verifies user email with the unique string provided in the verification email.
- Response: Redirects to success/error page.
- Method:
POST
- Endpoint:
/signin
- Request Body:
{ "email": "user@example.com", "password": "user_password" }
- Response: JSON with success/failure message.
Notes
- Verification Link: The link expires in 6 hours.
- MongoDB Schema: Ensure that
User
andUserVerification
schemas are set up in your MongoDB database.
- Nodemailer: Make sure to enable "less secure app access" on your Gmail account, or consider using OAuth for production environments.
- Port Configuration: Default port is 5000, but you can adjust as needed.
This project is licensed under the MIT License.