Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
log in to web app and return data to be saved in titanium app
//var win = Titanium.UI.currentWindow;
var currentWin = Ti.UI.currentWindow;
// if successful login - save user data to database
function insertRows(dbData) {
// call the database and table
var db = Ti.Database.install('../myapp.sqlite','users');
// call out the info to save then save it
var theData = db.execute('INSERT INTO users (username, email) VALUES("'+name.value+'","'+email.value+'")');
// run the db.execute line
theData;
// alert on success
alert("Rows Inserted");
};
var username = Titanium.UI.createTextField({
color:'#336699',
top:10,
left:10,
width:300,
height:40,
hintText:'Username',
keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
returnKeyType:Titanium.UI.RETURNKEY_DEFAULT,
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
currentWin.add(username);
var password = Titanium.UI.createTextField({
color:'#336699',
top:60,
left:10,
width:300,
height:40,
hintText:'Password',
passwordMask:true,
keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
returnKeyType:Titanium.UI.RETURNKEY_DEFAULT,
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
currentWin.add(password);
var loginBtn = Titanium.UI.createButton({
title:'Login',
top:110,
width:90,
height:35,
borderRadius:1,
font:{fontFamily:'Arial',fontWeight:'bold',fontSize:14}
});
currentWin.add(loginBtn);
var loginReq = Titanium.Network.createHTTPClient();
loginReq.onload = function()
{
var json = this.responseText;
var response = JSON.parse(json);
// if we get a positive response from the server
if (response.logged == true) {
// set the variables for our user db table
var dbData = {
name:response.name,
email:response.email
};
// add the above var data to the table
insertRows(dbData);
// fire this alert as proof of what we are being returned
//alert( response.name + response.email);
username.blur();
password.blur();
Ti.App.fireEvent('grantEntrance', {
name:response.name,
email:response.email
});
//win.close();
}
// if the username pw combo returns false
else
{
// alert(response.message + response.username + response.password);
alert(response.message);
}
};
loginBtn.addEventListener('click',function(e)
{
if (username.value != '' && password.value != '')
{
// where are we sending the login credentials
loginReq.open("POST","http://mysite.com/users/app_login");
// what are we sending to the server
var params = {
username: username.value,
// hashed value
//password: Ti.Utils.md5HexDigest(password.value)
// unhashed value
password: password.value
};
loginReq.send(params);
}
else
{
alert("Username/Password are required");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment