exports.ExchangeTokenFunction = async (req, res) => {
  try {
    const { code } = req.body;

    // Exchange authorization code for access token
    const tokenEndpoint = "https://oauth2.googleapis.com/token";
    const tokenParams = new URLSearchParams({
      client_id:
        "460702587834-648e1i2dmfu35i7ip1p5qa10mkcmastg.apps.googleusercontent.com",
      client_secret: "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",
      code: code,
      redirect_uri: "http://localhost:5500/Pages/callback.html",
      grant_type: "authorization_code",
    });

    // Use dynamic import() to import node-fetch
    const fetch = await import("node-fetch");

    const response = await fetch.default(tokenEndpoint, {
      method: "POST",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      body: tokenParams,
    });

    const data = await response.json();
    res.json(data); // Return the access token to the client
  } catch (error) {
    console.error("Error:", error);
    res.status(500).json({ error: "Internal Server Error" });
  }
};
