try{ if(localStorage.username && localStorage.pwd){ console.log("localStorage username is set."); if(!sessionStorage.username){ console.log("sessionStorage username is NOT set."); auto_login(window.localStorage.getItem("username"), window.localStorage.getItem("pwd")); // console.log("user "+ sessionStorage.username +" is already logged in matching ."); }else{ console.log("sessionStorage username is set."); } }else{ console.log("localstorage username and password not set."); } }catch(e){ console.log("localstorage " + e); } async function auto_login(ausername, apwd_hash){ var url = "https://restaurnaut.com/api/v1/user/secure/login/"; var post_body = { username : ausername, pwd : apwd_hash }; //check await fetch(url, { method: 'POST', headers: { 'Content-Type' : 'application/json' }, body : JSON.stringify(post_body) }) .then(response => response.json()) .then(data => { if(data.code != null && data.code == 200){ console.log("Automatically logged the user '"+ausername+"' in."); console.log("send_login_request: " + data.message); sessionStorage.setItem("restaurnaut_key", data.key); sessionStorage.setItem("username", data.username); sessionStorage.setItem("user_id", data.user_id); restaurnaut_key = data.key; console.log("SET SESSION STORAGE API KEY : "+sessionStorage.getItem("restaurnaut_key")); console.log("18.225.56.185"); 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"; document.getElementById("history-link-sidebar").style.display = "block"; //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("favorites-sidebar").style.display = "block"; document.getElementById("likes-sidebar").style.display = "block"; document.getElementById("dislikes-sidebar").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"; console.log("Logged in user " + ausername); }else if(data.code != null && data.code == 409){ //User is already logged in. console.log("user is already logged in."); document.getElementById("sign-up-link-sidebar").style.display = "none"; document.getElementById("sign-up-link-footer").style.display = "none"; document.getElementById("history-link-footer").style.display = "block"; document.getElementById("history-link-sidebar").style.display = "block"; //Change the login to logout document.getElementById("login-link-footer").textContent = "logout"; document.getElementById("login-link-header").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("favorites-sidebar").style.display = "block"; document.getElementById("likes-sidebar").style.display = "block"; document.getElementById("dislikes-sidebar").style.display = "block"; document.getElementById("footer-hl1").style.display = "block"; document.getElementById("footer-hl2").style.display = "block"; document.getElementById("footer-hl3").style.display = "block"; sessionStorage.setItem("username", localStorage.username); sessionStorage.setItem("user_id", localStorage.user_id); console.log("Set sessionStorage -> " + sessionStorage.username + " and " + sessionStorage.user_id); }else{ console.log("there was an error logging the user '"+ausername+"' in."); } }); }