> ## Documentation Index
> Fetch the complete documentation index at: https://ayakaleaf-pro.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Thiết lập công cụ phát triển

## Các công cụ phát triển được khuyến nghị

Các dịch vụ tùy chọn này không bắt buộc để chạy Overleaf, nhưng rất được khuyến nghị cho việc phát triển, gỡ lỗi và kiểm tra trong quá trình phát triển cục bộ.

* **Hoppscotch** là một công cụ kiểm thử API nhẹ tương tự Postman, nhưng chạy trên trình duyệt và rất tiện lợi cho việc phát triển.
  * Frontend: [http://localhost:3000](http://localhost:3000/)
  * Proxy (để vượt qua CORS): [http://localhost:9159](http://localhost:9159/)
* **RedisInsight** cung cấp giao diện trực quan để kiểm tra dữ liệu Redis. Giao diện có tại: [http://localhost:5540](http://localhost:5540/)
* **Mongo Express** là giao diện web cho MongoDB. Bạn có thể truy cập [http://localhost:8081](http://localhost:8081/) với **thông tin đăng nhập**: `admin:pass`

Vui lòng thêm nội dung sau vào `docker-compose.yml` của bạn:

<Accordion title="develop/docker-compose.yml">
  ```yml title="develop/docker-compose.yml" theme={null}
  # services: 
    # Overleaf Dev Containers
    # ...
    
    # Add those for your development
    # API debug
    hoppscotch-frontend:
      image: hoppscotch/hoppscotch-frontend:latest
      ports:
        - "3000:3000"

    hoppscotch-proxy:
      image: hoppscotch/proxyscotch:latest
      environment:
        - PORT=9159
      ports:
        - "9159:9159"

    # Redis-insight
    redis-insight:
      image: redislabs/redisinsight:latest
      ports:
        - "5540:5540"
      depends_on:
        - redis
      volumes:
        - ./data/redis-insight:/data

    # mongo-express
    # basicAuth credentials are "admin:pass"
    mongo-express:
      image: mongo-express
      ports:
        - "8081:8081"
      environment:
        ME_CONFIG_MONGODB_SERVER: mongo
  ```
</Accordion>

### Thiết lập Hoppscotch

#### Proxy cho yêu cầu API

Để thiết lập Hoppscotch, bạn truy cập [http://localhost:3000](http://localhost:3000/), sau đó vào tab cài đặt. Bạn cần đặt `Proxy` thành [http://localhost:9159](http://localhost:9159/), để công cụ gỡ lỗi của bạn có thể tương tác với mạng nội bộ của container.

<Frame>
  <img src="https://mintcdn.com/ayakaleaf-pro/x9kfDjtWlyyhG_mR/images/dev/image-6.png?fit=max&auto=format&n=x9kfDjtWlyyhG_mR&q=85&s=10eae686404ed9da3a1c722ee536e2bb" alt="" width="2210" height="1446" data-path="images/dev/image-6.png" />
</Frame>

Bây giờ bạn có thể thử với [http://web:3000/status](http://web:3000/status) để kiểm tra xem web có đang hoạt động hay không:

<Frame>
  <img src="https://mintcdn.com/ayakaleaf-pro/x9kfDjtWlyyhG_mR/images/dev/image-7.png?fit=max&auto=format&n=x9kfDjtWlyyhG_mR&q=85&s=7acf8612db25a86a3cbe85cb7732ae04" alt="" width="2126" height="1449" data-path="images/dev/image-7.png" />
</Frame>

#### Vấn đề CSRF

<Warning>
  Nếu bạn dùng Postman hoặc các công cụ gỡ lỗi khác để tạo một yêu cầu **post**, bạn có thể thấy việc đăng nhập bị **từ chối (forbidden)**, với lỗi 401. Đó là vì Overleaf được bảo vệ bằng cơ chế xác minh **csrf**.
</Warning>

<Frame>
  <img src="https://mintcdn.com/ayakaleaf-pro/x9kfDjtWlyyhG_mR/images/dev/image-9.png?fit=max&auto=format&n=x9kfDjtWlyyhG_mR&q=85&s=19d2f0be555349d1b1c0e95839afb8e4" alt="" width="1695" height="1200" data-path="images/dev/image-9.png" />
</Frame>

Trong console, bạn có thể thấy:

<Frame>
  <img src="https://mintcdn.com/ayakaleaf-pro/x9kfDjtWlyyhG_mR/images/dev/image-10.png?fit=max&auto=format&n=x9kfDjtWlyyhG_mR&q=85&s=65b38b322d5e940eaa171564dcdb0384" alt="" width="1614" height="551" data-path="images/dev/image-10.png" />
</Frame>

Để xử lý vấn đề này khi phát triển Overleaf, bạn cần thiết lập csrf token. Dưới đây là hướng dẫn chi tiết.

<Steps>
  <Step title="Thêm Pre Request">
    Thêm đoạn mã sau vào Pre-Request

    <Frame>
      <img src="https://mintcdn.com/ayakaleaf-pro/x9kfDjtWlyyhG_mR/images/dev/image-11.png?fit=max&auto=format&n=x9kfDjtWlyyhG_mR&q=85&s=dfc09ccaec771328ed3b1026f951bd0b" alt="" width="1123" height="704" data-path="images/dev/image-11.png" />
    </Frame>

    Script như sau:

    ```js wrap theme={null}
    pm.sendRequest("http://web:3000/dev/csrf", function (err, res) {
        if (err) {
            console.log(err);
            return;
        }

        // 1) Save CSRF token (/dev/csrf)
        const token = res.text().trim();
        pw.env.set("csrftoken", token);
        console.log("csrftoken =", token);

        const setCookieHeader = (res.headers || []).find(h => h.key?.toLowerCase() === "set-cookie")?.value;
        console.log("set-cookie =", setCookieHeader);

        if (!setCookieHeader) return;

        const cookiePair = setCookieHeader.split(";")[0].trim(); // overleaf.sid=...
        pw.env.set("cookie_header", cookiePair);
        console.log("cookie_header =", cookiePair);
    });
    ```
  </Step>

  <Step title="Thêm Request Header">
    Bạn cần thêm các header sau:

    | Tên header | Giá trị header |
    | - | - |
    | Content-Type | `application/json` |
    | X-Csrf-Token | `<<csrftoken>>` |
    | Cookie | `<<cookie_header>>` |

    <Frame>
      <img src="https://mintcdn.com/ayakaleaf-pro/x9kfDjtWlyyhG_mR/images/dev/image-12.png?fit=max&auto=format&n=x9kfDjtWlyyhG_mR&q=85&s=0e62c9be402f8caab1844a7b102eeb29" alt="" width="1018" height="479" data-path="images/dev/image-12.png" />
    </Frame>
  </Step>

  <Step title="Đăng nhập bằng công cụ API">
    Bây giờ bạn có thể đăng nhập bằng tên người dùng và mật khẩu của mình.

    ```json5 theme={null}
    {
      "_csrf": "<<csrftoken>>",
      "email": "admin@overleaf.com",
      "password": "xxxxxx"
    }
    ```

    Đây là phản hồi khi bạn nhập sai tên người dùng hoặc mật khẩu.

    <Frame>
      <img src="https://mintcdn.com/ayakaleaf-pro/x9kfDjtWlyyhG_mR/images/dev/image-13.png?fit=max&auto=format&n=x9kfDjtWlyyhG_mR&q=85&s=cf570827c538094c795c3de8874ba13c" alt="" width="1050" height="830" data-path="images/dev/image-13.png" />
    </Frame>
  </Step>
</Steps>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.