简体   繁体   中英

How can I using local storage to store login information?

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?

Localstorage 存储字符串数据,因此您应该使用 JSON.stringify(true) 并从本地存储中获取项目,您可以使用 JSON.parse(getItem("loginChelseaLocalStorage")) 在此处查看 Localstorage 的使用https://www.w3schools。 com/jsref/prop_win_localstorage.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM