A RESTful API for managing personal contacts with user authentication. Built with Node.js, Express.js, and MongoDB.
- 🔐 User registration and authentication with JWT
- 📇 Full CRUD operations for contacts
- 🔒 Password hashing with bcrypt
- 👤 User-specific contacts (each user can only access their own contacts)
- ⚡ Async error handling
- Runtime: Node.js
- Framework: Express.js
- Database: MongoDB with Mongoose ODM
- Authentication: JSON Web Tokens (JWT)
- Password Hashing: bcrypt
- Node.js (v14 or higher)
- MongoDB (local or Atlas)
- npm or yarn
git clone https://github.com/yourusername/contact-manager.git
cd contact-managernpm installCreate a .env file in the root directory:
PORT=5000
MONGO_URI=mongodb://localhost:27017/contact-manager
JWT_SECRET=your_super_secret_key_hereDevelopment mode (with auto-reload):
npm run devProduction mode:
npm startThe server will start on http://localhost:5000
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /api/users/register |
Register a new user | ❌ |
| POST | /api/users/login |
Login user | ❌ |
| GET | /api/users/current |
Get current user info | ✅ |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| GET | /api/contacts |
Get all contacts | ✅ |
| POST | /api/contacts |
Create a contact | ✅ |
| GET | /api/contacts/:id |
Get a contact | ✅ |
| PUT | /api/contacts/:id |
Update a contact | ✅ |
| DELETE | /api/contacts/:id |
Delete a contact | ✅ |
POST /api/users/register
Content-Type: application/json
{
"username": "johndoe",
"email": "john@example.com",
"password": "password123"
}Response:
{
"_id": "64a1b2c3d4e5f6g7h8i9j0k1",
"email": "john@example.com"
}POST /api/users/login
Content-Type: application/json
{
"email": "john@example.com",
"password": "password123"
}Response:
{
"message": "Login Successfully",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}POST /api/contacts
Authorization: <your_jwt_token>
Content-Type: application/json
{
"name": "Jane Doe",
"email": "jane@example.com",
"phone": "123-456-7890"
}Response:
{
"message": "Contact Created",
"contact": {
"_id": "64a1b2c3d4e5f6g7h8i9j0k2",
"userId": "64a1b2c3d4e5f6g7h8i9j0k1",
"name": "Jane Doe",
"email": "jane@example.com",
"phone": "123-456-7890",
"createdAt": "2026-01-30T10:00:00.000Z",
"updatedAt": "2026-01-30T10:00:00.000Z"
}
}GET /api/contacts
Authorization: <your_jwt_token>GET /api/contacts/:id
Authorization: <your_jwt_token>PUT /api/contacts/:id
Authorization: <your_jwt_token>
Content-Type: application/json
{
"name": "Jane Smith",
"phone": "987-654-3210"
}DELETE /api/contacts/:id
Authorization: <your_jwt_token>contact-manager/
├── config/
│ └── db.js # Database connection
├── controllers/
│ ├── contactController.js # Contact CRUD logic
│ └── userController.js # User auth logic
├── middleware/
│ ├── auth.js # JWT authentication
│ └── errorHandler.js # Global error handler
├── models/
│ ├── contactModel.js # Contact schema
│ └── userModel.js # User schema
├── routes/
│ ├── contactRoutes.js # Contact endpoints
│ └── userRoutes.js # User endpoints
├── .env # Environment variables
├── constants.js # HTTP status codes
├── package.json
├── README.md
└── server.js # Entry point
This API uses JWT (JSON Web Tokens) for authentication. After logging in, include the token in the Authorization header for all protected routes:
Authorization: <your_jwt_token>
Tokens expire after 1 day.
The API returns consistent error responses:
{
"title": "Error Type",
"message": "Error description",
"stackTrace": "..."
}| Status Code | Title | Description |
|---|---|---|
| 400 | Validation Failed | Invalid input data |
| 401 | Unauthorized | Invalid or missing token |
| 403 | Forbidden | Access denied |
| 404 | Not Found | Resource not found |
| 500 | Server Error | Internal server error |
Deepraj Singh
This project is licensed under the ISC License.