0

I'm trying to put together a VERY simple login page to test some things out (I'm not a web dev) and I created a script in javascript that sets an authorized variable to TRUE once the user successfully logs in and redirects the user to the index page. Once there, the body onload runs a function that checks to see if the authorized variable is true to allow the user to stay in the index page and than sets it to false.

The thing is that after I refresh the page the variable is now false (which it should be) but the FALSE condition is not running. It keeps running only the true condition.

Any thoughts?

    function validate()
{
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;

    if(username=="" && password=="")
    {
        alert("Please fill all the fields.")
        return true;
    }
    else if (username=="adm" && password=="test")
    {
        localStorage.setItem("authorized", true);
        window.location.href = "./index.html";
    }
    else
    {
        alert("Login failed.")
        return true;
    }}

    function isAutorized(){
    localStorage.getItem("authorized")
    if (localStorage.getItem("authorized")){
        console.log(localStorage.getItem("authorized"))
        console.log("variable is TRUE");
        localStorage.setItem("authorized", false);
    }
    else
    {
        console.log(localStorage.getItem("authorized"))
        console.log("variable is FALSE");
        window.location.href = "./login.html";
    }};

var=false but condition true is running printscreen

MacHenry
  • 1
  • 2
  • Local storage stores **strings**. Both the strings `"true"` and `"false"` are [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy) values. – T.J. Crowder Mar 08 '22 at 09:28
  • As an aside, you realise this is not secure _at all_ right? – Jamiec Mar 08 '22 at 09:29
  • I strongly recommend that you don't stumble around in the dark with a `console.log` torch. Instead, *turn on the lights* using the debugger built into your IDE and/or browser. More [here](https://stackoverflow.com/questions/25385173/) and [here](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – T.J. Crowder Mar 08 '22 at 09:32

0 Answers0