我有一个使用 phonegap 开发的网络应用程序,其中索引页面是登录页面/注册页面。
这很好,因为这是新用户在未注册使用该应用程序时应该看到的第一页。然而,这给我带来了 2 个问题,即当用户登录并点击后退按钮时,它会返回登录页面,我不希望这种情况发生。此外,当存储用户凭据并打开应用程序时,它仍然会进入登录页面片刻,然后进入应用程序。
我怎样才能阻止这种情况发生?我是否必须实现 cookie/会话类型的登录系统?
我的代码如下
HTML
<div id="home">
<div id="launcherPage" data-role="page">
<!-- I'm just here waiting for deviceReady -->
</div>
<div id="loginPage" data-role="page">
<div data-role="header">
<h1>CHUNE</h1>
</div>
<div data-role="content">
<form id="loginForm">
<div data-role="fieldcontain" class="ui-hide-label">
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="" placeholder="Username" />
</div>
<div data-role="fieldcontain" class="ui-hide-label">
<label for="password">Password:</label>
<input type="password" name="password" id="password" value="" placeholder="Password" />
</div>
<input type="submit" value="Login" id="submitButton">
</form>
<div style="text-align: center;">Or</div> <!--need to center-->
<a href="./register.html" data-role="button">Register</a>
</div>
<div data-role="footer">
<h4>© KewsPlus</h4>
</div>
</div>
</div>
JavaScript
function init() {
document.addEventListener("deviceready", deviceReady, true);
delete init;
}
function checkPreAuth() {
console.log("checkPreAuth");
var form = $("#loginForm");
if(window.localStorage["username"] != undefined && window.localStorage["password"] != undefined) {
$("#username", form).val(window.localStorage["username"]);
$("#password", form).val(window.localStorage["password"]);
handleLogin();
}
}
function handleLogin() {
var url = 'http://kewsplus.com/includes/login.php';
var form = $("#loginForm");
//disable the button so we can't resubmit while we wait
$("#submitButton",form).attr("disabled","disabled");
var u = $("#username", form).val();
var p = $("#password", form).val();
if(u != '' && p!= '') {
$.ajax({
type: "POST",
url: url,
data: {username:u,password:p},
dataType: "jsonp",
crossDomain: "true",
jsonp : "onJSONPLoad",
success: function (data) {
if (data = 1) {
//store
window.localStorage["username"] = u;
window.localStorage["password"] = p;
$.mobile.changePage("featuredtracks.html");
} else {
navigator.notification.alert("Your login failed", function() {});
}
}
}); //$.ajax
$("#submitButton").removeAttr("disabled");
}
else {
navigator.notification.alert("You must enter a username and password", function() {});
$("#submitButton").removeAttr("disabled");
}
return false;
}
function deviceReady() {
console.log("deviceReady");
$("#loginPage").on("pageinit",function() {
console.log("pageinit run");
$("#loginForm").on("submit",handleLogin);
checkPreAuth();
});
$.mobile.changePage("#loginPage");
});