Users

The Users API provides endpoints for user profile management and user administration. Regular users can manage their own profiles, while administrators have additional capabilities for user management.

Endpoints

Get Current User Profile

Retrieves the profile of the currently authenticated user.

Endpoint: GET /users/current

Authentication: Required (Bearer token)

Request: No body required

Success Response (200 OK):

{
  "user": {
    "_id": "60b7c8b4f9b3c12345678901",
    "username": "johndoe",
    "email": "john@example.com",
    "role": "user",
    "firstName": "John",
    "lastName": "Doe",
    "socialLinks": {
      "website": "https://johndoe.com",
      "linkedin": "https://linkedin.com/in/johndoe",
      "x": "https://x.com/johndoe"
    },
    "createdAt": "2024-01-15T10:30:00.000Z",
    "updatedAt": "2024-01-20T15:45:00.000Z"
  }
}

Error Responses:

  • 401 - Unauthorized (invalid or missing token)

  • 500 - Internal server error


Update Current User Profile

Updates the profile of the currently authenticated user.

Endpoint: PUT /users/current

Authentication: Required (Bearer token)

Request Body (all fields optional):

Success Response (200 OK):

Error Responses:

  • 400 - Validation errors (invalid email, username taken, etc.)

  • 401 - Unauthorized

  • 404 - User not found

  • 500 - Internal server error

Validation Rules:

  • username: Max 20 characters, must be unique

  • email: Valid email format, max 50 characters, must be unique

  • password: Min 8 characters

  • first_name, last_name: Max 20 characters each

  • Social links: Valid URLs, max 100 characters each


Delete Current User Account

Permanently deletes the currently authenticated user's account and all associated data.

Endpoint: DELETE /users/current

Authentication: Required (Bearer token)

Request: No body required

Success Response (204 No Content): Empty response body

Error Responses:

  • 401 - Unauthorized

  • 500 - Internal server error

Warning: This action is irreversible and will delete:

  • User profile and all personal data

  • All blog posts authored by the user

  • All comments made by the user

  • All likes given by the user


Admin-Only Endpoints

The following endpoints require administrator privileges (role: "admin").

Get All Users

Retrieves a paginated list of all users in the system.

Endpoint: GET /users/

Authentication: Required (Bearer token with admin role)

Query Parameters:

  • limit (optional): Number of users per page (1-50, default: 20)

  • offset (optional): Number of users to skip (default: 0)

Example Request:

Success Response (200 OK):

Error Responses:

  • 400 - Invalid query parameters

  • 401 - Unauthorized

  • 403 - Forbidden (user is not admin)

  • 500 - Internal server error


Get User by ID

Retrieves detailed information about a specific user.

Endpoint: GET /users/{userId}

Authentication: Required (Bearer token with admin role)

Path Parameters:

  • userId: MongoDB ObjectId of the user

Success Response (200 OK):

Error Responses:

  • 400 - Invalid userId format

  • 401 - Unauthorized

  • 403 - Forbidden (user is not admin)

  • 404 - User not found

  • 500 - Internal server error


Delete User by ID

Permanently deletes a specific user account and all associated data.

Endpoint: DELETE /users/{userId}

Authentication: Required (Bearer token with admin role)

Path Parameters:

  • userId: MongoDB ObjectId of the user to delete

Success Response (204 No Content): Empty response body

Error Responses:

  • 400 - Invalid userId format

  • 401 - Unauthorized

  • 403 - Forbidden (user is not admin)

  • 404 - User not found

  • 500 - Internal server error

Warning: This action is irreversible and will delete all user data and associated content.

Code Examples

Get Current User Profile

Update User Profile

Admin: Get All Users with Pagination

Complete User Management Class

User Roles and Permissions

Regular User (role: "user")

  • ✅ View own profile (GET /users/current)

  • ✅ Update own profile (PUT /users/current)

  • ✅ Delete own account (DELETE /users/current)

  • ❌ View other users' profiles

  • ❌ Manage other user accounts

Administrator (role: "admin")

  • ✅ All regular user permissions

  • ✅ View all users (GET /users/)

  • ✅ View any user profile (GET /users/{userId})

  • ✅ Delete any user account (DELETE /users/{userId})

  • ✅ Access user management features

Best Practices

  1. Profile Updates: Only send fields that need to be updated

  2. Password Changes: Always require current password verification in production

  3. Data Validation: Validate all input on the client side before sending

  4. Error Handling: Implement proper error handling for all user operations

  5. Pagination: Use appropriate page sizes when fetching user lists

  6. Security: Never expose sensitive user data unnecessarily

Last updated