Users

As the name suggests, users are a core part of LetsGo. On this page, we'll dive into the different users endpoints you can use to manage users programmatically. We'll look at how to query, create, update, and delete users.

The user model

The user model contains all the information about your users, such as their full name, email, and password, and whether if they have the admin role.

Properties

  • Name
    _id
    Type
    ObjectId
    Description

    Unique identifier for the user.

  • Name
    name
    Type
    string
    Description

    The name for the user.

  • Name
    email
    Type
    string
    Description

    The email for the user.

  • Name
    password
    Type
    string
    Description

    The hashed password for the user.

  • Name
    is_admin
    Type
    boolean
    Description

    The role of the user.

  • Name
    created_at
    Type
    date
    Description

    Timestamp of when the contact was created.

  • Name
    updated_at
    Type
    date
    Description

    Timestamp of when the contact was updated.

  • Name
    __v
    Type
    int-32
    Description

    The version key of the user.


POST/users/register

Create a user

This endpoint allows you to register a user.

Required attributes

  • Name
    name
    Type
    string
    Description

    The name for the user.

  • Name
    email
    Type
    string
    Description

    The email for the user.

  • Name
    password
    Type
    string
    Description

    The password for the user.

Request body

POST
/users/register
{
  "name" : "Le Thien Nam",
  "email" : "lethiennam111@gmail.com",
  "password": "password" 
}

Response

{
  "message": "User registered successfully",
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2NjNmODY5ZDJlZGNhYjhmMTZhN2ExODciLCJpYXQiOjE3MTU0MzkyNjEsImV4cCI6MTcxNTUyNTY2MX0.CdkcbMM1sm9kceWSopbXlrYZUqhpbrQGUCOgJcvOES0"
}

POST/users/login

Log in a user

This endpoint allows you to add a new user to your user list in LetsGo. To add a user, you must provide their name, email and password.

Required attributes

  • Name
    email
    Type
    string
    Description

    The email of the user.

  • Name
    password
    Type
    string
    Description

    The password of the user.

Request body

POST
/users/login
{
  "email" : "lethiennam111@gmail.com",
  "password": "password" 
}

Response

{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2NjNmODY5ZDJlZGNhYjhmMTZhN2ExODciLCJpYXQiOjE3MTU0Mzk1MDUsImV4cCI6MTcxNTUyNTkwNX0.3_4oS1eyIubo2YfXTD8xe_B-NqxhhCxEEX7zr6PSvbY"
}

GET/users

Retrieve a user

This endpoint allows you to retrieve a user by providing their id. Refer to the list at the top of this page to see which properties are included with user objects.

Required attributes

  • Name
    _id
    Type
    ObjectId
    Description

    The _id of the user, given in token from the Authorization header.

Response

{
  "user": {
    "_id": "662ceba7834c9447b5d960e8",
    "name": "ADMIN",
    "email": "admin@gmail.com",
    "is_admin": true
  }
}

GET/users/orders

Retrieve orders of a user

ONLY ADMIN AND THE USER

This endpoint allows you to retrieve the orders of the user by providing their user id.

Required attributes

  • Name
    user_id
    Type
    ObjectId
    Description

    The _id of the user, given in token from the Authorization header.

Response

[
  {
    "order": {
        "_id": "663612059b2eac88df0e33ca",
        "user_id": "662c20da7f42e1de734fe684",
        "total": 66000,
        "createdAt": "2024-05-04T10:46:29.406Z"
    },
    "orderLines": [
        {
            "product_id": {
                "_id": "6633e6555fec9229af0bab62",
                "name": "BIONICLE: Mask of Light",
                "price": 20000,
                //...
            },
            "quantity": 1
        },
        //...
    ]
  },
//...
]

Was this page helpful?