6

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

GreenROBO
  • 4,725
  • 4
  • 23
  • 43

3 Answers3

10

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");
locknies
  • 1,345
  • 3
  • 15
  • 36
  • Dear @KRIZTE I don't have Logout page or anything my main question is how to get that user has clicked in Logout link(which is in that website) i have made only login page nothing else after that i'm throwing it to website. your answer is really appreciable but I want to know when user click on logout link. You Get my point??? – GreenROBO Jan 12 '15 at 05:52
0

You can store the login status and the session id in the sqlLite or LocalStorage. And while starting the app, you can check for the values in storage you have used.

For your reference, you can check this link

http://docs.phonegap.com/en/edge/cordova_storage_localstorage_localstorage.md.html

Ashish_B
  • 114
  • 1
  • 9
  • it is the Single Page Application. Then i'm throwing directly to website. – GreenROBO Jan 08 '15 at 14:17
  • This works fine for SPA also. You have to check login status while splash screen is being displayed, if the user is logged in- redirect it to website else - to your login page – Ashish_B Jan 08 '15 at 14:21
  • and what should i do when the user click on logout which is in that website? how my SPA come to know that user has clicked on logout? – GreenROBO Jan 09 '15 at 05:42
  • One solution is: you can open that site inside your app using iframe which will keep the controls with you and you can always access your local storage – Ashish_B Jan 09 '15 at 07:40
  • Actually i'm not getting it can you give any example please? I am using LocalStorage as you sugested so if i'll succeed then tick on accepted. :) – GreenROBO Jan 09 '15 at 09:50
0

Use localStorage instead sessionStorage. For more better, use sqlite. SessionStorage only temporarily compare to localStorage. But sqlite more persistent.

localStorage syntax

localStorage.userId

sessionStorage syntax

sessionStorage.userId

sqlite

https://github.com/brodysoft/Cordova-SQLitePlugin

Nurdin
  • 23,382
  • 43
  • 130
  • 308
  • and what should i do when the user click on logout which is in that website? how my SPA come to know that user has clicked on logout? – GreenROBO Jan 09 '15 at 05:43
  • and if you don't mind can i have any eample regarding that. sorry but i'm Brand Newto phoneGap So... – GreenROBO Jan 09 '15 at 05:49