I was trying not to use database but the browser local-storage for a login example. I mean when user give 'username' and 'password' it will check from browser local-storage and then be able to login. I am using localStorage.setItem and localStorage.getItem of HTML5 to get the data and show it. Here is my Code-
var user_name=document.getElementById('userName').value;
var user_pswd=document.getElementById('password').value;
localStorage.setItem("user", user_name);
localStorage.setItem("pass", user_pswd);
// Retrieve
document.getElementById("output").innerHTML = localStorage.getItem("user");
It set the data and shows the user name,But is it possible to get the data after restarting the browser?? When I try to login for first time, how can I check with the browser data? Please any help is highly appreciated..
I was trying not to use database but the browser local-storage for a login example. I mean when user give 'username' and 'password' it will check from browser local-storage and then be able to login. I am using localStorage.setItem and localStorage.getItem of HTML5 to get the data and show it. Here is my Code-
var user_name=document.getElementById('userName').value;
var user_pswd=document.getElementById('password').value;
localStorage.setItem("user", user_name);
localStorage.setItem("pass", user_pswd);
// Retrieve
document.getElementById("output").innerHTML = localStorage.getItem("user");
It set the data and shows the user name,But is it possible to get the data after restarting the browser?? When I try to login for first time, how can I check with the browser data? Please any help is highly appreciated..
Share Improve this question edited May 3, 2017 at 19:42 Arashsoft 2,7676 gold badges37 silver badges61 bronze badges asked Oct 30, 2014 at 6:03 SubhoSubho 9235 gold badges25 silver badges49 bronze badges 3- This does not look like a safe/sane thing to do. – user3459110 Commented Oct 30, 2014 at 6:11
- Yaa I know but I have to do it that way.. Please help me – Subho Commented Oct 30, 2014 at 6:18
- Possible duplicate of Storing Objects in HTML5 localStorage – Arashsoft Commented May 3, 2017 at 14:42
1 Answer
Reset to default 3restarting browser does not delete local storage data.
you can check if you have data for both keys (userName , password) with following condition
if ((localStorage.getItem("userName") !== null) && (localStorage.getItem("password") !== null))
{
// you have values for both userName and password
}
for your case you can test if textbox values match the values in local storage
if ((localStorage.getItem("userName") === null) && (localStorage.getItem("password") === null))
{
localStorage.setItem("user", "Subho"); // writes name and password to local storage if not exists
localStorage.setItem("pass", "Subho");
}
if ( (localStorage.getItem("user") == document.getElementById('userName').value)
{
if ( (localStorage.getItem("pass") == document.getElementById('password').value)
{
// login is successful
}
}
however this is not a safe login method , local storage data is accesible by users