EntropyStarRover

Biggest catch log reg

Mar 3rd, 2021 (edited)
754
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachEvents() {
  2.     console.log("LOADING LOGIN PAGE CLEARS THE SESSION STORAGE!")
  3.     sessionStorage.clear();
  4.    
  5.  
  6.     let forms = Array.from(document.getElementsByTagName("form"));
  7.     let regForm = forms[0];
  8.  
  9.  
  10.     regForm.addEventListener("submit", function (e) {
  11.         e.preventDefault();
  12.         let regFormData = new FormData(regForm)
  13.         register(regFormData);
  14.     })
  15.  
  16.     function register(regFormData) {
  17.         let email = regFormData.get("email");
  18.         let password = regFormData.get("password");
  19.         let rePass = regFormData.get("rePass");
  20.  
  21.         if (email.length > 0 && password.length > 0 && rePass.length > 0) {
  22.  
  23.             if (password !== rePass) {
  24.                 alert("Passwords are different!");
  25.             } else {
  26.                 let newUser = {
  27.                     email,
  28.                     password
  29.                 }
  30.  
  31.                 let myHeaders = new Headers();
  32.                 myHeaders.append("Content-Type", "application/json");
  33.  
  34.                 var raw = JSON.stringify(newUser);
  35.  
  36.                 var requestOptions = {
  37.                     method: 'POST',
  38.                     headers: myHeaders,
  39.                     body: raw,
  40.                     redirect: 'follow'
  41.                 };
  42.  
  43.                 fetch("http://localhost:3030/users/register", requestOptions)
  44.                     .then(response => response.json())
  45.                     .then(result => redirect(result.accessToken, email))
  46.                     .catch(error => console.log('error', error));
  47.             }
  48.         }
  49.     }
  50.  
  51.  
  52.  
  53.     let logForm = forms[1];
  54.     logForm.addEventListener("submit", function (e) {
  55.         e.preventDefault();
  56.         let logFormData = new FormData(logForm)
  57.         login(logFormData);
  58.     })
  59.  
  60.     function login(logForm) {
  61.         let email = logForm.get("password");
  62.         let password = logForm.get("password");
  63.  
  64.         let logObj = {
  65.             email,
  66.             password
  67.         }
  68.  
  69.         let myHeaders = new Headers();
  70.         myHeaders.append("Content-Type", "application/json");
  71.  
  72.         let raw = JSON.stringify(logObj);
  73.  
  74.         var requestOptions = {
  75.             method: 'POST',
  76.             headers: myHeaders,
  77.             body: raw,
  78.             redirect: 'follow'
  79.         };
  80.  
  81.         fetch("http://localhost:3030/users/login", requestOptions)
  82.             .then(response => response.json())
  83.             .then(result=> redirect(result.accessToken,email))
  84.            
  85.             .catch(error => console.log('error', error));
  86.     }
  87.  
  88.     function redirect(token,email){
  89.         sessionStorage.setItem("token", token);
  90.         console.log(sessionStorage.getItem("token"));
  91.         sessionStorage.setItem("loggedEmail",email);
  92.         console.log(sessionStorage.getItem("loggedEmail"))
  93.         window.location.pathname="/index.html";
  94.      
  95.     }
  96.  
  97. }
  98.  
  99. attachEvents()
RAW Paste Data