javascript 如何在phoneGap中维护会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27841656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To maintain session in phoneGap
提问by GreenROBO
I am not able to find any way to handle this. I've developed one single page application in PhoneGap that is just login module, after login i throw it to web site. now the thing is i have to maintain login status also. For Example if the user exit from application without logout then he should not get my login screen it should directly go to the website.
我找不到任何方法来处理这个问题。我在 PhoneGap 中开发了一个单页应用程序,它只是登录模块,登录后我将它扔到网站上。现在的问题是我也必须保持登录状态。例如,如果用户在没有注销的情况下退出应用程序,那么他不应该看到我的登录屏幕,它应该直接转到网站。
In android we can handle it using shared preferences that i know but i am new into PhoneGap. Also when a user click on the logout My Login screen should appear(not that particular website's login screen!!!).
在android中,我们可以使用我知道的共享首选项来处理它,但我是PhoneGap的新手。此外,当用户单击注销时,我的登录屏幕应该会出现(不是那个特定网站的登录屏幕!!!)。
I've googled it but cannot find anything Helpful
我用谷歌搜索但找不到任何有用的东西
回答by locknies
You can make use of localStorage, as Nurdin said its not that persistent.
您可以使用 localStorage,因为 Nurdin 说它不是那么持久。
Read more about it here. http://www.w3schools.com/html/html5_webstorage.asp
在此处阅读更多相关信息。 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.
在注销页面中清除此 localStorage 值。
window.localStorage.removeItem("loggedIn");
window.localStorage.removeItem("username");
回答by Ashish_B
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.
您可以将登录状态和会话 ID 存储在 sqlLite 或 LocalStorage 中。在启动应用程序时,您可以检查已使用的存储中的值。
For your reference, you can check this link
供您参考,您可以查看此链接
http://docs.phonegap.com/en/edge/cordova_storage_localstorage_localstorage.md.html
http://docs.phonegap.com/en/edge/cordova_storage_localstorage_localstorage.md.html
回答by Mohammad Nurdin
Use localStorage
instead sessionStorage
. For more better, use sqlite
. SessionStorage only temporarily compare to localStorage. But sqlite more persistent.
使用localStorage
代替sessionStorage
。为了更好,请使用sqlite
. SessionStorage 只是暂时与 localStorage 比较。但是sqlite更持久。
localStorage syntax
本地存储语法
localStorage.userId
sessionStorage syntax
会话存储语法
sessionStorage.userId
sqlite
sqlite