You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

login.js 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //write code in next section
  2. //will run when DOM is loaded
  3. document.addEventListener("DOMContentLoaded", function(){
  4. var form = document.getElementById("loginform");
  5. async function login() {
  6. var un = document.getElementById("un").value;
  7. var pw = document.getElementById("pw").value;
  8. var body = {username:un, password:pw};
  9. var response = await fetch("/login", {
  10. method:"POST",
  11. headers:{
  12. "Accept":"application/json",
  13. "Content-Type":"application/json"
  14. },
  15. body:JSON.stringify(body)
  16. })
  17. var data = await response.json();
  18. console.log("Data: ");
  19. console.log(data);
  20. if(data.suc){
  21. window.location = "/main"
  22. } else {
  23. var loginText = document.getElementById("loginText");
  24. var inputs = document.getElementsByTagName("input")
  25. var inputList = Array.prototype.slice.call(inputs);
  26. loginText.classList.add("fadeout");
  27. setTimeout(function() {
  28. loginText.innerText = "Try Again";
  29. loginText.classList.remove("fadeout");
  30. }, 350);
  31. console.log(inputs);
  32. inputList.forEach(element => {
  33. element.style.borderBottom="1px solid #F00";
  34. if(element.getAttribute("type") === "password"){
  35. element.value = "";
  36. element.focus();
  37. }
  38. });
  39. }
  40. }
  41. form.addEventListener("submit", function(e){
  42. e.preventDefault();
  43. login();
  44. });
  45. var prevBtn = document.getElementById("prev")
  46. prevBtn.addEventListener("click", function(){
  47. window.location ="/info"
  48. })
  49. });