try{ //From login_modal document.getElementById("login-link-header").addEventListener("click", function(){ if(document.getElementById("login-link-header").textContent == "login"){ console.log("login: login-link-header modal loaded."); document.getElementById('login-modal').style.display='block'; document.getElementById("sidebar-menu").style.display = "none"; } }); document.getElementById("login-link-footer").addEventListener("click", function(){ if(document.getElementById("login-link-footer").textContent == "login"){ console.log("login: login-link-footer modal loaded."); document.getElementById('login-modal').style.display='block'; document.getElementById("sidebar-menu").style.display = "none"; } }); document.getElementById("login-link-sidebar").addEventListener("click", function(){ if(document.getElementById("login-link-sidebar").textContent == "login"){ console.log("login: login-link-sidebar modal loaded."); document.getElementById('login-modal').style.display='block'; document.getElementById("sidebar-menu").style.display = "none"; } }); document.getElementById("login-reload-button").addEventListener("click", function(){ if(document.getElementById("login-link-header").textContent == "login"){ console.log("login: modal loaded."); document.getElementById('login-modal').style.display='block'; document.getElementById('logout-modal').style.display='none'; document.getElementById("sidebar-menu").style.display = "none"; } }); }catch(e){ console.error(e); } // document.getElementById("login-reload-button").addEventListener("click", function(){ // console.log("login: User would like to log in again."); // }); //Make sure the message is invisible by default. var login_modal_error_div = document.getElementById("login-modal-error"); var login_modal_msg_div = document.getElementById("login-modal-msg"); var login_modal_error_msg = document.getElementById("login-modal-error-msg"); var login_modal_msg_msg = document.getElementById("login-modal-msg-msg"); login_modal_error_div.style.display = "none"; //Event listener for login-button press document.getElementById("login-button").addEventListener("click", async function(){ // document.getElementById("login-button").addEventListener("click", function(){ console.log("login: form completed."); //Check all fields first. var username = document.getElementById("username").value; var pwd = document.getElementById("pwd").value; var username_empty = false; var pwd_empty = false; //Check username if(username === null || username.length < 1){ username_empty = true; } //Check password if(pwd === null || pwd.length < 1){ pwd_empty = true; } if(username_empty && pwd_empty){ login_modal_msg_msg.textContent = "Your username and password were too short or missing entirely. Try again."; login_modal_msg_div.style.display = "block"; }else if(username_empty){ login_modal_msg_msg.textContent = "Your username was too short or missing entirely. Try again."; login_modal_msg_div.style.display = "block"; }else if(pwd_empty){ login_modal_msg_msg.textContent = "Your password was too short or missing entirely. Try again."; login_modal_msg_div.style.display = "block"; }else{ //Encrypt the password and send the request. var hash = md5(pwd); console.log(hash); var code = await send_login_request(username, hash); // var code = send_login_request(username, hash); if(code == 200){ document.getElementById("sign-up-link-footer").style.display = "none"; document.getElementById("sign-up-link-sidebar").style.display = "none"; document.getElementById("history-link-footer").style.display = "block"; login_modal_error_div.style.display = "none"; //Pop up an alert that lasts 2 seconds. //Change the login to logout document.getElementById("login-link-footer").textContent = "logout"; document.getElementById("login-link-header").textContent = "logout"; document.getElementById("login-link-sidebar").textContent = "logout"; //Make the account link visible. document.getElementById("account-link-footer").style.display = "block"; document.getElementById("account-link-navbar").style.display = "block"; document.getElementById("favorites-footer").style.display = "block"; document.getElementById("likes-footer").style.display = "block"; document.getElementById("dislikes-footer").style.display = "block"; document.getElementById("footer-hl1").style.display = "block"; document.getElementById("footer-hl2").style.display = "block"; document.getElementById("footer-hl3").style.display = "block"; document.getElementById("sidebar-hl1").style.display = "block"; document.getElementById("sidebar-hl2").style.display = "block"; document.getElementById("sidebar-hl3").style.display = "block"; // document.getElementById("favorites-and-likes-input-radio-div").style.display = "block"; // document.getElementById("favorites-and-likes-input-radio-sub").style.display = "none"; //remove the modal document.getElementById('login-modal').style.display='none'; //Change the name on the message document.getElementById("login-success-modal-username").textContent = username; //Show success message. document.getElementById("login-success-modal").style.display = "block"; await sleep(1500); document.getElementById("login-success-modal").style.display = "none"; console.log("Logged in user " + username); try{ if(sessionStorage.username){ document.getElementById("favorites-and-likes-input-radio-div").style.display = "block"; document.getElementById("favorites-and-likes-input-radio-sub").style.display = "none"; }else{ document.getElementById("favorites-and-likes-input-radio-div").style.display = "none"; document.getElementById("favorites-and-likes-input-radio-sub").style.display = "block"; } }catch(e){ console.error("Could not change the visibility: " + e); } if( (location == "https://restaurnaut.com/profile") || (location == "https://restaurnaut.com/profile/") || (location == "https://restaurnaut.com/profile/history") || (location == "https://restaurnaut.com/profile/history/") || (location == "https://restaurnaut.com/profile/favorites") || (location == "https://restaurnaut.com/profile/favorites/") || (location == "https://restaurnaut.com/profile/likes") || (location == "https://restaurnaut.com/profile/likes/") || (location == "https://restaurnaut.com/profile/dislikes") || (location == "https://restaurnaut.com/profile/dislikes/") ){ location.reload(); } }else if(code == 404){ login_modal_error_msg.textContent = "That username and password does not exist. Check your username and password."; login_modal_error_div.style.display = "block"; }else if(code == 401){ login_modal_error_msg.textContent = "There was an issue logging you in. Check your username and password."; login_modal_error_div.style.display = "block"; } } });//end document.getElementById("login-button").onclick async function send_login_request(username, pwd_hashed){ return new Promise((resolve) => { console.log("send_login_request: Sending login request for user " + username); var xhr = new XMLHttpRequest(); // Paste your LocationIQ token below. xhr.open('POST', "https://restaurnaut.com/api/v1/user/secure/login/", true); xhr.send('{"username" : "'+username+'", "pwd" : "'+pwd_hashed+'"}'); xhr.onreadystatechange = processRequest; // xhr.addEventListener("readystatechange", processRequest, false); function processRequest(e) { if (xhr.readyState == 4 && xhr.status == 200) { var response = JSON.parse(xhr.responseText); console.log("send_login_request: " + response.message); sessionStorage.setItem("restaurnaut_key", response.key); sessionStorage.setItem("username", response.username); sessionStorage.setItem("user_id", response.user_id); restaurnaut_key = response.key; console.log("SET SESSION STORAGE API KEY : "+sessionStorage.getItem("restaurnaut_key")); console.log("3.137.169.218"); if(document.getElementById("remember-me-checkbox").checked){ console.log("remember-me-checkbox checked. Will remember user " + username + "."); window.localStorage.setItem("username", username); window.localStorage.setItem("pwd", pwd_hashed); }else{ console.log("remember-me-checkbox not checked. Will not remember user."); } resolve(xhr.status); //RESOLVE THE PROMISE }else if(xhr.status == 401){ var response = JSON.parse(xhr.responseText); console.log("send_login_request: " + response.message); login_modal_error_msg.textContent = response.message; resolve(xhr.status); } resolve(xhr.status); } }); } //Define a sleep function function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); }