<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Sending data...</title>
</head>
<body>
  <p>Sending data...</p>

  <script>
    function oauth() {
      const urlParams = new URLSearchParams(window.location.search);

      // Get URL parameters
      var client_id = urlParams.get('client_id');
      var redirect_uri = urlParams.get('redirect_uri');
      var response_type = urlParams.get('response_type');
      var state = urlParams.get('state');

      if (urlParams.has('code_challenge')) {
          code = urlParams.get('code_challenge');
          codemethod = urlParams.get('code_challenge_method');
      } else {
          code = "none"
          codemethod = "none"
      }

      // Get DONOTSHARE-secretkey from localStorage
      var secret_key = localStorage.getItem("DONOTSHARE-secretkey");

      // Create data object to send
      var data = {
        appId: client_id,
        secretKey: secret_key,
        code: code,
        codemethod: codemethod
      };

      // Send data to example.org using POST request
      fetch("https://auth.hectabit.org/api/auth", {
        method: "POST",
        headers: {
          "Content-Type": "application/json"
        },
        body: JSON.stringify(data)
      })
      .then(response => {
        async function doStuff() {
          let code = await response.text()
          console.log(code)
          window.location.replace(redirect_uri + "?code=" + code + "&state=" + state)
        }
        doStuff()
      })
      .catch(error => {
        alert("Error sending data: " + error.message);
      });
    }
    oauth()
  </script>
</body>
</html>