Skip to main content

Overview

The Users API provides endpoints for managing user accounts in the Adoptme system. Users can be listed, retrieved, updated, and deleted. Each user has a profile with personal information and a list of adopted pets. Base Path: /api/users Source Files:
  • Routes: src/routes/users.router.js
  • Controller: src/controllers/users.controller.js
  • Model: src/dao/models/User.js

User Model

The User schema includes the following fields:
_id
ObjectId
required
MongoDB-generated unique identifier
first_name
string
required
User’s first name
last_name
string
required
User’s last name
email
string
required
User’s email address (must be unique)
password
string
required
Hashed password (bcrypt)
role
string
default:"user"
User role (default: “user”)
pets
array
default:"[]"
Array of pet ObjectIds that the user has adopted
[
  { "_id": "60acc54545c8e82e0475100a" },
  { "_id": "60acc54545c8e82e0475100b" }
]

Get All Users

curl -X GET http://localhost:8080/api/users

Endpoint

GET /api/users
Retrieves a list of all users in the system.

Response

status
string
“success”
payload
array
Array of user objects

Example Response

{
  "status": "success",
  "payload": [
    {
      "_id": "6893eaba2ac0b16fa177be7a",
      "first_name": "Jerónimo",
      "last_name": "Arroyo Alonzo",
      "email": "[email protected]",
      "password": "$2b$10$xjkkakjsiz...",
      "role": "user",
      "pets": []
    },
    {
      "_id": "6893eaba2ac0b16fa177be7b",
      "first_name": "María",
      "last_name": "González",
      "email": "[email protected]",
      "password": "$2b$10$abc123...",
      "role": "user",
      "pets": [
        { "_id": "60acc54545c8e82e0475100a" }
      ]
    }
  ]
}

Error Responses


Get User by ID

curl -X GET http://localhost:8080/api/users/6893eaba2ac0b16fa177be7a

Endpoint

GET /api/users/:uid
Retrieves a single user by their unique ID.

Path Parameters

uid
string
required
The unique identifier (MongoDB ObjectId) of the user

Response

status
string
“success”
payload
object
User object with all fields

Example Response

{
  "status": "success",
  "payload": {
    "_id": "6893eaba2ac0b16fa177be7a",
    "first_name": "Jerónimo",
    "last_name": "Arroyo Alonzo",
    "email": "[email protected]",
    "password": "$2b$10$xjkkakjsiz...",
    "role": "user",
    "pets": []
  }
}

Error Responses


Update User

curl -X PUT http://localhost:8080/api/users/6893eaba2ac0b16fa177be7a \
  -H "Content-Type: application/json" \
  -d '{
    "first_name": "Jerónimo",
    "last_name": "Arroyo Pérez",
    "email": "[email protected]"
  }'

Endpoint

PUT /api/users/:uid
Updates an existing user’s information.

Path Parameters

uid
string
required
The unique identifier (MongoDB ObjectId) of the user to update

Request Body

Send any user fields you want to update. All fields are optional:
first_name
string
Updated first name
last_name
string
Updated last name
email
string
Updated email address
role
string
Updated role
pets
array
Updated pets array (array of pet ObjectIds)
Do not update the password field directly through this endpoint. Password updates should go through proper authentication flows with hashing.

Example Request Body

{
  "first_name": "Jerónimo",
  "last_name": "Arroyo Pérez",
  "email": "[email protected]"
}

Response

status
string
“success”
message
string
“User updated”

Example Response

{
  "status": "success",
  "message": "User updated"
}

Error Responses


Delete User

curl -X DELETE http://localhost:8080/api/users/6893eaba2ac0b16fa177be7a

Endpoint

DELETE /api/users/:uid
Deletes a user from the system.

Path Parameters

uid
string
required
The unique identifier (MongoDB ObjectId) of the user to delete

Response

status
string
“success”
message
string
“User deleted”

Example Response

{
  "status": "success",
  "message": "User deleted"
}

Error Responses

Deleting a user does not automatically delete their adoptions or remove them from adopted pets. You may need to handle these relationships separately.

Implementation Details

Controller Source

The Users controller (src/controllers/users.controller.js) implements the following logic:

Router Configuration

Routes are defined in src/routes/users.router.js:
router.get('/', usersController.getAllUsers);
router.get('/:uid', usersController.getUser);
router.put('/:uid', usersController.updateUser);
router.delete('/:uid', usersController.deleteUser);

Sessions API

Register and authenticate users

Adoptions API

Link users to adopted pets

Build docs developers (and LLMs) love