QR Code ‐ Session Initalization - Smart-Cart-System/backend-fastapi GitHub Wiki

Collecting workspace informationHere is the documentation for the get_qr and scan_qr_code endpoints, tailored for the front-end team:


Endpoint: Get QR Code

URL

GET /customer-session/qr/{cart_id}

Description

Generates a QR code for a specific cart. The QR code contains an encoded token that can be scanned to initiate a customer session.

Path Parameters

  • cart_id (int): The unique identifier of the cart.

Response

  • 200 OK: Returns the QR code as a PNG image.
  • 404 Not Found: If the cart with the given cart_id does not exist.

Headers

  • Content-Type: image/png

Example Usage

const cartId = 123; // Replace with the actual cart ID
fetch(`http://<your-server-address>/customer-session/qr/${cartId}`)
  .then(response => {
    if (!response.ok) {
      throw new Error("Failed to fetch QR code");
    }
    return response.blob();
  })
  .then(blob => {
    const qrCodeUrl = URL.createObjectURL(blob);
    document.getElementById("qrCodeImage").src = qrCodeUrl;
  })
  .catch(error => {
    console.error("Error fetching QR code:", error);
  });

Endpoint: Scan QR Code

URL

POST /customer-session/scan-qr

Description

Processes a scanned QR code and creates a customer session if the QR code is valid.

Request Body

  • JSON Object:
    • token (string): The token extracted from the scanned QR code.

Response

  • 200 OK: Returns the created session details.
    • Response Body:
      {
        "session_id": 1,
        "user_id": 42,
        "cart_id": 123,
        "is_active": true,
        "created_at": "2023-10-01T12:00:00Z"
      }
      
  • 401 Unauthorized: If the QR code is invalid or expired.
  • 404 Not Found: If the cart associated with the QR code does not exist.
  • 400 Bad Request: If the cart is not available.

Example Usage

const qrToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."; // Replace with the actual token
fetch("http://<your-server-address>/customer-session/scan-qr", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ token: qrToken }),
})
  .then(response => {
    if (!response.ok) {
      throw new Error("Failed to scan QR code");
    }
    return response.json();
  })
  .then(session => {
    console.log("Session created:", session);
    // Handle the session details (e.g., redirect to a new page)
  })
  .catch(error => {
    console.error("Error scanning QR code:", error);
  });