Skip to content

Instantly share code, notes, and snippets.

@mcongrove
Created May 3, 2011 20:14
Show Gist options
  • Save mcongrove/954129 to your computer and use it in GitHub Desktop.
Save mcongrove/954129 to your computer and use it in GitHub Desktop.
Simple login example
var login = {
authorized: false,
session_id: null,
init: function() {
// See if we have an existing sess_id
login.session_id = login.checkForSession();
// If not, open login window
if(!login.session_id) {
login.openLoginPage();
}
},
checkForSession: function() {
// See if we have the property available
if(Ti.App.Properties.hasProperty("session_id")) {
return Ti.App.Properties.getString("session_id");
} else {
return false;
}
},
openLoginPage: function() {
// Create a window with text fields for username / password
// Create a submit button for login
submitButton.addEventListener("click", function(_event) {
// Check the supplied credentials
login.authorize();
}
},
authorize: function() {
// Call out to a login API
var httpc = Ti.Network.createHTTPClient();
httpc.onload = function(e) {
login.authorizeCallback(this.responseText);
};
httpc.onerror = function(_event) {
// Show error message
};
httpc.open("GET", "http://---/req.php");
},
authorizeCallback: function(_data) {
// Grab the JSON response (see below) from server
// { e: "ok", id: 123 }
if(_data && _data.e == "ok") {
login.complete(_data.id);
} else {
login.error();
}
},
complete: function(_id) {
// User is authorized
login.authorized = true;
login.session_id = _id;
Ti.App.Properties.setString("session_id", _id);
// Close login screen
},
error: function() {
// Incorrect user / password
// Show error message, allow retry
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment