A RESTful API built using Node.js, Express, and MongoDB that allows users to register, log in, and manage their contacts. Authentication is handled using JWT, and password security is ensured with bcrypt.
- User Registration
- User Login
- JWT Authentication Middleware
- Create, Read, Update, and Delete (CRUD) operations for contacts
- Secure password storage with bcrypt
- Node.js
- Express.js
- MongoDB with Mongoose
- JWT (JSON Web Token) for authentication
- bcrypt for password hashing
Ensure you have the following installed:
-
Clone the repository:
git clone <repository-url> cd <project-directory>
-
Install dependencies:
npm install
-
Create a
.envfile in the root directory and add the following variables:PORT=5000 MONGO_URL=<your-mongodb-connection-string> JWT=<your-secret-key>
-
Start the server:
npm start
The API will be running at
http://localhost:3000.
POST /api/auth/registerRequest Body:
{
"username": "user123",
"email": "[email protected]",
"password": "securepassword"
}POST /api/user/loginRequest Body:
{
"email": "[email protected]",
"password": "securepassword"
}GET /api/contact/
Headers: Authorization: Bearer <token>GET /api/contact/:id
Headers: Authorization: Bearer <token>POST /api/contact/new
Headers: Authorization: Bearer <token>Request Body:
{
"name": "John Doe",
"email": "[email protected]",
"phone": "1234567890"
"type":"personal"
}PUT /api/contact/:id
Headers: Authorization: Bearer <token>DELETE /api/contact/:id
Headers: Authorization: Bearer <token>The API uses JWT-based authentication. Users must log in to receive a token, which must be included in the Authorization header for contact-related requests.
This project is licensed under the MIT License.