-1

My html code like this :

<li id="login" class="hide-on-med-and-down">
    <a class="dark-text" style="padding-right:0 !important" href="#">Login</a>
</li>
<li id="user-login">
    <a class="white-txt dropdown-button" href="#" data-activates="user-dropdown">
        <span id="current-lang" data-code="">085132112345</span>
        <i class="fa fa-angle-down right"></i>
    </a>
    <ul id="user-dropdown" class="dropdown-content">
        <li class=""><a href="#">Profile</a></li>
        <li class="divider"></li>
        <li class="active"><a href="#">Logout</a></li>
        <li class="divider"></li>
    </ul>
</li>

My javascript code like this :

var x = document.getElementById("login");
var y = document.getElementById("user-login");
if (localStorage.getItem("loginChelseaLocalStorage") === null) {
    x.style.display = "block";
    y.style.display = "none";
}
else {
    x.style.display = "none";
    y.style.display = "block";
}

If the user login, it will store on local storage like this :

localStorage.setItem("loginChelseaLocalStorage", true);

If the user logout, it will remove local storage like this :

localStorage.removeItem("loginChelseaLocalStorage");

Is the use of local storage to store login information as above is correct and good?

moses toh
  • 12,344
  • 71
  • 243
  • 443

1 Answers1

0

Localstorage store string data so you should use JSON.stringify(true) and to get the item from local storage you can use JSON.parse(getItem("loginChelseaLocalStorage")) review the use of Localstorage here https://www.w3schools.com/jsref/prop_win_localstorage.asp

Nourhan Ahmed
  • 179
  • 1
  • 10