How To maintain session in phoneGap

GreenROBO picture GreenROBO · Jan 8, 2015 · Viewed 12.5k times · Source

I am unable to find any way how to handle this. I have developed one single page application in PhoneGap which is only login page.
After login, I am redirecting it to web site. Now the thing is I have to maintain login status inside App.
For Example if the user exit from application without logout then he should not get my login screen in next launch. It should directly go to the website.

In android we can handle it using shared preferences that I know but I am totally new into PhoneGap. Also, when a user click on the logout Which is in the redirected website, My App's Login screen should appear instead of Website's Login Screen.

I've googled it but cannot find anything Helpful

Answer

locknies picture locknies · Jan 9, 2015

You can make use of localStorage, as Nurdin said its not that persistent.

Read more about it here. http://www.w3schools.com/html/html5_webstorage.asp

so you must put a condition to check if the user is logged in or not before login page, ie

if(window.localStorage.getItem("loggedIn") == 1) {
// Logged In
// Redirect to first page after logged in.
}
else
{
// Redirect to login page.
}

In login page, after the login success.

window.localStorage.setItem("loggedIn", 1);
window.localStorage.setItem("username", document.getElementsByName("usernametextbox").value);

etc..

In logout page clear this localStorage value.

window.localStorage.removeItem("loggedIn");
window.localStorage.removeItem("username");