Manages user-related operations such as registration, login, profile management, and user follow functionality.
- Node.js
- MongoDB
- Install dependencies:
npm install
- Configure environment variables in
.env
file:PORT=5000 MONGO_URI=your_mongo_uri JWT_SECRET=your_secret_key
- Start the service:
npm start
- Method: POST
- Endpoint: /api/users/register
- Description: Register a new user with name, email, mobile number, and password.
- Method: POST
- Endpoint: /api/users/login
- Description: Login with email and password to obtain a JWT token.
- Method: GET
- Endpoint: /api/users/:id
- Description: Get user details by ID. Requires authentication.
- Method: PUT
- Endpoint: /api/users/:id
- Description: Update user details by ID. Requires authentication.
- Method: DELETE
- Endpoint: /api/users/:id
- Description: Delete a user by ID. Requires authentication.
- Method: POST
- Endpoint: /api/users/follow
- Description: Follow another user. Requires authentication.
- Method: GET
- Endpoint: /api/users/search
- Description: Search for users by name or email. Requires authentication.
- The endpoints that require authentication (GET, PUT, DELETE, POST /follow, GET /search) use JWT tokens.
- Include the JWT token in the Authorization header with the format "Bearer ".
- Clone the repository.
- Install dependencies with
npm install
. - Set environment variables (e.g., PORT, MONGO_URI, JWT_SECRET) in a .env file.
- Start the server with
npm start
.
- Register a user:
curl -X POST http://localhost:5000/api/users/register -H "Content-Type: application/json" -d '{"name": "John Doe", "email": "john@example.com", "mobile": "1234567890", "password": "password"}'
- Login:
curl -X POST http://localhost:5000/api/users/login -H "Content-Type: application/json" -d '{"email": "john@example.com", "password": "password"}'
- Get user details (requires authentication):
curl -X GET http://localhost:5000/api/users/<user_id> -H "Authorization: Bearer <jwt_token>"
- Update user details (requires authentication):
curl -X PUT http://localhost:5000/api/users/<user_id> -H "Authorization: Bearer <jwt_token>" -H "Content-Type: application/json" -d '{"name": "John Smith"}'
- Delete user (requires authentication):
curl -X DELETE http://localhost:5000/api/users/<user_id> -H "Authorization: Bearer <jwt_token>"
- Follow a user (requires authentication):
curl -X POST http://localhost:5000/api/users/follow -H "Authorization: Bearer <jwt_token>" -H "Content-Type: application/json" -d '{"userIdToFollow": "<user_id_to_follow>"}'
- Search users (requires authentication):
curl -X GET http://localhost:5000/api/users/search?query=<search_query> -H "Authorization: Bearer <jwt_token>"
Replace <user_id>
, <jwt_token>
, <user_id_to_follow>
, and <search_query>
with actual values when testing the endpoints.
+------------------------------------------------+
| User Service |
+------------------------------------------------+
| +----------------+ |
| | API Gateway | |
| +----------------+ |
| | |
| | |
| +------------+------------+ |
| | | | |
| +----------------+ +-------------+ +-----------------+
| | Authentication| | User | | User Data |
| | Service | | Management | | Storage |
| | | | Service | | (MongoDB) |
| +----------------+ +-------------+ +-----------------+
| | | |
| | | |
| +-------------------+ +-----------------+ +----------------+
| | Router | | Controller | | Database |
| +-------------------+ +-----------------+ +----------------+
| | | |
| | | |
| +------------------+ +--------------------+ |
| | Middleware | | Business Logic | |
| +------------------+ +--------------------+ |
| | | |
| +------------------+ |
| | |
+------------------------------------------------+
-
API Gateway:
- Acts as the entry point for external client requests.
- Routes requests to the appropriate services based on the endpoint.
-
Authentication Service:
- Handles user authentication and authorization.
- Generates and validates JWT tokens.
- Protects routes that require authentication.
-
User Management Service:
- Manages user-related operations such as registration, login, profile updates, and deletions.
- Handles user interactions and business logic related to user management.
-
User Data Storage (MongoDB):
- Stores user data including profiles, authentication details, and related information.
- Provides data persistence for the user-service.
-
Router:
- Defines API endpoints and routes incoming requests to the appropriate controllers.
-
Controller:
- Implements the business logic for each endpoint.
- Validates input data, processes requests, and interacts with the database.
-
Middleware:
- Contains middleware functions such as authentication middleware to verify JWT tokens.
- Executes before reaching the controller to perform pre-processing tasks.