index.html 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <h1>JSON Stash</h1>
  2. <div id="error"></div>
  3. <input type="username" id="username" name="username" />
  4. <input type="password" id="password" name="password" />
  5. <button id="login">Login</button>
  6. <script type="module">
  7. import CookieManager from "./CookieManager.js"
  8. const loginButton = document.getElementById("login")
  9. const errorDisplay = document.getElementById("error")
  10. const usernameInput = document.getElementById("username")
  11. const passwordInput = document.getElementById("password")
  12. const authEndpoint = "/jwt/login";
  13. loginButton.addEventListener("click", async () => {
  14. errorDisplay.innerHTML = ""
  15. const cookie = new CookieManager()
  16. const loginData = {
  17. "Username": usernameInput.value,
  18. "Password": passwordInput.value
  19. }
  20. const result = await fetch(authEndpoint, {
  21. method: "POST", // *GET, POST, PUT, DELETE, etc.
  22. mode: "cors", // no-cors, *cors, same-origin
  23. cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
  24. credentials: "same-origin", // include, *same-origin, omit
  25. headers: {
  26. "Content-Type": "application/json",
  27. // 'Content-Type': 'application/x-www-form-urlencoded',
  28. },
  29. redirect: "follow", // manual, *follow, error
  30. referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
  31. body: JSON.stringify(loginData), // body data type must match "Content-Type" header
  32. });
  33. if (result.status == 200) {
  34. const response = await result.json()
  35. cookie.add("auth", `${response.token}`);
  36. cookie.add("expires", response.expiration);
  37. document.location = "manage.html"
  38. } else {
  39. errorDisplay.innerHTML = `${result.status} ${result.statusText}`
  40. }
  41. });
  42. </script>