Reputation: 527
I want to develop a mobile app using the Web stack and package with phonegap.
My index.html page will contain a login form. I have a js function checkLoggedIn()
;
that checks if user is logged in by looking for a localStorage variable.
Challenge : I want index.html
to automatically redirect to member.html
if checkLoggedIn()
returns true
; otherwise it won't redirect and just stays on index.html
, that means checkLoggedIn()
will run on index.html
on load.
I don't know what kind of event to fire or how to fire it to achieve this.
Upvotes: 0
Views: 581
Reputation: 725
This is my working code for the auto login feature hope this works for both android and ios
Below is the code for login button after entering the user name and password
$('#login').click(function () {
var userName = $('#Username').val();
var password = $('#password').val();
if (userName == "" || password == "") {
window.plugins.toast.showLongBottom("Please enter a valid data");
return false;
}
var options = { dimBackground: true };
SpinnerPlugin.activityStart("Loading...", options);
$.ajax({
type: 'GET',
url: xxxxxx.xxxx.xxxxx,
data: "uid=" + userName + "&pwd=" + password + "",
success: function (resp) {
SpinnerPlugin.activityStop();
if (resp.status != 0) {
if (resp.RoleId == 1) {
mash.Session.getInstance().set({
userId: resp.sno,
userName: resp.Name,
});
var session = mash.Session.getInstance().get();
window.open('Add.html', '_self', 'location=no');
// Create session.
var today = new Date();
var expirationDate = new Date();
expirationDate.setTime(today.getTime() mash.Settings.sessionTimeoutInMSec);
}
else {
SpinnerPlugin.activityStop();
mash.Session.getInstance().set({
userId: resp.sno,
userName: resp.Name,
});
var session = mash.Session.getInstance().get();
var username = userName;
var password = password;
window.localStorage.setItem("uname", resp.Name);
window.localStorage.setItem("pass", password);
window.localStorage.setItem("sno", resp.sno);
window.localStorage.setItem("RoleId", resp.RoleId);
window.open('Main.html', '_self', 'location=no');
//window.plugins.toast.showLongBottom("Login Successfull");
// Create session.
var today = new Date();
var expirationDate = new Date();
expirationDate.setTime(today.getTime() + mash.Settings.sessionTimeoutInMSec);
}
}
else {
SpinnerPlugin.activityStop();
window.plugins.toast.showLongBottom("Please Enter valid Username and password");
SpinnerPlugin.activityStop();
}
},
error: function (e) {
SpinnerPlugin.activityStop();
window.plugins.toast.showLongBottom("Invalid Data");
SpinnerPlugin.activityStop();
}
});
});
after this in the index.html use onload handler fillpassword() to use this below function
function fillpassword() {
if (window.localStorage.getItem("uname") != 0) {
mash.Session.getInstance().set({
userId: window.localStorage.getItem("sno"),
userName: window.localStorage.getItem("uname"),
});
if (window.localStorage.getItem("RoleId") != 1) {
document.getElementById('Username').value = window.localStorage.getItem("uname");
document.getElementById('password').value = window.localStorage.getItem("pass");
window.open('Main.html', '_self', 'location=no');
}
}
else {
//alert("Yes")
}
}
the above code works and all u need to maintain a session
Upvotes: 0
Reputation: 1136
What about:
if(checkLoggedIn()) window.location = "member.html";
Upvotes: 1