This document provides instructions on how to use the APIs for the authentication and posts services.
Base URLs:
- Authentication Service:
http://localhost:3001 - Posts Service:
http://localhost:3002
Handles user registration, login, logout, and session management.
- Endpoint:
POST /auth/register - Description: Creates a new user account.
- Request Body:
{ "username": "testuser", "password": "password123" } - Success Response:
- Code:
201 Created - Body:
"User registered"
- Code:
- Error Response:
- Code:
400 Bad Request(e.g., if username already exists)
- Code:
- Endpoint:
POST /auth/login - Description: Authenticates a user and creates a session. A session cookie (
connect.sid) is returned upon success, which must be included in subsequent requests to authenticated endpoints. - Request Body:
{ "username": "testuser", "password": "password123" } - Success Response:
- Code:
200 OK - Body:
"Logged in"
- Code:
- Error Response:
- Code:
401 Unauthorized(Invalid credentials)
- Code:
- Endpoint:
POST /auth/logout - Description: Destroys the current user's session.
- Success Response:
- Code:
200 OK - Body:
"Logged out"
- Code:
- Endpoint:
GET /auth/me - Description: Retrieves the profile of the currently logged-in user.
- Authentication: Requires a valid session cookie.
- Success Response:
- Code:
200 OK - Body:
{ "_id": "60c72b2f9b1d8c001f8e4d2a", "username": "testuser" }
- Code:
- Error Response:
- Code:
401 Unauthorized(If not authenticated)
- Code:
Manages CRUD operations for posts. Requires authentication for creating, updating, and deleting posts.
- Endpoint:
POST /posts - Description: Creates a new post. The author ID is automatically taken from the session.
- Authentication: Requires a valid session cookie.
- Request Body:
{ "title": "My First Post", "content": "This is the content of my first post." } - Success Response:
- Code:
201 Created - Body: The created post object.
- Code:
- Error Response:
- Code:
401 Unauthorized
- Code:
- Endpoint:
GET /posts - Description: Retrieves a list of all posts.
- Success Response:
- Code:
200 OK - Body: An array of post objects.
- Code:
- Endpoint:
GET /posts/:id - Description: Retrieves a single post by its ID.
- Success Response:
- Code:
200 OK - Body: The requested post object.
- Code:
- Error Response:
- Code:
44 Not Found
- Code:
- Endpoint:
PUT /posts/:id - Description: Updates an existing post. Only the author of the post can perform this action.
- Authentication: Requires a valid session cookie.
- Request Body:
{ "title": "Updated Title", "content": "Updated content." } - Success Response:
- Code:
200 OK - Body: The updated post object.
- Code:
- Error Response:
- Code:
401 Unauthorized(Not logged in) - Code:
403 Forbidden(User is not the author) - Code:
404 Not Found
- Code:
- Endpoint:
DELETE /posts/:id - Description: Deletes a post. Only the author of the post can perform this action.
- Authentication: Requires a valid session cookie.
- Success Response:
- Code:
200 OK - Body:
"Post deleted"
- Code:
- Error Response:
- Code:
401 Unauthorized(Not logged in) - Code:
403 Forbidden(User is not the author) - Code:
404 Not Found
- Code: