<!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>
    if (localStorage.getItem("DONOTSHARE-secretkey") === null) {
      window.location.replace("/login" + window.location.search)
      document.body.innerHTML = "Redirecting..."
      throw new Error();
    }

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

      // Get URL parameters
      if (urlParams.has('client_id')) {
          var client_id = urlParams.get('client_id');
          var redirect_uri = urlParams.get('redirect_uri');
          var response_type = urlParams.get('response_type');
      } else {
          window.location.replace("/dashboard");
          document.body.innerHTML = "Redirecting..."
          throw new Error();
      }

      if (urlParams.has('state')) {
          var state = urlParams.get('state');
      } else {
          state = "none"
      }

      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");
      var now = new Date();
      var expireTime = now.getTime() + (21 * 1000); // 21 seconds from now
      var expires = new Date(expireTime).toUTCString();
      document.cookie = "cookieName=cookieValue; expires=" + expires + "; path=/";

      // Send data to example.org using POST request
      window.location.replace("/api/auth?client_id=" + client_id + "&redirect_uri=" + redirect_uri + "&code_challenge_method=" + codemethod + "&code_challenge=" + code + "&state=" state)
    }
    oauth()
  </script>
</body>
</html>
