Certainly! Here's a sample README file to explain how to use your user authentication API. You can modify and expand upon it as needed:
This is a user authentication API built with Node.js, Express, MongoDB, and JWT token authentication. It provides user registration, login, and user profile features, along with email confirmation functionality.
Before using this API, ensure you have the following prerequisites installed:
- Node.js (version 14 or higher)
- npm or yarn
- MongoDB (and a running database)
- An email service account (e.g., Gmail) for sending email confirmations
-
Clone the repository to your local machine:
git clone https://github.com/yourusername/user-auth-api.git
-
Navigate to the project directory:
cd user-auth-api -
Install the project dependencies:
npm install # or yarn install -
Configure the environment variables:
-
Create a
.envfile in the project root and define the following variables:MONGODB_URI: Your MongoDB database URI.EMAIL_SERVICE: Your email service provider (e.g., 'Gmail').EMAIL_USER: Your email service account username.EMAIL_PASS: Your email service account password.JWT_SECRET: A strong and secure secret key for JWT token generation.PORT: The port on which the API will run (default: 3000).
-
-
Start the server:
npm start # or yarn start
- Endpoint:
POST /auth/register - Request Body:
name(string): User's nameemail(string): User's emailpassword(string): User's password
- Response:
- 201 Created: User registered successfully (with JWT token)
- 400 Bad Request: Invalid request
- 409 Conflict: Email already in use
- Endpoint:
POST /auth/login - Request Body:
email(string): User's emailpassword(string): User's password
- Response:
- 200 OK: Login successful (with JWT token)
- 401 Unauthorized: Invalid password
- 404 Not Found: User not found
- Endpoint:
GET /user/profile - Headers:
Authorization(string): JWT token obtained during registration or login
- Response:
- 200 OK: User profile data
- 401 Unauthorized: Invalid token
- 404 Not Found: User not found
- Endpoint:
GET /auth/protected - Headers:
Authorization(string): JWT token obtained during registration or login
- Response:
- 200 OK: Access granted to protected route
- 401 Unauthorized: Invalid token
The API is configured to send a confirmation email after user registration. Make sure to configure your email service provider settings and email content in the emailService.js file.
If you would like to contribute to this project or report issues, please feel free to create a pull request or an issue on the project's GitHub repository.
This project is licensed under the MIT License - see the LICENSE file for details.
Feel free to adapt and expand this README to suit your project's specific requirements and provide more detailed information about your API and its usage.